CustomScpExplorer.cpp 344 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523
  1. //---------------------------------------------------------------------------
  2. #define NO_WIN32_LEAN_AND_MEAN
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include <Common.h>
  6. #include "CustomScpExplorer.h"
  7. #include <Bookmarks.h>
  8. #include <Interface.h>
  9. #include <Exceptions.h>
  10. #include <CoreMain.h>
  11. #include <FileSystems.h>
  12. #include <TextsCore.h>
  13. #include <TextsWin.h>
  14. #include <HelpWin.h>
  15. #include <VCLCommon.h>
  16. #include <Progress.h>
  17. #include <SynchronizeProgress.h>
  18. #include <DragExt.h>
  19. #include <WinApi.h>
  20. #include "GUITools.h"
  21. #include "NonVisual.h"
  22. #include "Glyphs.h"
  23. #include "Tools.h"
  24. #include "WinConfiguration.h"
  25. #include "TerminalManager.h"
  26. #include "EditorManager.h"
  27. #include "ProgParams.h"
  28. #include "Setup.h"
  29. #include <Consts.hpp>
  30. #include <DateUtils.hpp>
  31. #include <TB2Common.hpp>
  32. #include <DirectoryMonitor.hpp>
  33. //---------------------------------------------------------------------------
  34. #pragma package(smart_init)
  35. #pragma link "CustomDirView"
  36. #pragma link "CustomUnixDirView"
  37. #pragma link "IEListView"
  38. #pragma link "NortonLikeListView"
  39. #pragma link "UnixDirView"
  40. #pragma link "CustomDriveView"
  41. #pragma link "UnixDriveView"
  42. #pragma link "CustomDriveView"
  43. #pragma link "TB2Dock"
  44. #pragma link "TBXStatusBars"
  45. #pragma link "TB2Item"
  46. #pragma link "TB2Toolbar"
  47. #pragma link "ThemePageControl"
  48. #pragma link "PathLabel"
  49. #ifndef NO_RESOURCES
  50. #pragma resource "*.dfm"
  51. #endif
  52. //---------------------------------------------------------------------------
  53. #define SAVE_SELECTION(DIRVIEW) \
  54. UnicodeString FocusFile = L""; \
  55. UnicodeString LastFocusedFile = L""; \
  56. if (DIRVIEW->ItemFocused) LastFocusedFile = DIRVIEW->ItemFocused->Caption; \
  57. { TListItem * ClosestUnselected = DIRVIEW->ClosestUnselected(DIRVIEW->ItemFocused); \
  58. if (ClosestUnselected) FocusFile = ClosestUnselected->Caption; }
  59. #define RESTORE_SELECTION(DIRVIEW) \
  60. if (!LastFocusedFile.IsEmpty() && \
  61. (!DIRVIEW->ItemFocused || (DIRVIEW->ItemFocused->Caption != LastFocusedFile))) \
  62. { \
  63. TListItem *ItemToSelect = DIRVIEW->FindFileItem(FocusFile); \
  64. if (ItemToSelect) \
  65. { \
  66. DIRVIEW->ItemFocused = ItemToSelect; \
  67. DIRVIEW->ItemFocused->MakeVisible(False); \
  68. } \
  69. }
  70. //---------------------------------------------------------------------------
  71. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  72. static const int SessionPanelCount = 4;
  73. //---------------------------------------------------------------------------
  74. class TMutexGuard
  75. {
  76. public:
  77. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  78. int Timeout = 5000)
  79. {
  80. FMutex = NULL;
  81. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  82. if (WaitResult == WAIT_TIMEOUT)
  83. {
  84. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  85. }
  86. else
  87. {
  88. FMutex = AMutex;
  89. }
  90. }
  91. ~TMutexGuard()
  92. {
  93. if (FMutex != NULL)
  94. {
  95. ReleaseMutex(FMutex);
  96. }
  97. }
  98. private:
  99. HANDLE FMutex;
  100. };
  101. //---------------------------------------------------------------------------
  102. class TWindowLock
  103. {
  104. public:
  105. TWindowLock(TCustomScpExplorerForm * Form) :
  106. FForm(Form)
  107. {
  108. FForm->LockWindow();
  109. }
  110. ~TWindowLock()
  111. {
  112. FForm->UnlockWindow();
  113. }
  114. private:
  115. TCustomScpExplorerForm * FForm;
  116. };
  117. //---------------------------------------------------------------------------
  118. class TAutoBatch
  119. {
  120. public:
  121. TAutoBatch(TCustomScpExplorerForm * Form) :
  122. FForm(Form)
  123. {
  124. FForm->BatchStart(FBatchStorage);
  125. }
  126. ~TAutoBatch()
  127. {
  128. FForm->BatchEnd(FBatchStorage);
  129. }
  130. private:
  131. TCustomScpExplorerForm * FForm;
  132. void * FBatchStorage;
  133. };
  134. //---------------------------------------------------------------------------
  135. struct TTransferOperationParam
  136. {
  137. TTransferOperationParam();
  138. UnicodeString TargetDirectory;
  139. bool Temp;
  140. bool DragDrop;
  141. int Options;
  142. TAutoSwitch Queue;
  143. };
  144. //---------------------------------------------------------------------------
  145. TTransferOperationParam::TTransferOperationParam()
  146. {
  147. Temp = false;
  148. DragDrop = false;
  149. Options = 0;
  150. Queue = asAuto;
  151. }
  152. //---------------------------------------------------------------------------
  153. class TTransferPresetNoteData : public TObject
  154. {
  155. public:
  156. UnicodeString Message;
  157. };
  158. //---------------------------------------------------------------------------
  159. class TTerminalNoteData : public TObject
  160. {
  161. public:
  162. TTerminal * Terminal;
  163. };
  164. //---------------------------------------------------------------------------
  165. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  166. FFormRestored(false),
  167. TForm(Owner)
  168. {
  169. FCurrentSide = osRemote;
  170. FEverShown = false;
  171. FDocks = new TList();
  172. InitControls();
  173. RestoreParams();
  174. ConfigurationChanged();
  175. RemoteDirView->Invalidate();
  176. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  177. NonVisualDataModule->ScpExplorer = this;
  178. FAutoOperation = false;
  179. FOnFileOperationFinished = NULL;
  180. FForceExecution = false;
  181. FIgnoreNextDialogChar = 0;
  182. FErrorList = NULL;
  183. FSynchronizeProgressForm = NULL;
  184. FProgressForm = NULL;
  185. FRefreshLocalDirectory = false;
  186. FRefreshRemoteDirectory = false;
  187. FDDExtMapFile = NULL;
  188. // CreateMutexW keeps failing with ERROR_NOACCESS
  189. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  190. DebugAssert(FDDExtMutex != NULL);
  191. FDDTargetControl = NULL;
  192. FDelayedDeletionTimer = NULL;
  193. FDelayedDeletionList = new TStringList();
  194. FDDFileList = NULL;
  195. FPendingTempSpaceWarn = false;
  196. FCapturedLog = NULL;
  197. FDragDropOperation = false;
  198. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  199. FAllowTransferPresetAutoSelect = true;
  200. FCopyParamDefault = L"";
  201. FSynchronizeController = NULL;
  202. FPendingQueueActionItem = NULL;
  203. FLockLevel = 0;
  204. FLockSuspendLevel = 0;
  205. FDisabledOnLockSuspend = false;
  206. FAlternativeDelete = false;
  207. FTrayIcon = new ::TTrayIcon(0);
  208. FTrayIcon->OnClick = TrayIconClick;
  209. FMaxQueueLength = 0;
  210. FLastContextPopupScreenPoint = TPoint(-1, -1);
  211. FTransferResumeList = NULL;
  212. FMoveToQueue = false;
  213. FStandaloneEditing = false;
  214. FOnFeedSynchronizeError = NULL;
  215. FNeedSession = false;
  216. FDoNotIdleCurrentTerminal = 0;
  217. FIncrementalSearching = 0;
  218. FIncrementalSearchHaveNext = false;
  219. FEditorManager = new TEditorManager();
  220. FEditorManager->OnFileChange = ExecutedFileChanged;
  221. FEditorManager->OnFileReload = ExecutedFileReload;
  222. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  223. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  224. FLocalEditors = new TList();
  225. FQueueStatus = NULL;
  226. FQueueStatusSection = new TCriticalSection();
  227. FQueueStatusInvalidated = false;
  228. FQueueItemInvalidated = false;
  229. FQueueActedItem = NULL;
  230. FQueueController = new TQueueController(QueueView3);
  231. FUserActionTimer = new TTimer(this);
  232. FUserActionTimer->Enabled = false;
  233. FUserActionTimer->Interval = 10;
  234. FUserActionTimer->OnTimer = UserActionTimer;
  235. FNotes = new TStringList();
  236. FNoteTimer = new TTimer(this);
  237. FNoteTimer->Enabled = false;
  238. FNoteTimer->OnTimer = NoteTimer;
  239. FOnNoteClick = NULL;
  240. FOle32Library = LoadLibrary(L"Ole32.dll");
  241. FDragMoveCursor = FOle32Library != NULL ?
  242. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  243. UseSystemSettings(this);
  244. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  245. static_cast<TObject*>(GetComponent(fcTransferList)));
  246. DebugAssert(TransferList != NULL);
  247. FTransferListHoverIndex = -1;
  248. TransferList->OnChange = TransferListChange;
  249. TransferList->OnDrawItem = TransferListDrawItem;
  250. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))));
  251. SetSubmenu(NonVisualDataModule->ColorMenuItem);
  252. UseDesktopFont(SessionsPageControl);
  253. UpdateSessionsPageControlHeight();
  254. UseDesktopFont(RemoteDirView);
  255. UseDesktopFont(RemoteDriveView);
  256. UseDesktopFont(QueueView3);
  257. UseDesktopFont(QueueLabel);
  258. UseDesktopFont(RemoteStatusBar);
  259. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  260. QueueSplitter->ShowHint = true;
  261. RemotePanelSplitter->ShowHint = true;
  262. UpdateImages();
  263. FCustomCommandMenu = CreateTBXPopupMenu(this);
  264. FCustomCommandLocalFileList = NULL;
  265. FCustomCommandRemoteFileList = NULL;
  266. FSessionColors = new TPngImageList(this);
  267. FSessionColors->ColorDepth = cd32Bit;
  268. AddFixedSessionImages();
  269. SessionsPageControl->Images = FSessionColors;
  270. QueueLabel->FocusControl = QueueView3;
  271. UpdateQueueLabel();
  272. FRemoteDirViewWasFocused = true;
  273. CreateHiddenWindow();
  274. StartUpdates();
  275. }
  276. //---------------------------------------------------------------------------
  277. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  278. {
  279. // this has to be one of the very first things to do
  280. StopUpdateThread();
  281. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  282. TTerminalManager::Instance()->ScpExplorer = NULL;
  283. delete FCustomCommandLocalFileList;
  284. delete FCustomCommandRemoteFileList;
  285. delete FCustomCommandMenu;
  286. delete FTrayIcon;
  287. FTrayIcon = NULL;
  288. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  289. delete FEditorManager;
  290. ForceCloseLocalEditors();
  291. delete FLocalEditors;
  292. HideFileFindDialog();
  293. if (FDelayedDeletionTimer)
  294. {
  295. DoDelayedDeletion(NULL);
  296. SAFE_DESTROY(FDelayedDeletionTimer);
  297. }
  298. SAFE_DESTROY(FDelayedDeletionList);
  299. // sometimes we do not get DDEnd so the list is not released
  300. SAFE_DESTROY(FDDFileList);
  301. DebugAssert(FSynchronizeController == NULL);
  302. CloseHandle(FDDExtMutex);
  303. FDDExtMutex = NULL;
  304. FreeLibrary(FOle32Library);
  305. FOle32Library = NULL;
  306. FDragMoveCursor = NULL;
  307. DebugAssert(!FErrorList);
  308. if (FEverShown)
  309. {
  310. // when window is never shown (like when running command-line operation),
  311. // particularly window site is not restored correctly (BoundsRect value set
  312. // in RestoreForm gets lost during handle allocation), so we do not want
  313. // it to be stored
  314. StoreParams();
  315. }
  316. Terminal = NULL;
  317. Queue = NULL;
  318. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  319. NonVisualDataModule->ScpExplorer = NULL;
  320. delete FQueueController;
  321. FQueueController = NULL;
  322. delete FQueueStatusSection;
  323. FQueueStatusSection = NULL;
  324. delete FQueueStatus;
  325. FQueueStatus = NULL;
  326. delete FUserActionTimer;
  327. FUserActionTimer = NULL;
  328. delete FNoteTimer;
  329. delete FNotes;
  330. delete FNoteData;
  331. SAFE_DESTROY(FDocks);
  332. SessionsPageControl->Images = NULL;
  333. // only after clearing Terminal (after DoTerminalListChanged => ... => AddSessionColor is called)
  334. SAFE_DESTROY(FSessionColors);
  335. SAFE_DESTROY(FSessionsDragDropFilesEx);
  336. SAFE_DESTROY(FHistoryMenu[0][0]);
  337. SAFE_DESTROY(FHistoryMenu[0][1]);
  338. SAFE_DESTROY(FHistoryMenu[1][0]);
  339. SAFE_DESTROY(FHistoryMenu[1][1]);
  340. if (FHiddenWindow != NULL)
  341. {
  342. DestroyWindow(FHiddenWindow);
  343. FHiddenWindow = NULL;
  344. }
  345. }
  346. //---------------------------------------------------------------------------
  347. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  348. {
  349. std::unique_ptr<TSessionData> Data;
  350. if (!Session.IsEmpty())
  351. {
  352. bool DefaultsOnly;
  353. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  354. }
  355. TTerminalManager * Manager = TTerminalManager::Instance();
  356. for (int Index = 0; Index < Manager->Count; Index++)
  357. {
  358. TTerminal * Terminal = Manager->Terminals[Index];
  359. if (Session.IsEmpty() ||
  360. Terminal->SessionData->IsSameSite(Data.get()))
  361. {
  362. if (Path.IsEmpty())
  363. {
  364. Terminal->ClearCaches();
  365. }
  366. else
  367. {
  368. Terminal->DirectoryModified(Path, true);
  369. }
  370. }
  371. }
  372. // We should flag a pending refresh for the background terminals or busy foreground terminals
  373. if ((Terminal != NULL) && Terminal->Active &&
  374. CanCommandLineFromAnotherInstance() &&
  375. (Session.IsEmpty() || Terminal->SessionData->IsSameSite(Data.get())) &&
  376. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  377. {
  378. Terminal->ReloadDirectory();
  379. }
  380. }
  381. //---------------------------------------------------------------------------
  382. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  383. {
  384. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  385. size_t MessageSize = sizeof(TCopyDataMessage);
  386. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  387. if (Result)
  388. {
  389. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  390. Result = (Message.Version == TCopyDataMessage::Version1);
  391. if (Result)
  392. {
  393. switch (Message.Command)
  394. {
  395. case TCopyDataMessage::CommandCanCommandLine:
  396. Result = CanCommandLineFromAnotherInstance();
  397. break;
  398. case TCopyDataMessage::CommandCommandLine:
  399. {
  400. UnicodeString CommandLine(Message.CommandLine);
  401. Result = CommandLineFromAnotherInstance(CommandLine);
  402. }
  403. break;
  404. case TCopyDataMessage::RefreshPanel:
  405. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  406. break;
  407. case TCopyDataMessage::MainWindowCheck:
  408. Result = true;
  409. break;
  410. default:
  411. Result = false;
  412. break;
  413. }
  414. }
  415. }
  416. Message.Result = Result ? 1 : 0;
  417. }
  418. //---------------------------------------------------------------------------
  419. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  420. {
  421. WNDCLASS WindowClass = {0};
  422. WindowClass.lpfnWndProc = DefWindowProc;
  423. WindowClass.hInstance = HInstance;
  424. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  425. FHiddenWindow = NULL;
  426. if (RegisterClass(&WindowClass))
  427. {
  428. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  429. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  430. }
  431. }
  432. //---------------------------------------------------------------------------
  433. bool __fastcall TCustomScpExplorerForm::CanConsole()
  434. {
  435. return
  436. (Terminal != NULL) &&
  437. Terminal->Active &&
  438. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  439. }
  440. //---------------------------------------------------------------------------
  441. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  442. {
  443. bool Result = Visible && !NonVisualDataModule->Busy;
  444. return Result;
  445. }
  446. //---------------------------------------------------------------------------
  447. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  448. const UnicodeString & CommandLine)
  449. {
  450. TProgramParams Params(CommandLine);
  451. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  452. if (Result)
  453. {
  454. NonVisualDataModule->StartBusy();
  455. try
  456. {
  457. // this action is initiated from another process,
  458. // so it's likely that our window is not visible,
  459. // and user won't see what is going on
  460. Application->BringToFront();
  461. // reload sessions as we may be asked to open a session
  462. // just stored by another instance
  463. StoredSessions->Reload();
  464. UnicodeString SessionName = Params.Param[1];
  465. std::unique_ptr<TObjectList> DataList(new TObjectList());
  466. UnicodeString DownloadFile; // unused
  467. try
  468. {
  469. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this);
  470. }
  471. catch (EAbort &)
  472. {
  473. // Happens when opening session in PuTTY and in other situations,
  474. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  475. // to process the command-line on its own (or to pass it to yet another instance)
  476. }
  477. if (DataList->Count > 0)
  478. {
  479. TTerminalManager * Manager = TTerminalManager::Instance();
  480. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  481. }
  482. }
  483. __finally
  484. {
  485. NonVisualDataModule->EndBusy();
  486. }
  487. }
  488. return Result;
  489. }
  490. //---------------------------------------------------------------------------
  491. void __fastcall TCustomScpExplorerForm::SetTerminal(TTerminal * value)
  492. {
  493. if (FTerminal != value)
  494. {
  495. TerminalChanging();
  496. DoSetTerminal(value, false);
  497. }
  498. }
  499. //---------------------------------------------------------------------------
  500. void __fastcall TCustomScpExplorerForm::DoSetTerminal(TTerminal * value, bool Replace)
  501. {
  502. FTerminal = value;
  503. bool PrevAllowTransferPresetAutoSelect = FAllowTransferPresetAutoSelect;
  504. FAllowTransferPresetAutoSelect = false;
  505. try
  506. {
  507. TerminalChanged(Replace);
  508. }
  509. __finally
  510. {
  511. FAllowTransferPresetAutoSelect = PrevAllowTransferPresetAutoSelect;
  512. }
  513. if (Terminal != NULL)
  514. {
  515. TransferPresetAutoSelect();
  516. }
  517. }
  518. //---------------------------------------------------------------------------
  519. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TTerminal * value)
  520. {
  521. DoSetTerminal(value, true);
  522. }
  523. //---------------------------------------------------------------------------
  524. void __fastcall TCustomScpExplorerForm::TerminalChanging()
  525. {
  526. if (FTerminal != NULL)
  527. {
  528. UpdateTerminal(Terminal);
  529. }
  530. }
  531. //---------------------------------------------------------------------------
  532. void __fastcall TCustomScpExplorerForm::TerminalChanged(bool Replaced)
  533. {
  534. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  535. if (ManagedTerminal != NULL)
  536. {
  537. UpdateSessionColor((TColor)ManagedTerminal->StateData->Color);
  538. }
  539. DoTerminalListChanged();
  540. if (Replaced)
  541. {
  542. RemoteDirView->ReplaceTerminal(Terminal);
  543. }
  544. else
  545. {
  546. RemoteDirView->Terminal = Terminal;
  547. }
  548. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  549. if (Terminal != NULL)
  550. {
  551. if (Terminal->Active)
  552. {
  553. Terminal->RefreshDirectory();
  554. }
  555. DebugAssert(ManagedTerminal == Terminal);
  556. if (WinConfiguration->PreservePanelState)
  557. {
  558. if (ManagedTerminal->RemoteExplorerState != NULL)
  559. {
  560. RemoteDirView->RestoreState(ManagedTerminal->RemoteExplorerState);
  561. }
  562. else
  563. {
  564. RemoteDirView->ClearState();
  565. }
  566. }
  567. if (!Replaced)
  568. {
  569. InitStatusBar();
  570. }
  571. }
  572. UpdateTransferList();
  573. // Update panels Enable state before refreshing the labels
  574. UpdateControls();
  575. }
  576. //---------------------------------------------------------------------------
  577. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  578. {
  579. if (Queue != value)
  580. {
  581. if (FQueue != NULL)
  582. {
  583. FQueue->OnListUpdate = NULL;
  584. FQueue->OnQueueItemUpdate = NULL;
  585. }
  586. FQueue = value;
  587. if (FQueue != NULL)
  588. {
  589. DebugAssert(FQueue->OnListUpdate == NULL);
  590. FQueue->OnListUpdate = QueueListUpdate;
  591. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  592. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  593. }
  594. QueueChanged();
  595. }
  596. }
  597. //---------------------------------------------------------------------------
  598. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  599. TListItem * Item)
  600. {
  601. if (FQueueActedItem == Item)
  602. {
  603. FQueueActedItem = NULL;
  604. if ((QueueView3->PopupMenu != NULL) &&
  605. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  606. {
  607. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  608. ReleaseCapture();
  609. }
  610. }
  611. if (Item->Data == FPendingQueueActionItem)
  612. {
  613. FPendingQueueActionItem = NULL;
  614. }
  615. }
  616. //---------------------------------------------------------------------------
  617. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  618. {
  619. {
  620. TGuard Guard(FQueueStatusSection);
  621. FQueueStatusInvalidated = false;
  622. if (FQueue != NULL)
  623. {
  624. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  625. }
  626. }
  627. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  628. {
  629. FMaxQueueLength = FQueueStatus->Count;
  630. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  631. }
  632. FQueueController->UpdateQueueStatus(FQueueStatus);
  633. SetQueueProgress();
  634. UpdateQueueView();
  635. bool IsEmpty = (FQueueStatus == NULL) || (FQueueStatus->Count == 0);
  636. if (IsEmpty && (Terminal != NULL))
  637. {
  638. TOnceDoneOperation OnceDoneOperation =
  639. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  640. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  641. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  642. {
  643. FQueue->Enabled = false;
  644. }
  645. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  646. {
  647. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  648. }
  649. }
  650. }
  651. //---------------------------------------------------------------------------
  652. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  653. {
  654. UnicodeString Result;
  655. if (FQueueStatus != NULL)
  656. {
  657. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  658. {
  659. TFileOperationProgressType * ProgressData =
  660. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  661. if ((ProgressData != NULL) && ProgressData->InProgress)
  662. {
  663. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  664. }
  665. }
  666. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  667. {
  668. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  669. }
  670. }
  671. return Result;
  672. }
  673. //---------------------------------------------------------------------------
  674. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  675. {
  676. ComponentVisible[fcQueueView] =
  677. (WinConfiguration->QueueView.Show == qvShow) ||
  678. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  679. (FQueueStatus != NULL) && (FQueueStatus->Count > 0));
  680. }
  681. //---------------------------------------------------------------------------
  682. void __fastcall TCustomScpExplorerForm::QueueChanged()
  683. {
  684. if (FQueueStatus != NULL)
  685. {
  686. delete FQueueStatus;
  687. FQueueStatus = NULL;
  688. }
  689. UpdateQueueStatus(true);
  690. }
  691. //---------------------------------------------------------------------------
  692. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  693. {
  694. if (FQueue == Queue)
  695. {
  696. FQueueStatusInvalidated = true;
  697. }
  698. }
  699. //---------------------------------------------------------------------------
  700. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  701. TQueueItem * Item)
  702. {
  703. if (FQueue == Queue)
  704. {
  705. TGuard Guard(FQueueStatusSection);
  706. // this may be running in parallel with QueueChanged
  707. if (FQueueStatus != NULL)
  708. {
  709. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  710. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  711. {
  712. FRefreshLocalDirectory = (QueueItem == NULL) ||
  713. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  714. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  715. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  716. }
  717. if (QueueItem != NULL)
  718. {
  719. QueueItem->UserData = (void*)true;
  720. FQueueItemInvalidated = true;
  721. }
  722. }
  723. }
  724. }
  725. //---------------------------------------------------------------------------
  726. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  727. {
  728. // during standalone editing, we have no way to see/control queue,
  729. // so we have to always popup prompts automatically
  730. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  731. }
  732. //---------------------------------------------------------------------------
  733. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  734. {
  735. if (FQueueStatus != NULL)
  736. {
  737. bool QueueAutoPopup = IsQueueAutoPopup();
  738. bool NeedRefresh = FQueueController->NeedRefresh();
  739. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  740. FQueueItemInvalidated = false;
  741. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  742. bool Updated = false;
  743. TQueueItemProxy * QueueItem;
  744. bool WasUserAction;
  745. for (int Index = 0; Index < Limit; Index++)
  746. {
  747. bool Update = false;
  748. QueueItem = FQueueStatus->Items[Index];
  749. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  750. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  751. (FPendingQueueActionItem == NULL))
  752. {
  753. FPendingQueueActionItem = QueueItem;
  754. FUserActionTimer->Enabled = true;
  755. }
  756. if ((bool)QueueItem->UserData)
  757. {
  758. QueueItem->UserData = (void*)false;
  759. QueueItem->Update();
  760. Updated = true;
  761. Update = true;
  762. }
  763. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  764. {
  765. Update = true;
  766. }
  767. if (Update)
  768. {
  769. FQueueController->RefreshQueueItem(QueueItem);
  770. }
  771. }
  772. if (Updated)
  773. {
  774. NonVisualDataModule->UpdateNonVisibleActions();
  775. SetQueueProgress();
  776. }
  777. }
  778. }
  779. //---------------------------------------------------------------------------
  780. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  781. {
  782. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  783. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  784. }
  785. //---------------------------------------------------------------------------
  786. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  787. {
  788. if (ProgressData->Operation != foCalculateSize)
  789. {
  790. int OverallProgress;
  791. // FProgressForm is null when this is called from SetQueueProgress
  792. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  793. {
  794. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  795. }
  796. else
  797. {
  798. OverallProgress = ProgressData->OverallProgress();
  799. }
  800. // implies TBPF_NORMAL
  801. FTaskbarList->SetProgressValue(GetMainForm()->Handle, OverallProgress, 100);
  802. }
  803. else
  804. {
  805. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  806. }
  807. }
  808. //---------------------------------------------------------------------------
  809. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  810. {
  811. TTerminalManager::Instance()->QueueStatusUpdated();
  812. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  813. {
  814. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  815. {
  816. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  817. {
  818. TFileOperationProgressType * ProgressData = NULL;
  819. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  820. {
  821. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  822. }
  823. if ((ProgressData != NULL) &&
  824. ProgressData->InProgress)
  825. {
  826. SetTaskbarListProgressValue(ProgressData);
  827. }
  828. else
  829. {
  830. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  831. }
  832. }
  833. else
  834. {
  835. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  836. }
  837. }
  838. else
  839. {
  840. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  841. }
  842. }
  843. UpdateQueueLabel();
  844. }
  845. //---------------------------------------------------------------------------
  846. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  847. {
  848. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  849. if (FQueueStatus != NULL)
  850. {
  851. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  852. if (ActiveAndPendingPrimaryCount > 0)
  853. {
  854. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  855. }
  856. }
  857. QueueLabel->Caption = Caption;
  858. }
  859. //---------------------------------------------------------------------------
  860. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  861. {
  862. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  863. static_cast<TComponent*>(GetComponent(fcTransferList)));
  864. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  865. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  866. TransferList->Strings->BeginUpdate();
  867. try
  868. {
  869. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  870. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  871. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  872. if (FTransferDropDownHint.IsEmpty())
  873. {
  874. FTransferDropDownHint = TransferDropDown->Hint;
  875. }
  876. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  877. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  878. TransferDropDown->Text = StripHotkey(Name);
  879. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  880. (FTransferDropDownHint, StripHotkey(Name),
  881. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  882. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General))));
  883. // update the label, otherwise when it is updated only on the first draw
  884. // of the list, it is drawn "bold" for some reason
  885. FTransferListHoverIndex = TransferList->ItemIndex;
  886. UpdateTransferLabel();
  887. }
  888. __finally
  889. {
  890. TransferList->Strings->EndUpdate();
  891. }
  892. }
  893. //---------------------------------------------------------------------------
  894. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  895. {
  896. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  897. DebugAssert(Toolbar != NULL);
  898. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  899. }
  900. //---------------------------------------------------------------------------
  901. void __fastcall TCustomScpExplorerForm::UpdateActions()
  902. {
  903. TForm::UpdateActions();
  904. if (ComponentVisible[fcCustomCommandsBand])
  905. {
  906. UpdateCustomCommandsToolbar();
  907. }
  908. }
  909. //---------------------------------------------------------------------------
  910. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  911. {
  912. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  913. }
  914. //---------------------------------------------------------------------------
  915. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  916. {
  917. Color = GetBtnFaceColor();
  918. DebugAssert(Configuration && RemoteDirView);
  919. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  920. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  921. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  922. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  923. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  924. if (RemoteDirView->RowSelect != WinConfiguration->FullRowSelect)
  925. {
  926. RemoteDirView->RowSelect = WinConfiguration->FullRowSelect;
  927. // selection is not redrawn automatically when RowSelect changes
  928. RemoteDirView->Invalidate();
  929. }
  930. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  931. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  932. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  933. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  934. UpdateSessionsPageControlHeight();
  935. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  936. UpdateToolbarDisplayMode();
  937. UpdateTransferList();
  938. if (Terminal != NULL)
  939. {
  940. TransferPresetAutoSelect();
  941. }
  942. UpdateQueueView();
  943. UpdateControls();
  944. // this can be called even before constuctor finishes.
  945. if (FEditorManager != NULL)
  946. {
  947. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  948. }
  949. // this can be called even before constuctor finishes.
  950. if (FLocalEditors != NULL)
  951. {
  952. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  953. {
  954. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  955. }
  956. }
  957. if (ComponentVisible[fcCustomCommandsBand])
  958. {
  959. UpdateCustomCommandsToolbar();
  960. }
  961. // show only when keeping queue items forever,
  962. // otherwise, its enough to have in in the context menu
  963. QueueDeleteAllDoneQueueToolbarItem->Visible =
  964. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  965. if (FFileColorsCurrent != WinConfiguration->FileColors)
  966. {
  967. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  968. FFileColorsCurrent = WinConfiguration->FileColors;
  969. FileColorsChanged();
  970. }
  971. }
  972. //---------------------------------------------------------------------------
  973. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  974. {
  975. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  976. DirView->Invalidate();
  977. }
  978. //---------------------------------------------------------------------------
  979. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  980. {
  981. DoFileColorsChanged(RemoteDirView);
  982. }
  983. //---------------------------------------------------------------------------
  984. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  985. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  986. void * /*Arg*/)
  987. {
  988. if (Token != NULL)
  989. {
  990. TForm * Editor = dynamic_cast<TForm*>(Token);
  991. DebugAssert(Editor != NULL);
  992. ReconfigureEditorForm(Editor);
  993. }
  994. }
  995. //---------------------------------------------------------------------------
  996. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  997. {
  998. WinConfiguration->DDTransferConfirmation = asOn;
  999. }
  1000. //---------------------------------------------------------------------------
  1001. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  1002. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  1003. {
  1004. // noop
  1005. }
  1006. //---------------------------------------------------------------------------
  1007. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  1008. TTransferDirection Direction, TTransferType Type, bool Temp,
  1009. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1010. bool Confirm, bool DragDrop, int Options)
  1011. {
  1012. bool Result = true;
  1013. DebugAssert(Terminal && Terminal->Active);
  1014. // these parameters are known in advance
  1015. int Params =
  1016. FLAGMASK(Type == ttMove, cpDelete);
  1017. bool ToTemp = (Temp && (Direction == tdToLocal));
  1018. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1019. {
  1020. bool DisableNewerOnly =
  1021. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1022. ToTemp;
  1023. Options |=
  1024. FLAGMASK(ToTemp, coTemp) |
  1025. FLAGMASK(!Terminal->IsCapable[fsBackgroundTransfers], coDisableQueue) |
  1026. coDoNotShowAgain;
  1027. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1028. int CopyParamAttrs = (Direction == tdToRemote ?
  1029. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1030. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1031. int OutputOptions =
  1032. FLAGMASK(DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto),
  1033. cooDoNotShowAgain);
  1034. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1035. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1036. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1037. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions);
  1038. if (Result)
  1039. {
  1040. if (FLAGSET(OutputOptions, cooDoNotShowAgain))
  1041. {
  1042. if (DragDrop)
  1043. {
  1044. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1045. {
  1046. PopupTrayBalloon(NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation,
  1047. NULL, 0, EnableDDTransferConfirmation, NULL);
  1048. }
  1049. WinConfiguration->DDTransferConfirmation = asOff;
  1050. }
  1051. else
  1052. {
  1053. WinConfiguration->ConfirmTransferring = false;
  1054. }
  1055. }
  1056. else
  1057. {
  1058. // User explicitly unchecked "do not show again",
  1059. // so show him the dialog the next time
  1060. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1061. {
  1062. WinConfiguration->DDTransferConfirmation = asOn;
  1063. }
  1064. }
  1065. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1066. }
  1067. }
  1068. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fsBackgroundTransfers])
  1069. {
  1070. Configuration->Usage->Inc(L"TransfersOnBackground");
  1071. // these parameter are known only after transfer dialog
  1072. Params |=
  1073. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1074. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1075. Result = false;
  1076. ClearTransferSourceSelection(Direction);
  1077. }
  1078. return Result;
  1079. }
  1080. //---------------------------------------------------------------------------
  1081. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1082. {
  1083. if (FOnFileOperationFinished != NULL)
  1084. {
  1085. FOnFileOperationFinished(UnicodeString(), true);
  1086. }
  1087. else
  1088. {
  1089. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1090. if (HasDirView[Side])
  1091. {
  1092. DirView(Side)->SelectAll(smNone);
  1093. }
  1094. }
  1095. }
  1096. //---------------------------------------------------------------------------
  1097. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1098. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1099. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1100. int Params)
  1101. {
  1102. DebugAssert(Queue != NULL);
  1103. bool SingleFile = false;
  1104. if (FileList->Count == 1)
  1105. {
  1106. if (Direction == tdToRemote)
  1107. {
  1108. UnicodeString FileName = FileList->Strings[0];
  1109. SingleFile = FileExists(ApiPath(FileName));
  1110. }
  1111. else
  1112. {
  1113. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1114. SingleFile = !File->IsDirectory;
  1115. }
  1116. }
  1117. TQueueItem * QueueItem;
  1118. if (Direction == tdToRemote)
  1119. {
  1120. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1121. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1122. }
  1123. else
  1124. {
  1125. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1126. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1127. }
  1128. AddQueueItem(Queue, QueueItem, Terminal);
  1129. }
  1130. //---------------------------------------------------------------------------
  1131. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TTerminal * Terminal)
  1132. {
  1133. if (Queue->IsEmpty)
  1134. {
  1135. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  1136. ManagedTerminal->QueueOperationStart = Now();
  1137. }
  1138. Queue->AddItem(QueueItem);
  1139. }
  1140. //---------------------------------------------------------------------------
  1141. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1142. {
  1143. }
  1144. //---------------------------------------------------------------------------
  1145. void __fastcall TCustomScpExplorerForm::InitControls()
  1146. {
  1147. DebugAssert(FDocks != NULL);
  1148. for (int Index = 0; Index < ComponentCount; Index++)
  1149. {
  1150. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1151. if ((Dock != NULL) && (Dock->Tag == 0))
  1152. {
  1153. FDocks->Add(Dock);
  1154. }
  1155. }
  1156. CollectItemsWithTextDisplayMode(this);
  1157. }
  1158. //---------------------------------------------------------------------------
  1159. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1160. {
  1161. ConfigurationChanged();
  1162. QueuePanel->Height =
  1163. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1164. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1165. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1166. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1167. }
  1168. //---------------------------------------------------------------------------
  1169. void __fastcall TCustomScpExplorerForm::StoreParams()
  1170. {
  1171. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1172. WinConfiguration->QueueView.HeightPixelsPerInch = GetControlPixelsPerInch(this);
  1173. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1174. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1175. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1176. }
  1177. //---------------------------------------------------------------------------
  1178. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1179. {
  1180. if (!FFormRestored)
  1181. {
  1182. FFormRestored = true;
  1183. RestoreFormParams();
  1184. }
  1185. TForm::CreateParams(Params);
  1186. }
  1187. //---------------------------------------------------------------------------
  1188. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1189. {
  1190. DebugAssert(FDocks != NULL);
  1191. for (int Index = 0; Index < FDocks->Count; Index++)
  1192. {
  1193. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1194. }
  1195. }
  1196. //---------------------------------------------------------------------------
  1197. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1198. {
  1199. SetDockAllowDrag(true);
  1200. while (!ButtonsStr.IsEmpty())
  1201. {
  1202. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1203. UnicodeString Name = CutToChar(S, L'=', true);
  1204. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1205. {
  1206. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1207. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1208. {
  1209. while (!S.IsEmpty())
  1210. {
  1211. UnicodeString S2 = CutToChar(S, L',', true);
  1212. UnicodeString Name = CutToChar(S2, L':', true);
  1213. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1214. {
  1215. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1216. if (SameText(GetToolbarItemName(Item), Name))
  1217. {
  1218. // forward compatibility
  1219. UnicodeString S3 = CutToChar(S2, L':', true);
  1220. bool Visible = StrToIntDef(S3, 0);
  1221. Item->Visible = Visible;
  1222. }
  1223. }
  1224. }
  1225. }
  1226. }
  1227. }
  1228. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1229. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1230. }
  1231. //---------------------------------------------------------------------------
  1232. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1233. {
  1234. UnicodeString Result;
  1235. SetDockAllowDrag(true);
  1236. Result = ::GetToolbarsLayoutStr(this);
  1237. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1238. return Result;
  1239. }
  1240. //---------------------------------------------------------------------------
  1241. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1242. {
  1243. UnicodeString Result;
  1244. if (Item->Action != NULL)
  1245. {
  1246. Result = Item->Action->Name;
  1247. Result = RemoveSuffix(Result, L"Action");
  1248. }
  1249. else
  1250. {
  1251. Result = Item->Name;
  1252. Result = RemoveSuffix(Result, L"SubmenuItem");
  1253. Result = RemoveSuffix(Result, L"Item");
  1254. }
  1255. return Result;
  1256. }
  1257. //---------------------------------------------------------------------------
  1258. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1259. {
  1260. UnicodeString Result;
  1261. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1262. {
  1263. UnicodeString ToolbarStr;
  1264. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1265. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1266. {
  1267. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1268. {
  1269. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1270. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1271. // Once we introduce any buttons that aer hidden by default, we would have to remember their initial state
  1272. // and save the changes here only.
  1273. if (!Item->Visible)
  1274. {
  1275. UnicodeString Name = GetToolbarItemName(Item);
  1276. DebugAssert(Name.Pos(L"TBX") == 0);
  1277. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1278. }
  1279. }
  1280. }
  1281. if (!ToolbarStr.IsEmpty())
  1282. {
  1283. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1284. }
  1285. }
  1286. return Result;
  1287. }
  1288. //---------------------------------------------------------------------------
  1289. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1290. {
  1291. DebugAssert(FProgressForm == NULL);
  1292. bool AllowSkip = (Terminal != NULL) ? Terminal->IsCapable[fsSkipTransfer] : false;
  1293. FProgressForm = new TProgressForm(Application, (FTransferResumeList != NULL), AllowSkip, SynchronizeProgress);
  1294. FProgressForm->DeleteLocalToRecycleBin =
  1295. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1296. FProgressForm->DeleteRemoteToRecycleBin =
  1297. (Terminal != NULL) &&
  1298. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1299. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1300. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1301. LockWindow();
  1302. if (FTaskbarList != NULL)
  1303. {
  1304. // Actually, do not know what hides the progress once the operation finishes
  1305. SetTaskbarListProgressState(TBPF_NORMAL);
  1306. }
  1307. }
  1308. //---------------------------------------------------------------------------
  1309. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1310. {
  1311. UnlockWindow();
  1312. SAFE_DESTROY(FProgressForm);
  1313. SetQueueProgress();
  1314. }
  1315. //---------------------------------------------------------------------------
  1316. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1317. TFileOperationProgressType & ProgressData)
  1318. {
  1319. // Download to temporary local directory
  1320. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1321. {
  1322. bool Continue = true;
  1323. FPendingTempSpaceWarn = false;
  1324. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1325. if (!Continue)
  1326. {
  1327. Abort();
  1328. }
  1329. }
  1330. // operation is being executed and we still didn't show up progress form
  1331. if (ProgressData.InProgress && !FProgressForm)
  1332. {
  1333. CreateProgressForm(NULL);
  1334. // When main window is hidden, synchronisation form does not exist (we do not get here during synchronization - only comparing),
  1335. // we suppose "/upload" or URL download mode
  1336. if (!Visible && (ProgressData.Operation != foCalculateSize) &&
  1337. (ProgressData.Operation != foCalculateChecksum) &&
  1338. // Hack to prevent renames from DoSynchronizeMove (synchronize checklist) closing the connection
  1339. (ProgressData.Operation != foRemoteMove) &&
  1340. (FSynchronizeProgressForm == NULL) &&
  1341. !FStandaloneEditing)
  1342. {
  1343. FProgressForm->OnceDoneOperation = odoDisconnect;
  1344. }
  1345. }
  1346. // operation is finished (or terminated), so we hide progress form
  1347. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1348. {
  1349. DestroyProgressForm();
  1350. if (ProgressData.Operation == foCalculateSize)
  1351. {
  1352. // When calculating size before transfer, the abort caused by
  1353. // cancel flag set due to "MoveToQueue" below,
  1354. // is not propagated back to us in ExecuteFileOperation,
  1355. // as it is silently swallowed.
  1356. // So we explicitly (re)throw it here.
  1357. if (FMoveToQueue)
  1358. {
  1359. Abort();
  1360. }
  1361. }
  1362. else
  1363. {
  1364. if ((ProgressData.Cancel == csContinue) &&
  1365. !FAutoOperation)
  1366. {
  1367. OperationComplete(ProgressData.StartTime);
  1368. }
  1369. }
  1370. }
  1371. if (FProgressForm &&
  1372. // Can happen only during synchronization, when ending one of the operations
  1373. ProgressData.InProgress)
  1374. {
  1375. FProgressForm->SetProgressData(ProgressData);
  1376. if (FTaskbarList != NULL)
  1377. {
  1378. DebugAssert(ProgressData.InProgress);
  1379. SetTaskbarListProgressValue(&ProgressData);
  1380. }
  1381. if (FProgressForm->Cancel > csContinue)
  1382. {
  1383. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1384. if (FProgressForm->Cancel == csCancelFile)
  1385. {
  1386. FProgressForm->ClearCancel();
  1387. }
  1388. // cancel cancels even the move
  1389. FMoveToQueue = false;
  1390. }
  1391. else if (FProgressForm->MoveToQueue)
  1392. {
  1393. FMoveToQueue = true;
  1394. ProgressData.SetCancelAtLeast(csCancel);
  1395. }
  1396. if ((FTransferResumeList != NULL) &&
  1397. ProgressData.InProgress &&
  1398. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1399. !ProgressData.FullFileName.IsEmpty())
  1400. {
  1401. if ((FTransferResumeList->Count == 0) ||
  1402. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1403. {
  1404. // note that we do not recognize directories from files here
  1405. FTransferResumeList->Add(ProgressData.FullFileName);
  1406. }
  1407. }
  1408. }
  1409. }
  1410. //---------------------------------------------------------------------------
  1411. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1412. const TDateTime & StartTime)
  1413. {
  1414. if (GUIConfiguration->BeepOnFinish &&
  1415. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1416. {
  1417. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1418. DWORD Sound;
  1419. if (!ExtractFileExt(BeepSound).IsEmpty())
  1420. {
  1421. Sound = SND_FILENAME;
  1422. }
  1423. else
  1424. {
  1425. Sound = SND_ALIAS;
  1426. }
  1427. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1428. }
  1429. }
  1430. //---------------------------------------------------------------------------
  1431. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1432. TFileOperationProgressType & ProgressData)
  1433. {
  1434. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1435. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1436. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1437. FileOperationProgress(ProgressData);
  1438. }
  1439. //---------------------------------------------------------------------------
  1440. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1441. {
  1442. UnicodeString Result;
  1443. if (FProgressForm != NULL)
  1444. {
  1445. Result = FProgressForm->ProgressStr();
  1446. }
  1447. return Result;
  1448. }
  1449. //---------------------------------------------------------------------------
  1450. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1451. bool DragDrop)
  1452. {
  1453. return (!DragDrop && (DropSourceControl == NULL)) ||
  1454. (DropSourceControl == RemoteDirView);
  1455. }
  1456. //---------------------------------------------------------------------------
  1457. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1458. TFileOperation Operation, TOperationSide Side,
  1459. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1460. TOnceDoneOperation & OnceDoneOperation)
  1461. {
  1462. if (!FAutoOperation)
  1463. {
  1464. // no selection on "/upload", form servers only as event handler
  1465. // (it is not displayed)
  1466. if (PanelOperation(Side, FDragDropOperation) &&
  1467. Visible && (Operation != foCalculateSize) &&
  1468. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1469. {
  1470. if (FOnFileOperationFinished != NULL)
  1471. {
  1472. FOnFileOperationFinished(FileName, Success);
  1473. }
  1474. else
  1475. {
  1476. TCustomDirView * DView = DirView(Side);
  1477. UnicodeString FileNameOnly = ExtractFileName(FileName, (Side == osRemote));
  1478. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1479. // this can happen when local drive is unplugged in the middle of the operation
  1480. if (Item != NULL)
  1481. {
  1482. if (Success) Item->Selected = false;
  1483. DView->MakeProgressVisible(Item);
  1484. }
  1485. }
  1486. }
  1487. if ((Operation == foCopy) || (Operation == foMove))
  1488. {
  1489. if (Side == osLocal)
  1490. {
  1491. Configuration->Usage->Inc(L"UploadedFiles");
  1492. }
  1493. else
  1494. {
  1495. Configuration->Usage->Inc(L"DownloadedFiles");
  1496. }
  1497. }
  1498. }
  1499. if (Success && (FSynchronizeController != NULL))
  1500. {
  1501. if (Operation == foCopy)
  1502. {
  1503. DebugAssert(Side == osLocal);
  1504. FSynchronizeController->LogOperation(soUpload, FileName);
  1505. }
  1506. else if (Operation == foDelete)
  1507. {
  1508. DebugAssert(Side == osRemote);
  1509. FSynchronizeController->LogOperation(soDelete, FileName);
  1510. }
  1511. }
  1512. if (FProgressForm)
  1513. {
  1514. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1515. }
  1516. }
  1517. //---------------------------------------------------------------------------
  1518. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1519. TFileOperation Operation, TOperationSide Side,
  1520. bool Temp, const UnicodeString & FileName, Boolean Success,
  1521. TOnceDoneOperation & OnceDoneOperation)
  1522. {
  1523. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1524. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1525. OnceDoneOperation);
  1526. }
  1527. //---------------------------------------------------------------------------
  1528. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1529. {
  1530. DebugAssert(GetSide(Side) == osRemote);
  1531. DebugUsedParam(Side);
  1532. return RemoteDirView;
  1533. }
  1534. //---------------------------------------------------------------------------
  1535. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1536. {
  1537. DebugAssert(GetSide(Side) == osRemote);
  1538. DebugUsedParam(Side);
  1539. return (Terminal != NULL);
  1540. }
  1541. //---------------------------------------------------------------------------
  1542. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1543. TOperationSide Side, int FilesOnly)
  1544. {
  1545. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1546. }
  1547. //---------------------------------------------------------------------------
  1548. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1549. TOperationSide Side, int FilesOnly)
  1550. {
  1551. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1552. }
  1553. //---------------------------------------------------------------------------
  1554. struct THistoryItemData
  1555. {
  1556. short int Side;
  1557. short int Index;
  1558. };
  1559. //---------------------------------------------------------------------------
  1560. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1561. {
  1562. DirView(Side)->HistoryGo(Index);
  1563. }
  1564. //---------------------------------------------------------------------------
  1565. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1566. {
  1567. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1568. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1569. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1570. }
  1571. //---------------------------------------------------------------------------
  1572. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1573. bool Back)
  1574. {
  1575. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1576. {
  1577. TCustomDirView * DView = DirView(Side);
  1578. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1579. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1580. if (ICount > 10)
  1581. {
  1582. ICount = 10;
  1583. }
  1584. Menu->Items->Clear();
  1585. THistoryItemData Data;
  1586. Data.Side = Side;
  1587. for (short int i = 1; i <= ICount; i++)
  1588. {
  1589. TTBCustomItem * Item = new TTBXItem(Menu);
  1590. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1591. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, (Side == osRemote));
  1592. Item->Hint = DView->HistoryPath[Data.Index];
  1593. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1594. Item->Tag = *reinterpret_cast<int*>(&Data);
  1595. Item->OnClick = HistoryItemClick;
  1596. Menu->Items->Add(Item);
  1597. }
  1598. }
  1599. }
  1600. //---------------------------------------------------------------------------
  1601. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1602. TOperationSide Side, bool Back)
  1603. {
  1604. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1605. {
  1606. // workaround
  1607. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1608. // operator new allocates memory in Pascal code, but calls inline
  1609. // contructor in C++, leading in problems, the function does
  1610. // both in Pascal code
  1611. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1612. UpdateHistoryMenu(Side, Back);
  1613. }
  1614. return FHistoryMenu[Side == osLocal][Back];
  1615. }
  1616. //---------------------------------------------------------------------------
  1617. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1618. TCustomDirView *Sender)
  1619. {
  1620. TOperationSide Side = (Sender == DirView(osRemote) ? osRemote : osLocal);
  1621. UpdateHistoryMenu(Side, true);
  1622. UpdateHistoryMenu(Side, false);
  1623. }
  1624. //---------------------------------------------------------------------------
  1625. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1626. {
  1627. // remote custom commands can be executed only if the server supports shell commands
  1628. // or have secondary shell
  1629. return (FTerminal != NULL) && (FTerminal->IsCapable[fcSecondaryShell] || FTerminal->IsCapable[fcShellAnyCommand]);
  1630. }
  1631. //---------------------------------------------------------------------------
  1632. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1633. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1634. {
  1635. int Result;
  1636. TFileCustomCommand RemoteCustomCommand;
  1637. TLocalCustomCommand LocalCustomCommand;
  1638. TFileCustomCommand * NonInteractiveCustomCommand =
  1639. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1640. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1641. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1642. if (FLAGCLEAR(Command.Params, ccLocal))
  1643. {
  1644. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1645. // custom command that does not operate with files can be executed anytime ...
  1646. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1647. {
  1648. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1649. {
  1650. // ... but do not show such command in remote file menu (TODO)
  1651. Result = AllowedState;
  1652. }
  1653. else
  1654. {
  1655. Result = -1;
  1656. }
  1657. }
  1658. else
  1659. {
  1660. if ((ListType == ccltAll) || (ListType == ccltFile))
  1661. {
  1662. Result = ((FCurrentSide == osRemote) && DirView(osRemote)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1663. }
  1664. else
  1665. {
  1666. Result = -1;
  1667. }
  1668. }
  1669. }
  1670. else
  1671. {
  1672. // custom command that does not operate with files can be executed anytime
  1673. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1674. {
  1675. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1676. }
  1677. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1678. {
  1679. if ((ListType == ccltAll) || (ListType == ccltFile))
  1680. {
  1681. // special case is "diff"-style command that can be executed over any side,
  1682. // if we have both sides
  1683. Result =
  1684. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1685. // directly (instead of EnableSelectedOperation) to pass
  1686. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1687. ((HasDirView[osLocal] && DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1688. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1689. }
  1690. else if (ListType == ccltBoth)
  1691. {
  1692. Result = 1;
  1693. }
  1694. else
  1695. {
  1696. Result = -1;
  1697. }
  1698. }
  1699. else
  1700. {
  1701. if ((ListType == ccltAll) || (ListType == ccltFile))
  1702. {
  1703. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1704. }
  1705. else
  1706. {
  1707. Result = -1;
  1708. }
  1709. }
  1710. }
  1711. return Result;
  1712. }
  1713. //---------------------------------------------------------------------------
  1714. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1715. TStrings * FileList, const TCustomCommandType & ACommand,
  1716. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1717. {
  1718. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1719. {
  1720. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1721. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1722. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1723. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1724. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1725. bool Capture =
  1726. FLAGSET(ACommand.Params, ccShowResults) ||
  1727. FLAGSET(ACommand.Params, ccCopyResults) ||
  1728. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1729. TCaptureOutputEvent OutputEvent = NULL;
  1730. DebugAssert(FCapturedLog == NULL);
  1731. if (Capture)
  1732. {
  1733. FCapturedLog = new TStringList();
  1734. OutputEvent = TerminalCaptureLog;
  1735. }
  1736. try
  1737. {
  1738. if (!RemoteCustomCommand.IsFileCommand(Command))
  1739. {
  1740. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1741. OutputEvent);
  1742. }
  1743. else
  1744. {
  1745. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1746. }
  1747. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1748. {
  1749. if (FLAGSET(ACommand.Params, ccCopyResults))
  1750. {
  1751. CopyToClipboard(FCapturedLog);
  1752. }
  1753. if (FLAGSET(ACommand.Params, ccShowResults))
  1754. {
  1755. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1756. }
  1757. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1758. {
  1759. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1760. }
  1761. }
  1762. }
  1763. __finally
  1764. {
  1765. SAFE_DESTROY(FCapturedLog);
  1766. }
  1767. }
  1768. }
  1769. //---------------------------------------------------------------------------
  1770. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1771. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1772. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1773. {
  1774. TStrings * LocalFileList = NULL;
  1775. TStrings * RemoteFileList = NULL;
  1776. TStrings * RemoteFileListFull = NULL;
  1777. try
  1778. {
  1779. if (LocalFileCommand)
  1780. {
  1781. if (ALocalFileList == NULL)
  1782. {
  1783. DebugAssert(HasDirView[osLocal]);
  1784. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1785. // directly (instead of EnableSelectedOperation) to pass
  1786. // false to FocusedFileOnlyWhenFocused
  1787. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1788. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1789. }
  1790. else
  1791. {
  1792. LocalFileList = ALocalFileList;
  1793. }
  1794. if (FileListCommand)
  1795. {
  1796. if (LocalFileList->Count != 1)
  1797. {
  1798. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1799. }
  1800. }
  1801. else
  1802. {
  1803. if ((LocalFileList->Count != 1) &&
  1804. (FileList->Count != 1) &&
  1805. (LocalFileList->Count != FileList->Count))
  1806. {
  1807. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1808. }
  1809. }
  1810. }
  1811. UnicodeString RootTempDir;
  1812. UnicodeString TempDir;
  1813. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1814. if (!RemoteFiles)
  1815. {
  1816. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1817. }
  1818. try
  1819. {
  1820. TDateTimes RemoteFileTimes;
  1821. if (RemoteFiles)
  1822. {
  1823. RemoteFileList = FileList;
  1824. }
  1825. else
  1826. {
  1827. RemoteFileList = new TStringList();
  1828. TMakeLocalFileListParams MakeFileListParam;
  1829. MakeFileListParam.FileList = RemoteFileList;
  1830. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1831. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1832. MakeFileListParam.Recursive =
  1833. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1834. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1835. if (!MakeFileListParam.Recursive)
  1836. {
  1837. RemoteFileTimes.clear();
  1838. RemoteFileListFull = new TStringList();
  1839. MakeFileListParam.FileList = RemoteFileListFull;
  1840. MakeFileListParam.Recursive = true;
  1841. MakeFileListParam.IncludeDirs = false;
  1842. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1843. }
  1844. else
  1845. {
  1846. RemoteFileListFull = RemoteFileList;
  1847. }
  1848. }
  1849. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1850. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1851. if (!NonBlocking)
  1852. {
  1853. int Count;
  1854. if (FileListCommand)
  1855. {
  1856. Count = 1;
  1857. }
  1858. else if (LocalFileCommand)
  1859. {
  1860. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  1861. }
  1862. else
  1863. {
  1864. Count = RemoteFileList->Count;
  1865. }
  1866. Progress.Start(foCustomCommand, osRemote, Count);
  1867. DebugAssert(FProgressForm != NULL);
  1868. FProgressForm->ReadOnly = true;
  1869. }
  1870. try
  1871. {
  1872. if (FileListCommand)
  1873. {
  1874. UnicodeString LocalFile;
  1875. // MakeFileList does not delimit filenames
  1876. UnicodeString FileList = MakeFileList(RemoteFileList);
  1877. if (LocalFileCommand)
  1878. {
  1879. DebugAssert(LocalFileList->Count == 1);
  1880. LocalFile = LocalFileList->Strings[0];
  1881. }
  1882. TLocalCustomCommand CustomCommand(Data,
  1883. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1884. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1885. if (NonBlocking)
  1886. {
  1887. DebugAssert(!POutput);
  1888. ExecuteShellChecked(ShellCommand);
  1889. }
  1890. else
  1891. {
  1892. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1893. }
  1894. }
  1895. else if (LocalFileCommand)
  1896. {
  1897. if (LocalFileList->Count == 1)
  1898. {
  1899. UnicodeString LocalFile = LocalFileList->Strings[0];
  1900. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1901. {
  1902. UnicodeString FileName = RemoteFileList->Strings[Index];
  1903. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1904. Progress.SetFile(FileNameForProgress);
  1905. TLocalCustomCommand CustomCommand(Data,
  1906. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  1907. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1908. TOnceDoneOperation OnceDoneOperation;
  1909. // Do not unselect anything, as it won't work with two panels anyway
  1910. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1911. }
  1912. }
  1913. else if (RemoteFileList->Count == 1)
  1914. {
  1915. UnicodeString FileName = RemoteFileList->Strings[0];
  1916. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1917. {
  1918. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1919. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1920. Progress.SetFile(LocalFileNameOnly);
  1921. TLocalCustomCommand CustomCommand(
  1922. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1923. FileName, LocalFileName, L"");
  1924. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1925. TOnceDoneOperation OnceDoneOperation;
  1926. // Do not unselect anything, as it won't work with two panels anyway
  1927. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1928. }
  1929. }
  1930. else
  1931. {
  1932. if (LocalFileList->Count != RemoteFileList->Count)
  1933. {
  1934. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  1935. }
  1936. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1937. {
  1938. UnicodeString FileName = RemoteFileList->Strings[Index];
  1939. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1940. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1941. Progress.SetFile(LocalFileNameOnly); // sic
  1942. TLocalCustomCommand CustomCommand(
  1943. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1944. FileName, LocalFileName, L"");
  1945. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1946. TOnceDoneOperation OnceDoneOperation;
  1947. // Do not unselect anything, as it won't work with two panels anyway
  1948. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1949. }
  1950. }
  1951. }
  1952. else
  1953. {
  1954. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1955. {
  1956. UnicodeString FileName = RemoteFileList->Strings[Index];
  1957. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1958. Progress.SetFile(FileNameForProgress);
  1959. TLocalCustomCommand CustomCommand(Data,
  1960. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1961. FileName, L"", L"");
  1962. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1963. TOnceDoneOperation OnceDoneOperation;
  1964. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  1965. }
  1966. }
  1967. }
  1968. __finally
  1969. {
  1970. if (!NonBlocking)
  1971. {
  1972. Progress.Stop();
  1973. }
  1974. }
  1975. DebugAssert(!FAutoOperation);
  1976. if (!RemoteFiles)
  1977. {
  1978. TempDir = IncludeTrailingBackslash(TempDir);
  1979. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  1980. {
  1981. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  1982. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  1983. // Skip directories, process only nested files.
  1984. // The check is redundant as FileAge fails for directories anyway.
  1985. !DirectoryExists(FileName))
  1986. {
  1987. UnicodeString RemoteDir =
  1988. UnixExtractFileDir(
  1989. UnixIncludeTrailingBackslash(FTerminal->CurrentDirectory) +
  1990. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  1991. TDateTime NewTime;
  1992. if (FileAge(FileName, NewTime) &&
  1993. (NewTime != RemoteFileTimes[Index]))
  1994. {
  1995. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  1996. TemporaryFileCopyParam(CopyParam);
  1997. CopyParam.FileMask = L"";
  1998. FAutoOperation = true;
  1999. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2000. TemporaryFilesList->Add(FileName);
  2001. FTerminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2002. }
  2003. }
  2004. }
  2005. }
  2006. }
  2007. __finally
  2008. {
  2009. FAutoOperation = false;
  2010. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2011. {
  2012. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2013. }
  2014. }
  2015. }
  2016. __finally
  2017. {
  2018. if (RemoteFileList != FileList)
  2019. {
  2020. delete RemoteFileList;
  2021. }
  2022. if (LocalFileList != ALocalFileList)
  2023. {
  2024. delete LocalFileList;
  2025. }
  2026. if (RemoteFileListFull != RemoteFileList)
  2027. {
  2028. delete RemoteFileListFull;
  2029. }
  2030. }
  2031. }
  2032. //---------------------------------------------------------------------------
  2033. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2034. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2035. bool FileListCommand, UnicodeString * POutput)
  2036. {
  2037. std::unique_ptr<TStrings> SelectedFileList(DirView(osLocal)->CreateFileList(false, true, NULL));
  2038. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2039. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2040. {
  2041. UnicodeString FileName = SelectedFileList->Strings[Index];
  2042. if (DirectoryExists(FileName))
  2043. {
  2044. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2045. {
  2046. LocalFileList->Add(FileName);
  2047. }
  2048. if (FLAGSET(ACommand.Params, ccRecursive))
  2049. {
  2050. TMakeLocalFileListParams MakeFileListParam;
  2051. MakeFileListParam.FileList = LocalFileList.get();
  2052. MakeFileListParam.FileTimes = NULL;
  2053. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2054. MakeFileListParam.Recursive = true;
  2055. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2056. }
  2057. }
  2058. else
  2059. {
  2060. LocalFileList->Add(FileName);
  2061. }
  2062. }
  2063. if (FileListCommand)
  2064. {
  2065. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2066. TLocalCustomCommand CustomCommand(
  2067. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2068. L"", L"", FileList);
  2069. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2070. }
  2071. else
  2072. {
  2073. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2074. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2075. DebugAssert(FProgressForm != NULL);
  2076. FProgressForm->ReadOnly = true;
  2077. try
  2078. {
  2079. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2080. {
  2081. UnicodeString FileName = LocalFileList->Strings[Index];
  2082. Progress.SetFile(FileName);
  2083. TLocalCustomCommand CustomCommand(
  2084. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2085. FileName, L"", L"");
  2086. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2087. TOnceDoneOperation OnceDoneOperation;
  2088. Progress.Finish(FileName, true, OnceDoneOperation);
  2089. }
  2090. }
  2091. __finally
  2092. {
  2093. Progress.Stop();
  2094. }
  2095. }
  2096. }
  2097. //---------------------------------------------------------------------------
  2098. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2099. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2100. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2101. {
  2102. TLocalCustomCommand LocalCustomCommand(Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory());
  2103. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2104. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2105. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2106. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2107. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2108. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2109. std::unique_ptr<UnicodeString> POutput;
  2110. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2111. FLAGSET(ACommand.Params, ccCopyResults))
  2112. {
  2113. POutput.reset(new UnicodeString());
  2114. }
  2115. if (!LocalCustomCommand.IsFileCommand(Command))
  2116. {
  2117. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2118. }
  2119. // remote files?
  2120. else if ((FCurrentSide == osRemote) || LocalFileCommand)
  2121. {
  2122. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2123. }
  2124. // local files
  2125. else
  2126. {
  2127. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2128. }
  2129. if (POutput.get() != NULL)
  2130. {
  2131. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2132. int P = POutput->Pos(sLineBreak);
  2133. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2134. {
  2135. POutput->SetLength(P - 1);
  2136. }
  2137. // Copy even empty output to clipboard
  2138. if (FLAGSET(ACommand.Params, ccCopyResults))
  2139. {
  2140. CopyToClipboard(*POutput);
  2141. }
  2142. // But do not show an empty message box.
  2143. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2144. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2145. !POutput->IsEmpty())
  2146. {
  2147. TClipboardHandler ClipboardHandler;
  2148. ClipboardHandler.Text = *POutput;
  2149. TMessageParams Params;
  2150. TQueryButtonAlias Aliases[1];
  2151. Aliases[0].Button = qaRetry;
  2152. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2153. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2154. Params.Aliases = Aliases;
  2155. Params.AliasesCount = LENOF(Aliases);
  2156. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2157. }
  2158. }
  2159. }
  2160. //---------------------------------------------------------------------------
  2161. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2162. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2163. {
  2164. TCustomCommandData Data(Terminal);
  2165. UnicodeString Site = Terminal->SessionData->SessionKey;
  2166. UnicodeString HelpKeyword = ACommand.HomePage;
  2167. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2168. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2169. {
  2170. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2171. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2172. {
  2173. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, Terminal->CurrentDirectory));
  2174. }
  2175. else
  2176. {
  2177. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory()));
  2178. }
  2179. if (!DoCustomCommandOptionsDialog(
  2180. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2181. {
  2182. Abort();
  2183. }
  2184. }
  2185. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2186. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2187. {
  2188. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2189. }
  2190. else
  2191. {
  2192. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2193. }
  2194. }
  2195. //---------------------------------------------------------------------------
  2196. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2197. const TCustomCommandType & Command)
  2198. {
  2199. DebugAssert(FCustomCommandLocalFileList != NULL);
  2200. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2201. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2202. TStrings * LocalFileList = new TStringList();
  2203. TStrings * RemoteFileList = new TStringList();
  2204. try
  2205. {
  2206. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2207. {
  2208. LocalFileList->Clear();
  2209. LocalFileList->AddObject(
  2210. FCustomCommandLocalFileList->Strings[Index],
  2211. FCustomCommandLocalFileList->Objects[Index]);
  2212. RemoteFileList->Clear();
  2213. RemoteFileList->AddObject(
  2214. FCustomCommandRemoteFileList->Strings[Index],
  2215. FCustomCommandRemoteFileList->Objects[Index]);
  2216. CustomCommand(RemoteFileList, Command, LocalFileList);
  2217. }
  2218. }
  2219. __finally
  2220. {
  2221. delete LocalFileList;
  2222. delete RemoteFileList;
  2223. }
  2224. }
  2225. //---------------------------------------------------------------------------
  2226. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2227. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2228. {
  2229. delete FCustomCommandLocalFileList;
  2230. delete FCustomCommandRemoteFileList;
  2231. // takeover ownership,
  2232. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2233. FCustomCommandLocalFileList = LocalFileList;
  2234. FCustomCommandRemoteFileList = RemoteFileList;
  2235. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2236. if (Button != NULL)
  2237. {
  2238. FCustomCommandMenu->Items->Clear();
  2239. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth);
  2240. MenuPopup(FCustomCommandMenu, Button);
  2241. }
  2242. else
  2243. {
  2244. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2245. }
  2246. }
  2247. //---------------------------------------------------------------------------
  2248. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2249. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2250. {
  2251. DebugAssert(FCapturedLog != NULL);
  2252. if ((OutputType == cotOutput) || (OutputType == cotError))
  2253. {
  2254. FCapturedLog->Add(AddedLine);
  2255. }
  2256. }
  2257. //---------------------------------------------------------------------------
  2258. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2259. TOperationSide Side)
  2260. {
  2261. return (Side == osRemote) &&
  2262. ((Control == RemoteDirView) || (Control == RemoteDriveView));
  2263. }
  2264. //---------------------------------------------------------------------------
  2265. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2266. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2267. {
  2268. TTBItemOptions O;
  2269. O = Item->Options;
  2270. TCustomDirView * DView = DirView(Side);
  2271. if ((DView->ItemFocused != NULL) &&
  2272. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2273. // when resolving links is disabled, default action is to enter the directory,
  2274. // no matter what DoubleClickAction is configured to
  2275. ((Side != osRemote) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2276. // Can only Edit files, but can Open/Copy even directories
  2277. ((DoubleClickAction != dcaEdit) ||
  2278. !DView->ItemIsDirectory(DView->ItemFocused)))
  2279. {
  2280. Item->Options = O << tboDefault;
  2281. }
  2282. else
  2283. {
  2284. Item->Options = O >> tboDefault;
  2285. }
  2286. }
  2287. //---------------------------------------------------------------------------
  2288. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2289. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2290. {
  2291. TCustomDirView * DView = DirView(Side);
  2292. TListItem * Item = DView->ItemFocused;
  2293. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2294. EnableFocusedOperation[Side])
  2295. {
  2296. TPoint ClientPoint;
  2297. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2298. TPoint(0, 0) : MousePos;
  2299. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2300. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2301. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2302. }
  2303. }
  2304. //---------------------------------------------------------------------------
  2305. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2306. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2307. {
  2308. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2309. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2310. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2311. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2312. Handled = true;
  2313. }
  2314. //---------------------------------------------------------------------------
  2315. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2316. {
  2317. }
  2318. //---------------------------------------------------------------------------
  2319. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2320. {
  2321. DebugAssert(FErrorList == NULL);
  2322. if (WinConfiguration->ContinueOnError)
  2323. {
  2324. FErrorList = new TStringList();
  2325. Configuration->Usage->Inc(L"ContinuationsOnError");
  2326. }
  2327. NonVisualDataModule->StartBusy();
  2328. }
  2329. //---------------------------------------------------------------------------
  2330. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2331. {
  2332. NonVisualDataModule->EndBusy();
  2333. if (FErrorList)
  2334. {
  2335. HandleErrorList(FErrorList);
  2336. }
  2337. }
  2338. //---------------------------------------------------------------------------
  2339. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2340. const TCopyParamType & CopyParam)
  2341. {
  2342. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2343. {
  2344. Configuration->Usage->Inc(L"FileMaskUses");
  2345. }
  2346. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2347. {
  2348. Configuration->Usage->Inc(L"OperationMaskUses");
  2349. }
  2350. }
  2351. //---------------------------------------------------------------------------
  2352. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2353. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2354. {
  2355. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2356. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2357. TTransferOperationParam DefaultParam;
  2358. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2359. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2360. switch (Param.Queue)
  2361. {
  2362. case asOn:
  2363. CopyParam.Queue = true;
  2364. break;
  2365. case asOff:
  2366. CopyParam.Queue = false;
  2367. break;
  2368. case asAuto:
  2369. default:
  2370. // keep default
  2371. break;
  2372. }
  2373. bool Result =
  2374. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2375. if (Result)
  2376. {
  2377. DebugAssert(Terminal);
  2378. bool SelectionRestored = false;
  2379. TCustomDirView * DView = NULL;
  2380. if (HasDirView[Side])
  2381. {
  2382. DView = DirView(Side);
  2383. DView->SaveSelection();
  2384. DView->SaveSelectedNames();
  2385. }
  2386. UpdateCopyParamCounters(CopyParam);
  2387. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2388. DebugAssert(FTransferResumeList == NULL);
  2389. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2390. FMoveToQueue = false;
  2391. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2392. try
  2393. {
  2394. TStrings * PermanentFileList;
  2395. std::unique_ptr<TStrings> PermanentFileListOwner;
  2396. try
  2397. {
  2398. if (Side == osLocal)
  2399. {
  2400. PermanentFileList = FileList;
  2401. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2402. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2403. if (Operation == foMove)
  2404. {
  2405. ReloadLocalDirectory();
  2406. if (DView != NULL)
  2407. {
  2408. DView->RestoreSelection();
  2409. }
  2410. SelectionRestored = true;
  2411. }
  2412. }
  2413. else
  2414. {
  2415. // Clone the file list as it may refer to current directory files,
  2416. // which get destroyed, when the source directory is reloaded after foMove operation.
  2417. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2418. // But for a hotfix, we are not going to do such a big change.
  2419. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2420. PermanentFileList = PermanentFileListOwner.get();
  2421. try
  2422. {
  2423. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2424. }
  2425. __finally
  2426. {
  2427. if (Operation == foMove)
  2428. {
  2429. if (DView != NULL)
  2430. {
  2431. DView->RestoreSelection();
  2432. }
  2433. SelectionRestored = true;
  2434. }
  2435. ReloadLocalDirectory(Param.TargetDirectory);
  2436. }
  2437. }
  2438. }
  2439. catch (EAbort &)
  2440. {
  2441. if (FMoveToQueue)
  2442. {
  2443. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2444. DebugAssert(CopyParam.TransferSkipList == NULL);
  2445. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2446. if (TransferResumeList->Count > 0)
  2447. {
  2448. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2449. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2450. }
  2451. CopyParam.TransferSkipList = TransferResumeList.release();
  2452. // not really needed, just to keep it consistent with TransferResumeList
  2453. FTransferResumeList = NULL;
  2454. FMoveToQueue = false;
  2455. Configuration->Usage->Inc("MovesToBackground");
  2456. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2457. ClearTransferSourceSelection(Direction);
  2458. }
  2459. throw;
  2460. }
  2461. }
  2462. __finally
  2463. {
  2464. if (!SelectionRestored && (DView != NULL))
  2465. {
  2466. DView->DiscardSavedSelection();
  2467. }
  2468. FTransferResumeList = NULL;
  2469. }
  2470. }
  2471. return Result;
  2472. }
  2473. //---------------------------------------------------------------------------
  2474. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2475. TOperationSide Side, TStrings * FileList, void * Param)
  2476. {
  2477. DebugAssert(FileList->Count);
  2478. // We deliberately do not toggle alternative flag (Param), but use OR,
  2479. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2480. // shortcut of CurrentDeleteAlternativeAction
  2481. bool Alternative =
  2482. bool(Param) || UseAlternativeFunction();
  2483. bool Recycle;
  2484. if (Side == osLocal)
  2485. {
  2486. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2487. }
  2488. else
  2489. {
  2490. Recycle =
  2491. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2492. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2493. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2494. }
  2495. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2496. if (!Result)
  2497. {
  2498. UnicodeString Query;
  2499. if (FileList->Count == 1)
  2500. {
  2501. if (Side == osLocal)
  2502. {
  2503. Query = ExtractFileName(FileList->Strings[0]);
  2504. }
  2505. else
  2506. {
  2507. Query = UnixExtractFileName(FileList->Strings[0]);
  2508. }
  2509. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2510. }
  2511. else
  2512. {
  2513. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2514. }
  2515. TMessageParams Params(mpNeverAskAgainCheck);
  2516. Params.ImageName = L"Delete file";
  2517. unsigned int Answer =
  2518. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2519. if (Answer == qaNeverAskAgain)
  2520. {
  2521. Result = true;
  2522. if (Recycle)
  2523. {
  2524. WinConfiguration->ConfirmRecycling = false;
  2525. }
  2526. else
  2527. {
  2528. WinConfiguration->ConfirmDeleting = false;
  2529. }
  2530. }
  2531. else
  2532. {
  2533. Result = (Answer == qaOK);
  2534. }
  2535. }
  2536. if (Result)
  2537. {
  2538. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2539. }
  2540. return Result;
  2541. }
  2542. //---------------------------------------------------------------------------
  2543. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2544. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2545. {
  2546. TAutoBatch AutoBatch(this);
  2547. bool Result;
  2548. if ((Operation == foCopy) || (Operation == foMove))
  2549. {
  2550. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2551. }
  2552. else if (Operation == foRename)
  2553. {
  2554. DebugAssert(DirView(Side)->ItemFocused);
  2555. DirView(Side)->ItemFocused->EditCaption();
  2556. Result = true;
  2557. }
  2558. else if (Operation == foDelete)
  2559. {
  2560. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2561. }
  2562. else if (Operation == foSetProperties)
  2563. {
  2564. RemoteDirView->SaveSelectedNames();
  2565. Result = SetProperties(Side, FileList);
  2566. }
  2567. else if (Operation == foCustomCommand)
  2568. {
  2569. DebugAssert(Param);
  2570. DebugAssert(Side == osRemote);
  2571. RemoteDirView->SaveSelectedNames();
  2572. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2573. CustomCommand(FileList, *Command, NULL);
  2574. Result = true;
  2575. }
  2576. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2577. {
  2578. DebugAssert(Side == osRemote);
  2579. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2580. (Operation == foRemoteMove), reinterpret_cast<TTerminal *>(Param));
  2581. }
  2582. else if (Operation == foLock)
  2583. {
  2584. DebugAssert(Side == osRemote);
  2585. LockFiles(FileList, true);
  2586. Result = true;
  2587. }
  2588. else if (Operation == foUnlock)
  2589. {
  2590. DebugAssert(Side == osRemote);
  2591. LockFiles(FileList, false);
  2592. Result = true;
  2593. }
  2594. else
  2595. {
  2596. DebugFail();
  2597. }
  2598. return Result;
  2599. }
  2600. //---------------------------------------------------------------------------
  2601. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2602. {
  2603. if (Side == osCurrent)
  2604. {
  2605. Side = FCurrentSide;
  2606. }
  2607. return Side;
  2608. }
  2609. //---------------------------------------------------------------------------
  2610. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2611. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2612. {
  2613. Side = GetSide(Side);
  2614. bool Result;
  2615. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, (Side == osLocal), NULL);
  2616. try
  2617. {
  2618. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2619. }
  2620. __finally
  2621. {
  2622. delete FileList;
  2623. }
  2624. return Result;
  2625. }
  2626. //---------------------------------------------------------------------------
  2627. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2628. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2629. bool NoConfirmation, void * Param)
  2630. {
  2631. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2632. {
  2633. if ((Operation == foCopy) || (Operation == foMove))
  2634. {
  2635. if (GetSide(Side) == osLocal)
  2636. {
  2637. Configuration->Usage->Inc(L"UploadsCommand");
  2638. }
  2639. else
  2640. {
  2641. Configuration->Usage->Inc(L"DownloadsCommand");
  2642. }
  2643. }
  2644. }
  2645. }
  2646. //---------------------------------------------------------------------------
  2647. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2648. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2649. {
  2650. TTransferOperationParam Param;
  2651. if ((WinConfiguration->Interface != ifCommander) ||
  2652. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2653. {
  2654. Flags &= ~cocShortCutHint;
  2655. }
  2656. TCustomDirView * DView = DirView(Side);
  2657. Param.Options =
  2658. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2659. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2660. if (FLAGSET(Flags, cocQueue))
  2661. {
  2662. Param.Queue = asOn;
  2663. }
  2664. else if (FLAGSET(Flags, cocNonQueue))
  2665. {
  2666. Param.Queue = asOff;
  2667. }
  2668. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2669. }
  2670. //---------------------------------------------------------------------------
  2671. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2672. {
  2673. try
  2674. {
  2675. if (ErrorList->Count)
  2676. {
  2677. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2678. if (MessageDialog(Message, qtError,
  2679. qaOK | qaCancel, HELP_NONE) == qaOK)
  2680. {
  2681. unsigned int Answer;
  2682. int Index = 0;
  2683. do
  2684. {
  2685. DebugAssert(Index >= 0 && Index < ErrorList->Count);
  2686. TQueryButtonAlias Aliases[2];
  2687. Aliases[0].Button = qaYes;
  2688. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2689. Aliases[1].Button = qaNo;
  2690. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2691. TMessageParams Params;
  2692. Params.Aliases = Aliases;
  2693. Params.AliasesCount = LENOF(Aliases);
  2694. Answer = MoreMessageDialog(
  2695. FMTLOAD(ERROR_LIST_NUMBER, (Index+1, ErrorList->Count, ErrorList->Strings[Index])),
  2696. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2697. (Index ? qaYes : 0) | (Index < ErrorList->Count - 1 ? qaNo : 0) |
  2698. qaOK, HELP_NONE, &Params);
  2699. if (Answer == qaNo)
  2700. {
  2701. Index++;
  2702. }
  2703. if (Answer == qaYes)
  2704. {
  2705. Index--;
  2706. }
  2707. }
  2708. while (Answer != qaOK);
  2709. }
  2710. }
  2711. }
  2712. __finally
  2713. {
  2714. TStrings * List = ErrorList;
  2715. ErrorList = NULL;
  2716. for (int i = 0; i < List->Count; i++)
  2717. {
  2718. delete List->Objects[i];
  2719. }
  2720. delete List;
  2721. }
  2722. }
  2723. //---------------------------------------------------------------------------
  2724. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2725. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2726. {
  2727. // needed for checking filemasks, as there's no directory object
  2728. // associated with the file object
  2729. File->FullFileName = FullFileName;
  2730. TFileMasks::TParams MaskParams;
  2731. MaskParams.Size = File->Size;
  2732. MaskParams.Modification = File->Modification;
  2733. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2734. }
  2735. //---------------------------------------------------------------------------
  2736. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2737. {
  2738. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2739. Side = GetSide(Side);
  2740. TCustomDirView * DView = DirView(Side);
  2741. TListItem * FocusedItem = DView->ItemFocused;
  2742. UnicodeString Name;
  2743. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2744. {
  2745. Name = DView->ItemFileName(FocusedItem);
  2746. }
  2747. else
  2748. {
  2749. Name = LoadStr(NEW_FILE);
  2750. }
  2751. UnicodeString Names = Name;
  2752. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2753. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2754. HELP_EDIT_NEW, History.get(), true))
  2755. {
  2756. while (!Names.IsEmpty())
  2757. {
  2758. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2759. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2760. UnicodeString TargetFileName;
  2761. UnicodeString LocalFileName;
  2762. UnicodeString RootTempDir;
  2763. UnicodeString TempDir;
  2764. UnicodeString RemoteDirectory;
  2765. bool ExistingFile = false;
  2766. if (Side == osRemote)
  2767. {
  2768. Name = AbsolutePath(FTerminal->CurrentDirectory, Name);
  2769. TRemoteFile * File = NULL;
  2770. if (FTerminal->FileExists(Name, &File) &&
  2771. !File->IsDirectory)
  2772. {
  2773. try
  2774. {
  2775. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2776. ExistingFile = true;
  2777. }
  2778. __finally
  2779. {
  2780. delete File;
  2781. }
  2782. }
  2783. if (!ExistingFile)
  2784. {
  2785. RemoteDirectory = UnixExtractFilePath(Name);
  2786. TemporaryDirectoryForRemoteFiles(
  2787. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2788. TargetFileName = UnixExtractFileName(Name);
  2789. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2790. LocalFileName = TempDir +
  2791. // We probably do not want to trim the VMS version here
  2792. FTerminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2793. }
  2794. }
  2795. else
  2796. {
  2797. if (ExtractFilePath(Name).IsEmpty())
  2798. {
  2799. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2800. }
  2801. else
  2802. {
  2803. LocalFileName = ExpandFileName(Name);
  2804. }
  2805. TargetFileName = ExtractFileName(Name);
  2806. }
  2807. if (!ExistingFile)
  2808. {
  2809. if (!FileExists(ApiPath(LocalFileName)))
  2810. {
  2811. int File = FileCreate(ApiPath(LocalFileName));
  2812. if (File < 0)
  2813. {
  2814. if (!RootTempDir.IsEmpty())
  2815. {
  2816. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2817. }
  2818. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2819. }
  2820. else
  2821. {
  2822. FileClose(File);
  2823. }
  2824. }
  2825. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2826. const TEditorData * ExternalEditor = NULL;
  2827. TFileMasks::TParams MaskParams; // size not known
  2828. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2829. false, MaskParams);
  2830. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2831. ExternalEditor, RootTempDir, RemoteDirectory);
  2832. }
  2833. }
  2834. }
  2835. }
  2836. //---------------------------------------------------------------------------
  2837. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2838. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2839. {
  2840. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2841. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2842. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2843. return
  2844. ((ExecuteFileBy == efInternalEditor)) ||
  2845. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2846. }
  2847. //---------------------------------------------------------------------------
  2848. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2849. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2850. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2851. UnicodeString RemoteDirectory)
  2852. {
  2853. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2854. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2855. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2856. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2857. Side = GetSide(Side);
  2858. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2859. if (Side == osRemote)
  2860. {
  2861. Data->Terminal = Terminal;
  2862. Data->Queue = Queue;
  2863. Data->SessionData = CloneCurrentSessionData();
  2864. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2865. Data->RemoteDirectory = RemoteDirectory;
  2866. Data->SessionName = Terminal->SessionData->SessionName;
  2867. Data->LocalRootDirectory = LocalRootDirectory;
  2868. Data->OriginalFileName = OriginalFileName;
  2869. Data->Command = L""; // will be changed later for external editor
  2870. }
  2871. if (ExecuteFileBy == efInternalEditor)
  2872. {
  2873. if (Side == osRemote)
  2874. {
  2875. UnicodeString Caption = UnixIncludeTrailingBackslash(RemoteDirectory) + OriginalFileName +
  2876. L" - " + Terminal->SessionData->SessionName;
  2877. TForm * Editor;
  2878. try
  2879. {
  2880. TNotifyEvent OnFileChanged = NULL;
  2881. TNotifyEvent OnSaveAll = NULL;
  2882. TAnyModifiedEvent OnAnyModified = NULL;
  2883. // Edited files are uploaded in background queue, what we do not support with encrypted files
  2884. if (Terminal->IsCapable[fsBackgroundTransfers])
  2885. {
  2886. OnFileChanged = FEditorManager->FileChanged;
  2887. OnSaveAll = SaveAllInternalEditors;
  2888. OnAnyModified = AnyInternalEditorModified;
  2889. }
  2890. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  2891. FEditorManager->FileReload, FEditorManager->FileClosed,
  2892. OnSaveAll, OnAnyModified,
  2893. Caption, FStandaloneEditing, SessionColor, Terminal->SessionData->InternalEditorEncoding);
  2894. }
  2895. catch(...)
  2896. {
  2897. if (!LocalRootDirectory.IsEmpty())
  2898. {
  2899. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  2900. }
  2901. throw;
  2902. }
  2903. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  2904. }
  2905. else
  2906. {
  2907. DebugAssert(!FStandaloneEditing);
  2908. TForm * Editor =
  2909. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  2910. SaveAllInternalEditors, AnyInternalEditorModified,
  2911. L"", false, SessionColor, -1);
  2912. FLocalEditors->Add(Editor);
  2913. }
  2914. }
  2915. else
  2916. {
  2917. HANDLE Process;
  2918. if (ExecuteFileBy == efExternalEditor)
  2919. {
  2920. UnicodeString Program, Params, Dir;
  2921. Data->Command = ExternalEditor->ExternalEditor;
  2922. ReformatFileNameCommand(Data->Command);
  2923. SplitCommand(Data->Command, Program, Params, Dir);
  2924. Params = ExpandFileNameCommand(Params, FileName);
  2925. Program = ExpandEnvironmentVariables(Program);
  2926. if (!ExecuteShell(Program, Params, Process))
  2927. {
  2928. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  2929. }
  2930. }
  2931. else
  2932. {
  2933. DebugAssert(Side == osRemote);
  2934. if (!ExecuteShell(FileName, L"", Process))
  2935. {
  2936. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  2937. }
  2938. }
  2939. if ((Side == osLocal) ||
  2940. ((ExecuteFileBy == efShell) &&
  2941. !WinConfiguration->Editor.SDIShellEditor) ||
  2942. ((ExecuteFileBy == efExternalEditor) &&
  2943. !ExternalEditor->SDIExternalEditor))
  2944. {
  2945. // no need for handle
  2946. if (Process != NULL)
  2947. {
  2948. DebugCheck(CloseHandle(Process));
  2949. }
  2950. Process = INVALID_HANDLE_VALUE;
  2951. }
  2952. else
  2953. {
  2954. if (Process == NULL)
  2955. {
  2956. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  2957. }
  2958. }
  2959. if (Side == osRemote)
  2960. {
  2961. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  2962. }
  2963. }
  2964. }
  2965. //---------------------------------------------------------------------------
  2966. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  2967. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2968. void * /*Arg*/)
  2969. {
  2970. if (Token != NULL)
  2971. {
  2972. EditorFormFileSave(static_cast<TForm *>(Token));
  2973. }
  2974. }
  2975. //---------------------------------------------------------------------------
  2976. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  2977. {
  2978. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  2979. {
  2980. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  2981. }
  2982. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  2983. }
  2984. //---------------------------------------------------------------------------
  2985. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  2986. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2987. void * Arg)
  2988. {
  2989. if ((Token != NULL) &&
  2990. IsEditorFormModified(static_cast<TForm *>(Token)))
  2991. {
  2992. *static_cast<bool *>(Arg) = true;
  2993. }
  2994. }
  2995. //---------------------------------------------------------------------------
  2996. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  2997. TObject * /*Sender*/, bool & Modified)
  2998. {
  2999. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3000. {
  3001. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3002. {
  3003. Modified = true;
  3004. }
  3005. }
  3006. if (!Modified)
  3007. {
  3008. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3009. }
  3010. }
  3011. //---------------------------------------------------------------------------
  3012. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3013. {
  3014. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3015. }
  3016. //---------------------------------------------------------------------------
  3017. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3018. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3019. UnicodeString & Result, UnicodeString & RootDirectory)
  3020. {
  3021. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3022. {
  3023. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3024. Result = RootDirectory;
  3025. }
  3026. else
  3027. {
  3028. RootDirectory = L"";
  3029. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3030. Result = IncludeTrailingBackslash(Result);
  3031. }
  3032. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3033. {
  3034. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3035. }
  3036. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3037. {
  3038. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3039. {
  3040. RemoteDirectory.Delete(1, 1);
  3041. }
  3042. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3043. }
  3044. if (!ForceDirectories(ApiPath(Result)))
  3045. {
  3046. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3047. }
  3048. }
  3049. //---------------------------------------------------------------------------
  3050. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3051. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3052. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  3053. {
  3054. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3055. if (ForceText)
  3056. {
  3057. CopyParam.TransferMode = tmAscii;
  3058. }
  3059. TemporaryFileCopyParam(CopyParam);
  3060. if (AllFiles)
  3061. {
  3062. CopyParam.IncludeFileMask = TFileMasks();
  3063. }
  3064. if (TempDir.IsEmpty())
  3065. {
  3066. TemporaryDirectoryForRemoteFiles(FTerminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3067. }
  3068. DebugAssert(!FAutoOperation);
  3069. FAutoOperation = AutoOperation;
  3070. Terminal->ExceptionOnFail = true;
  3071. try
  3072. {
  3073. try
  3074. {
  3075. // turn off confirmations, as for MDI editors we may possibly download
  3076. // the same file over
  3077. Terminal->CopyToLocal(
  3078. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3079. if (GetTargetNames)
  3080. {
  3081. for (int i = 0; i < FileList->Count; i++)
  3082. {
  3083. FileList->Strings[i] =
  3084. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3085. }
  3086. }
  3087. }
  3088. catch(...)
  3089. {
  3090. if (!RootTempDir.IsEmpty())
  3091. {
  3092. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3093. }
  3094. throw;
  3095. }
  3096. }
  3097. __finally
  3098. {
  3099. FAutoOperation = false;
  3100. Terminal->ExceptionOnFail = false;
  3101. }
  3102. }
  3103. //---------------------------------------------------------------------------
  3104. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3105. {
  3106. // Do not waste time checking for default editor list the next time,
  3107. // or testing if default editor is not notepad.
  3108. bool TryNextTime = false;
  3109. UnicodeString UsageState;
  3110. if (!WinConfiguration->EditorList->IsDefaultList())
  3111. {
  3112. UsageState = "H";
  3113. }
  3114. else
  3115. {
  3116. UnicodeString Executable;
  3117. UnicodeString ExecutableDescription;
  3118. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3119. {
  3120. UnicodeString Message =
  3121. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3122. unsigned int Answer =
  3123. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3124. if (Answer != qaOK)
  3125. {
  3126. UsageState = "R";
  3127. }
  3128. else
  3129. {
  3130. UsageState = "A";
  3131. TEditorData EditorData;
  3132. EditorData.Editor = edExternal;
  3133. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3134. EditorData.DecideExternalEditorText();
  3135. TEditorList EditorList;
  3136. EditorList = *WinConfiguration->EditorList;
  3137. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3138. WinConfiguration->EditorList = &EditorList;
  3139. }
  3140. }
  3141. }
  3142. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3143. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3144. }
  3145. //---------------------------------------------------------------------------
  3146. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3147. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3148. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3149. {
  3150. if (ExecuteFileBy == efDefaultEditor)
  3151. {
  3152. if (!WinConfiguration->OfferedEditorAutoConfig)
  3153. {
  3154. EditorAutoConfig();
  3155. }
  3156. const TEditorPreferences * Editor =
  3157. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3158. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3159. {
  3160. ExecuteFileBy = efInternalEditor;
  3161. ExternalEditor = NULL;
  3162. }
  3163. else if (Editor->Data->Editor == edOpen)
  3164. {
  3165. ExecuteFileBy = efShell;
  3166. ExternalEditor = NULL;
  3167. }
  3168. else
  3169. {
  3170. ExecuteFileBy = efExternalEditor;
  3171. ExternalEditor = Editor->Data;
  3172. }
  3173. }
  3174. }
  3175. //---------------------------------------------------------------------------
  3176. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3177. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3178. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3179. {
  3180. UnicodeString OriginalFileName;
  3181. UnicodeString LocalRootDirectory;
  3182. UnicodeString RemoteDirectory;
  3183. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3184. (Side == osLocal), MaskParams);
  3185. UnicodeString Counter;
  3186. UnicodeString LocalFileName;
  3187. bool Handled = false;
  3188. if (Side == osRemote)
  3189. {
  3190. // We need to trim VMS version here, so that we use name without version
  3191. // when uploading back to create a new version of the file
  3192. OriginalFileName = FTerminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3193. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3194. TObject * Token = NULL;
  3195. UnicodeString LocalDirectory;
  3196. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3197. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3198. LocalDirectory))
  3199. {
  3200. if (Token != NULL)
  3201. {
  3202. TForm * Form = dynamic_cast<TForm *>(Token);
  3203. if (Form->WindowState == wsMinimized)
  3204. {
  3205. ShowWindow(Form->Handle, SW_RESTORE);
  3206. }
  3207. else
  3208. {
  3209. Form->SetFocus();
  3210. }
  3211. Handled = true;
  3212. }
  3213. else
  3214. {
  3215. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3216. }
  3217. }
  3218. if (!Handled)
  3219. {
  3220. TStringList * FileList1 = new TStringList();
  3221. try
  3222. {
  3223. FileList1->AddObject(FullFileName, Object);
  3224. TemporarilyDownloadFiles(FileList1,
  3225. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3226. LocalRootDirectory, LocalDirectory, true, true, true);
  3227. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3228. }
  3229. __finally
  3230. {
  3231. delete FileList1;
  3232. }
  3233. switch (ExecuteFileBy)
  3234. {
  3235. case efShell:
  3236. Counter = "RemoteFilesExecuted";
  3237. break;
  3238. case efInternalEditor:
  3239. Counter = "RemoteFilesOpenedInInternalEditor";
  3240. break;
  3241. case efExternalEditor:
  3242. Counter = "RemoteFilesOpenedInExternalEditor";
  3243. break;
  3244. default:
  3245. DebugFail();
  3246. }
  3247. }
  3248. }
  3249. else
  3250. {
  3251. LocalFileName = FullFileName;
  3252. OriginalFileName = ExtractFileName(FullFileName);
  3253. switch (ExecuteFileBy)
  3254. {
  3255. case efShell:
  3256. Counter = "LocalFilesExecuted";
  3257. break;
  3258. case efInternalEditor:
  3259. Counter = "LocalFilesOpenedInInternalEditor";
  3260. break;
  3261. case efExternalEditor:
  3262. Counter = "LocalFilesOpenedInExternalEditor";
  3263. break;
  3264. default:
  3265. DebugFail();
  3266. }
  3267. }
  3268. if (!Handled)
  3269. {
  3270. Configuration->Usage->Inc(Counter);
  3271. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3272. ExternalEditor, LocalRootDirectory, RemoteDirectory);
  3273. }
  3274. }
  3275. //---------------------------------------------------------------------------
  3276. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3277. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3278. bool AllSelected, bool OnFocused)
  3279. {
  3280. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3281. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3282. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3283. Side = GetSide(Side);
  3284. TCustomDirView * DView = DirView(Side);
  3285. TStrings * FileList = AllSelected ?
  3286. DView->CreateFileList(OnFocused, Side == osLocal) :
  3287. DView->CreateFocusedFileList(Side == osLocal);
  3288. try
  3289. {
  3290. DebugAssert(AllSelected || (FileList->Count == 1));
  3291. for (int i = 0; i < FileList->Count; i++)
  3292. {
  3293. UnicodeString ListFileName = FileList->Strings[i];
  3294. UnicodeString FileNameOnly = (Side == osRemote) ?
  3295. UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3296. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3297. if (!DView->ItemIsDirectory(Item))
  3298. {
  3299. UnicodeString FullFileName;
  3300. if (Side == osRemote)
  3301. {
  3302. FullFileName = RemoteDirView->Path + ListFileName;
  3303. }
  3304. else
  3305. {
  3306. FullFileName = ListFileName;
  3307. }
  3308. TFileMasks::TParams MaskParams;
  3309. MaskParams.Size = DView->ItemFileSize(Item);
  3310. TDateTimePrecision Precision;
  3311. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3312. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3313. FileList->Objects[i], MaskParams);
  3314. }
  3315. }
  3316. }
  3317. __finally
  3318. {
  3319. delete FileList;
  3320. }
  3321. }
  3322. //---------------------------------------------------------------------------
  3323. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3324. {
  3325. // do not forget to add additional options to TemporarilyDownloadFiles, and AS
  3326. CopyParam.FileNameCase = ncNoChange;
  3327. CopyParam.PreserveRights = false;
  3328. CopyParam.PreserveReadOnly = false;
  3329. CopyParam.ReplaceInvalidChars = true;
  3330. CopyParam.IncludeFileMask = TFileMasks();
  3331. CopyParam.NewerOnly = false;
  3332. CopyParam.FileMask = L"";
  3333. }
  3334. //---------------------------------------------------------------------------
  3335. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3336. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3337. {
  3338. TTerminalManager * Manager = TTerminalManager::Instance();
  3339. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3340. {
  3341. if (!NonVisualDataModule->Busy)
  3342. {
  3343. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3344. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3345. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3346. if (Data->Terminal == NULL)
  3347. {
  3348. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3349. if (SameSiteTerminal != NULL)
  3350. {
  3351. UnicodeString Message =
  3352. FMTLOAD(EDIT_SESSION_REATTACH,
  3353. (FileNameOnly, Data->SessionName, FileNameOnly));
  3354. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3355. {
  3356. Data->Terminal = Terminal;
  3357. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3358. Data->SessionName = Terminal->SessionData->SessionName;
  3359. // We might also overwrite session data
  3360. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3361. }
  3362. else
  3363. {
  3364. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3365. Abort();
  3366. }
  3367. }
  3368. }
  3369. // foreground session should reconnect itself
  3370. else if (Terminal != Data->Terminal)
  3371. {
  3372. UnicodeString Message =
  3373. MainInstructions(
  3374. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3375. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3376. {
  3377. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3378. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3379. }
  3380. else
  3381. {
  3382. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3383. Abort();
  3384. }
  3385. }
  3386. }
  3387. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3388. {
  3389. Configuration->Usage->Inc(L"EditInactiveSession");
  3390. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3391. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3392. (ExtractFileName(FileName), Data->SessionName)));
  3393. }
  3394. }
  3395. TStrings * FileList = new TStringList();
  3396. try
  3397. {
  3398. FileList->Add(FileName);
  3399. // consider using the same settings (preset) as when the file was downloaded
  3400. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3401. TemporaryFileCopyParam(CopyParam);
  3402. if (Data->ForceText)
  3403. {
  3404. CopyParam.TransferMode = tmAscii;
  3405. }
  3406. // so i do not need to worry if masking algorithm works in all cases
  3407. // ("" means "copy file name", no masking is actually done)
  3408. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3409. {
  3410. CopyParam.FileMask = L"";
  3411. }
  3412. else
  3413. {
  3414. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3415. }
  3416. DebugAssert(Data->Queue != NULL);
  3417. int Params = cpNoConfirmation | cpTemporary;
  3418. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3419. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3420. QueueItem->CompleteEvent = UploadCompleteEvent;
  3421. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3422. }
  3423. __finally
  3424. {
  3425. delete FileList;
  3426. }
  3427. }
  3428. //---------------------------------------------------------------------------
  3429. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3430. const UnicodeString FileName, const TEditedFileData * Data)
  3431. {
  3432. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3433. DebugAssert(!NonVisualDataModule->Busy);
  3434. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3435. {
  3436. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3437. (ExtractFileName(FileName), Data->SessionName)));
  3438. }
  3439. TTerminal * PrevTerminal = TTerminalManager::Instance()->ActiveTerminal;
  3440. TTerminalManager::Instance()->ActiveTerminal = Data->Terminal;
  3441. NonVisualDataModule->StartBusy();
  3442. try
  3443. {
  3444. std::unique_ptr<TRemoteFile> File;
  3445. UnicodeString RemoteFileName =
  3446. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3447. FTerminal->ExceptionOnFail = true;
  3448. try
  3449. {
  3450. TRemoteFile * AFile = NULL;
  3451. FTerminal->ReadFile(RemoteFileName, AFile);
  3452. File.reset(AFile);
  3453. if (!File->HaveFullFileName)
  3454. {
  3455. File->FullFileName = RemoteFileName;
  3456. }
  3457. }
  3458. __finally
  3459. {
  3460. FTerminal->ExceptionOnFail = false;
  3461. }
  3462. std::unique_ptr<TStrings> FileList(new TStringList());
  3463. FileList->AddObject(RemoteFileName, File.get());
  3464. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3465. UnicodeString TempDir = ExtractFilePath(FileName);
  3466. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3467. TempDir, true, true, true);
  3468. // sanity check, the target file name should be still the same
  3469. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3470. }
  3471. __finally
  3472. {
  3473. NonVisualDataModule->EndBusy();
  3474. // it actually may not exist anymore...
  3475. TTerminalManager::Instance()->ActiveTerminal = PrevTerminal;
  3476. }
  3477. }
  3478. //---------------------------------------------------------------------------
  3479. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3480. const TEditedFileData * Data, bool & KeepOpen)
  3481. {
  3482. // Command is set for external editors only (not for "shell" open).
  3483. if (!Data->Command.IsEmpty())
  3484. {
  3485. bool AnyFound = false;
  3486. bool AnyMDI = false;
  3487. bool AnyNonMDI = false;
  3488. bool AnyDetect = false;
  3489. TEditorList * EditorList = new TEditorList();
  3490. try
  3491. {
  3492. *EditorList = *WinConfiguration->EditorList;
  3493. for (int i = 0; i < EditorList->Count; i++)
  3494. {
  3495. const TEditorPreferences * Editor = EditorList->Editors[i];
  3496. if ((Editor->Data->Editor == edExternal) &&
  3497. (Editor->Data->ExternalEditor == Data->Command))
  3498. {
  3499. AnyFound = true;
  3500. if (Editor->Data->SDIExternalEditor)
  3501. {
  3502. AnyNonMDI = true;
  3503. if (Editor->Data->DetectMDIExternalEditor)
  3504. {
  3505. AnyDetect = true;
  3506. }
  3507. }
  3508. else
  3509. {
  3510. AnyMDI = true;
  3511. }
  3512. }
  3513. }
  3514. bool EnableMDI = false;
  3515. bool DisableDetect = false;
  3516. if (AnyMDI)
  3517. {
  3518. KeepOpen = true;
  3519. if (AnyNonMDI)
  3520. {
  3521. // there is at least one instance of the editor with MDI support enabled,
  3522. // and one with disabled, enable it for all instances
  3523. EnableMDI = true;
  3524. }
  3525. }
  3526. else if (AnyFound && !AnyDetect)
  3527. {
  3528. // at least once instance found but all have MDI autodetection disabled
  3529. // => close the file (default action)
  3530. }
  3531. else
  3532. {
  3533. // no instance of the editor has MDI support enabled
  3534. TMessageParams Params;
  3535. if (AnyFound)
  3536. {
  3537. // there is at least one instance of the editor
  3538. Params.Params |= mpNeverAskAgainCheck;
  3539. }
  3540. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3541. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3542. switch (Answer)
  3543. {
  3544. case qaNeverAskAgain:
  3545. DisableDetect = true;
  3546. break;
  3547. case qaNo:
  3548. EnableMDI = true;
  3549. KeepOpen = true;
  3550. break;
  3551. }
  3552. }
  3553. if (AnyFound && (EnableMDI || DisableDetect))
  3554. {
  3555. bool Changed = false;
  3556. for (int i = 0; i < EditorList->Count; i++)
  3557. {
  3558. const TEditorPreferences * Editor = EditorList->Editors[i];
  3559. if ((Editor->Data->Editor == edExternal) &&
  3560. (Editor->Data->ExternalEditor == Data->Command) &&
  3561. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3562. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3563. {
  3564. Changed = true;
  3565. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3566. if (EnableMDI)
  3567. {
  3568. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3569. }
  3570. if (DisableDetect)
  3571. {
  3572. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3573. }
  3574. EditorList->Change(i, UpdatedEditor);
  3575. }
  3576. }
  3577. if (Changed)
  3578. {
  3579. WinConfiguration->EditorList = EditorList;
  3580. }
  3581. }
  3582. }
  3583. __finally
  3584. {
  3585. delete EditorList;
  3586. }
  3587. }
  3588. else
  3589. {
  3590. // "open" case
  3591. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3592. qaOK, HELP_APP_EARLY_CLOSED);
  3593. }
  3594. }
  3595. //---------------------------------------------------------------------------
  3596. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3597. {
  3598. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3599. }
  3600. //---------------------------------------------------------------------------
  3601. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3602. {
  3603. SideEnter(osRemote);
  3604. }
  3605. //---------------------------------------------------------------------------
  3606. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3607. {
  3608. MakeNextInTabOrder(RemoteDirView, RemoteDriveView);
  3609. SideEnter(osRemote);
  3610. }
  3611. //---------------------------------------------------------------------------
  3612. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3613. {
  3614. FCurrentSide = Side;
  3615. if (Visible)
  3616. {
  3617. UpdateControls();
  3618. }
  3619. }
  3620. //---------------------------------------------------------------------------
  3621. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3622. TStrings * FileList, bool Alternative)
  3623. {
  3624. TCustomDirView * DView = DirView(Side);
  3625. DView->SaveSelection();
  3626. DView->SaveSelectedNames();
  3627. DebugAssert(!FAlternativeDelete);
  3628. FAlternativeDelete = Alternative;
  3629. try
  3630. {
  3631. if (Side == osRemote)
  3632. {
  3633. DebugAssert(Terminal != NULL);
  3634. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3635. }
  3636. else
  3637. {
  3638. try
  3639. {
  3640. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3641. }
  3642. __finally
  3643. {
  3644. ReloadLocalDirectory();
  3645. }
  3646. }
  3647. FAlternativeDelete = false;
  3648. }
  3649. catch(...)
  3650. {
  3651. FAlternativeDelete = false;
  3652. DView->DiscardSavedSelection();
  3653. throw;
  3654. }
  3655. DView->RestoreSelection();
  3656. }
  3657. //---------------------------------------------------------------------------
  3658. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3659. {
  3660. DebugAssert(Terminal);
  3661. RemoteDirView->SaveSelection();
  3662. RemoteDirView->SaveSelectedNames();
  3663. try
  3664. {
  3665. if (Lock)
  3666. {
  3667. Terminal->LockFiles(FileList);
  3668. }
  3669. else
  3670. {
  3671. Terminal->UnlockFiles(FileList);
  3672. }
  3673. }
  3674. catch(...)
  3675. {
  3676. RemoteDirView->DiscardSavedSelection();
  3677. throw;
  3678. }
  3679. RemoteDirView->RestoreSelection();
  3680. }
  3681. //---------------------------------------------------------------------------
  3682. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TTerminal *& Session,
  3683. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3684. bool NoConfirmation, bool Move)
  3685. {
  3686. DebugAssert(Terminal != NULL);
  3687. // update Terminal->StateData->RemoteDirectory
  3688. UpdateTerminal(Terminal);
  3689. if (Session == NULL)
  3690. {
  3691. Session = TTerminalManager::Instance()->ActiveTerminal;
  3692. }
  3693. if (Session == TTerminalManager::Instance()->ActiveTerminal)
  3694. {
  3695. if (RemoteDriveView->DropTarget != NULL)
  3696. {
  3697. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3698. }
  3699. else if (RemoteDirView->DropTarget != NULL)
  3700. {
  3701. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3702. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3703. }
  3704. else
  3705. {
  3706. Target = RemoteDirView->Path;
  3707. }
  3708. }
  3709. else
  3710. {
  3711. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(Session));
  3712. Target = ManagedTerminal->StateData->RemoteDirectory;
  3713. }
  3714. Target = UnixIncludeTrailingBackslash(Target);
  3715. if (FileList->Count == 1)
  3716. {
  3717. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3718. }
  3719. else
  3720. {
  3721. FileMask = AnyMask;
  3722. }
  3723. DirectCopy = FTerminal->IsCapable[fcRemoteCopy] || FTerminal->IsCapable[fcSecondaryShell];
  3724. bool Result = true;
  3725. if (!NoConfirmation)
  3726. {
  3727. bool Multi = (FileList->Count > 1);
  3728. if (Move)
  3729. {
  3730. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3731. }
  3732. else
  3733. {
  3734. TStrings * Sessions = TTerminalManager::Instance()->TerminalList;
  3735. TStrings * Directories = new TStringList;
  3736. try
  3737. {
  3738. for (int Index = 0; Index < Sessions->Count; Index++)
  3739. {
  3740. TManagedTerminal * Terminal =
  3741. dynamic_cast<TManagedTerminal *>(Sessions->Objects[Index]);
  3742. Directories->Add(Terminal->StateData->RemoteDirectory);
  3743. }
  3744. TDirectRemoteCopy AllowDirectCopy;
  3745. if (FTerminal->IsCapable[fcRemoteCopy] || FTerminal->CommandSessionOpened)
  3746. {
  3747. DebugAssert(DirectCopy);
  3748. AllowDirectCopy = drcAllow;
  3749. }
  3750. else if (FTerminal->IsCapable[fcSecondaryShell])
  3751. {
  3752. DebugAssert(DirectCopy);
  3753. AllowDirectCopy = drcConfirmCommandSession;
  3754. }
  3755. else
  3756. {
  3757. DebugAssert(!DirectCopy);
  3758. AllowDirectCopy = drcDisallow;
  3759. }
  3760. void * ASession = Session;
  3761. Result = DoRemoteCopyDialog(Sessions, Directories, AllowDirectCopy,
  3762. Multi, ASession, Target, FileMask, DirectCopy, TTerminalManager::Instance()->ActiveTerminal);
  3763. Session = static_cast<TTerminal *>(ASession);
  3764. }
  3765. __finally
  3766. {
  3767. delete Directories;
  3768. }
  3769. }
  3770. }
  3771. return Result;
  3772. }
  3773. //---------------------------------------------------------------------------
  3774. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3775. TStrings * FileList, bool NoConfirmation, bool Move, TTerminal * Session)
  3776. {
  3777. bool DirectCopy;
  3778. UnicodeString Target, FileMask;
  3779. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3780. if (Result)
  3781. {
  3782. if (!Move && !DirectCopy)
  3783. {
  3784. Configuration->Usage->Inc("RemoteCopyTemp");
  3785. UnicodeString RootTempDir;
  3786. UnicodeString TempDir;
  3787. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3788. TStrings * TemporaryFilesList = new TStringList();
  3789. try
  3790. {
  3791. TMakeLocalFileListParams MakeFileListParam;
  3792. MakeFileListParam.FileList = TemporaryFilesList;
  3793. MakeFileListParam.FileTimes = NULL;
  3794. MakeFileListParam.IncludeDirs = true;
  3795. MakeFileListParam.Recursive = false;
  3796. ProcessLocalDirectory(TempDir, FTerminal->MakeLocalFileList, &MakeFileListParam);
  3797. TTerminalManager::Instance()->ActiveTerminal = Session;
  3798. if (TemporaryFilesList->Count > 0)
  3799. {
  3800. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3801. CopyParam.FileMask = FileMask;
  3802. DebugAssert(!FAutoOperation);
  3803. FAutoOperation = true;
  3804. FTerminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3805. }
  3806. }
  3807. __finally
  3808. {
  3809. delete TemporaryFilesList;
  3810. FAutoOperation = false;
  3811. if (!RootTempDir.IsEmpty())
  3812. {
  3813. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3814. }
  3815. }
  3816. }
  3817. else
  3818. {
  3819. RemoteDirView->SaveSelection();
  3820. RemoteDirView->SaveSelectedNames();
  3821. try
  3822. {
  3823. if (Move)
  3824. {
  3825. Configuration->Usage->Inc("RemoteMove");
  3826. Terminal->MoveFiles(FileList, Target, FileMask);
  3827. }
  3828. else
  3829. {
  3830. Configuration->Usage->Inc("RemoteCopyDirect");
  3831. DebugAssert(DirectCopy);
  3832. DebugAssert(Session == FTerminal);
  3833. if (FTerminal->IsCapable[fcRemoteCopy] ||
  3834. FTerminal->CommandSessionOpened ||
  3835. CommandSessionFallback())
  3836. {
  3837. Terminal->CopyFiles(FileList, Target, FileMask);
  3838. }
  3839. }
  3840. }
  3841. catch(...)
  3842. {
  3843. RemoteDirView->DiscardSavedSelection();
  3844. throw;
  3845. }
  3846. RemoteDirView->RestoreSelection();
  3847. }
  3848. }
  3849. return Result;
  3850. }
  3851. //---------------------------------------------------------------------------
  3852. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3853. const UnicodeString & Path, TRemoteProperties & Properties)
  3854. {
  3855. Properties.Valid = Properties.Valid << vpEncrypt;
  3856. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  3857. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  3858. }
  3859. //---------------------------------------------------------------------------
  3860. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3861. {
  3862. Side = GetSide(Side);
  3863. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3864. TRemoteProperties * AProperties = (Side == osRemote ? &Properties : NULL);
  3865. UnicodeString Name = LoadStr(NEW_FOLDER);
  3866. int AllowedChanges =
  3867. FLAGMASK((Side == osRemote) && Terminal->IsCapable[fcModeChanging], cpMode);
  3868. bool SaveSettings = false;
  3869. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  3870. {
  3871. TWindowLock Lock(this);
  3872. if (Side == osRemote)
  3873. {
  3874. if (SaveSettings)
  3875. {
  3876. GUIConfiguration->NewDirectoryProperties = Properties;
  3877. }
  3878. CreateRemoteDirectory(Name, Properties);
  3879. }
  3880. else
  3881. {
  3882. DirView(Side)->CreateDirectory(Name);
  3883. }
  3884. }
  3885. }
  3886. //---------------------------------------------------------------------------
  3887. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  3888. {
  3889. TWindowLock Lock(this);
  3890. DirView(Side)->ExecuteHomeDirectory();
  3891. }
  3892. //---------------------------------------------------------------------------
  3893. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  3894. {
  3895. DoOpenDirectoryDialog(odBrowse, Side);
  3896. }
  3897. //---------------------------------------------------------------------------
  3898. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  3899. {
  3900. UnicodeString Path = Bookmark->GetSideDirectory(Side);
  3901. bool Result = !Path.IsEmpty();
  3902. if (Result)
  3903. {
  3904. // While we might get here when the session is closed (from location profiles),
  3905. // it's not a problem as the Path setter is noop then
  3906. DirView(Side)->Path = Path;
  3907. }
  3908. return Result;
  3909. }
  3910. //---------------------------------------------------------------------------
  3911. void __fastcall TCustomScpExplorerForm::CalculateSize(
  3912. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  3913. bool & Close)
  3914. {
  3915. // terminal can be already closed (e.g. dropped connection)
  3916. if (Terminal != NULL)
  3917. {
  3918. try
  3919. {
  3920. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  3921. }
  3922. catch(...)
  3923. {
  3924. if (!Terminal->Active)
  3925. {
  3926. Close = true;
  3927. }
  3928. throw;
  3929. }
  3930. }
  3931. }
  3932. //---------------------------------------------------------------------------
  3933. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  3934. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  3935. bool & Close)
  3936. {
  3937. // terminal can be already closed (e.g. dropped connection)
  3938. if (Terminal != NULL)
  3939. {
  3940. Configuration->Usage->Inc(L"ChecksumCalculated");
  3941. try
  3942. {
  3943. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  3944. }
  3945. catch(...)
  3946. {
  3947. if (!Terminal->Active)
  3948. {
  3949. Close = true;
  3950. }
  3951. throw;
  3952. }
  3953. }
  3954. }
  3955. //---------------------------------------------------------------------------
  3956. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  3957. {
  3958. bool Result;
  3959. if (Side == osRemote)
  3960. {
  3961. TRemoteTokenList * GroupList = NULL;
  3962. TRemoteTokenList * UserList = NULL;
  3963. try
  3964. {
  3965. TRemoteProperties CurrentProperties;
  3966. if (Terminal->LoadFilesProperties(FileList))
  3967. {
  3968. RemoteDirView->Invalidate();
  3969. }
  3970. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  3971. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  3972. if (CapableGroupChanging || CapableOwnerChanging)
  3973. {
  3974. if (CapableGroupChanging)
  3975. {
  3976. GroupList = Terminal->Groups->Duplicate();
  3977. }
  3978. if (CapableOwnerChanging)
  3979. {
  3980. UserList = Terminal->Users->Duplicate();
  3981. }
  3982. TRemoteDirectory * Files = Terminal->Files;
  3983. int Count = Files->Count;
  3984. if (Count > 100)
  3985. {
  3986. Count = 100;
  3987. }
  3988. for (int Index = 0; Index < Count; Index++)
  3989. {
  3990. TRemoteFile * File = Files->Files[Index];
  3991. if (CapableGroupChanging)
  3992. {
  3993. GroupList->AddUnique(File->Group);
  3994. }
  3995. if (CapableOwnerChanging)
  3996. {
  3997. UserList->AddUnique(File->Owner);
  3998. }
  3999. }
  4000. // if we haven't collected tokens for all files in current directory,
  4001. // make sure we collect them at least for all selected files.
  4002. // (note that so far the files in FileList has to be from current direcotry)
  4003. if (Count < Files->Count)
  4004. {
  4005. for (int Index = 0; Index < FileList->Count; Index++)
  4006. {
  4007. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4008. if (CapableGroupChanging)
  4009. {
  4010. GroupList->AddUnique(File->Group);
  4011. }
  4012. if (CapableOwnerChanging)
  4013. {
  4014. UserList->AddUnique(File->Owner);
  4015. }
  4016. }
  4017. }
  4018. }
  4019. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4020. int Flags = 0;
  4021. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4022. if (CapableOwnerChanging) Flags |= cpOwner;
  4023. if (CapableGroupChanging) Flags |= cpGroup;
  4024. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4025. if (Terminal->IsCapable[fcCalculatingChecksum])
  4026. {
  4027. CalculateChecksumEvent = CalculateChecksum;
  4028. }
  4029. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4030. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4031. TRemoteProperties NewProperties = CurrentProperties;
  4032. Result =
  4033. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4034. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4035. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4036. CalculateSize, CalculateChecksumEvent);
  4037. if (Result)
  4038. {
  4039. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4040. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4041. }
  4042. }
  4043. __finally
  4044. {
  4045. delete GroupList;
  4046. delete UserList;
  4047. }
  4048. }
  4049. else
  4050. {
  4051. DirView(Side)->DisplayPropertiesMenu();
  4052. Result = true;
  4053. }
  4054. return Result;
  4055. }
  4056. //---------------------------------------------------------------------------
  4057. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4058. {
  4059. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4060. {
  4061. FIgnoreNextDialogChar = Key;
  4062. }
  4063. Key = 0;
  4064. }
  4065. //---------------------------------------------------------------------------
  4066. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4067. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4068. {
  4069. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4070. if (Command != NULL)
  4071. {
  4072. KeyProcessed(Key, Shift);
  4073. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4074. {
  4075. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4076. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4077. false, false, const_cast<TCustomCommandType *>(Command));
  4078. }
  4079. }
  4080. }
  4081. //---------------------------------------------------------------------------
  4082. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4083. {
  4084. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4085. {
  4086. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4087. }
  4088. if (!DirView(osCurrent)->IsEditing())
  4089. {
  4090. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4091. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4092. {
  4093. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4094. if (((Action->ShortCut == KeyShortCut) ||
  4095. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4096. AllowedAction(Action, aaShortCut))
  4097. {
  4098. // Has to be called before the action as the dialog char is already in queue.
  4099. // So when the action consumes message queue, we already need to have the
  4100. // FIgnoreNextDialogChar set
  4101. KeyProcessed(Key, Shift);
  4102. // Reset reference to previous component (when menu/toolbar was clicked).
  4103. // Needed to detect that action was invoked by keyboard shortcut
  4104. // in TNonVisualDataModule::ExplorerActionsExecute
  4105. Action->ActionComponent = NULL;
  4106. Action->Execute();
  4107. return;
  4108. }
  4109. }
  4110. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4111. {
  4112. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4113. {
  4114. KeyProcessed(Key, Shift);
  4115. TTerminalManager::Instance()->ActiveTerminalIndex = i;
  4116. return;
  4117. }
  4118. }
  4119. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4120. {
  4121. KeyProcessed(Key, Shift);
  4122. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4123. }
  4124. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4125. if (IsCustomShortCut(CustomShortCut))
  4126. {
  4127. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4128. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4129. if (WinConfiguration->SharedBookmarks != NULL)
  4130. {
  4131. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4132. if ((Bookmark != NULL) &&
  4133. OpenBookmark(FCurrentSide, Bookmark))
  4134. {
  4135. KeyProcessed(Key, Shift);
  4136. }
  4137. }
  4138. }
  4139. }
  4140. TForm::KeyDown(Key, Shift);
  4141. }
  4142. //---------------------------------------------------------------------------
  4143. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4144. {
  4145. DebugAssert(Terminal != NULL);
  4146. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4147. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  4148. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4149. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4150. bool SecurityEnabled = !SessionInfo.SecurityProtocolName.IsEmpty();
  4151. SessionStatusBar->Panels->Items[Offset + 0]->Enabled = SecurityEnabled;
  4152. // expanded from ?: to avoid memory leaks
  4153. if (SecurityEnabled)
  4154. {
  4155. SessionStatusBar->Panels->Items[Offset + 0]->Hint =
  4156. FMTLOAD(STATUS_SECURE, (SessionInfo.SecurityProtocolName));
  4157. }
  4158. else
  4159. {
  4160. SessionStatusBar->Panels->Items[Offset + 0]->Hint = LoadStr(STATUS_INSECURE);
  4161. }
  4162. if (FileSystemInfo.ProtocolName.IsEmpty())
  4163. {
  4164. SessionStatusBar->Panels->Items[Offset + 1]->Caption = SessionInfo.ProtocolName;
  4165. }
  4166. else
  4167. {
  4168. SessionStatusBar->Panels->Items[Offset + 1]->Caption = FileSystemInfo.ProtocolName;
  4169. }
  4170. SessionStatusBar->Panels->Items[Offset + 1]->Hint = LoadStr(STATUS_PROTOCOL_HINT);
  4171. SessionStatusBar->Panels->Items[Offset + 2]->Enabled =
  4172. (!SessionInfo.CSCompression.IsEmpty() || !SessionInfo.SCCompression.IsEmpty());
  4173. UnicodeString CSCompressionStr = DefaultStr(SessionInfo.CSCompression, LoadStr(NO_STR));
  4174. UnicodeString SCCompressionStr = DefaultStr(SessionInfo.SCCompression, LoadStr(NO_STR));
  4175. UnicodeString CompressionStr = CSCompressionStr;
  4176. if (CSCompressionStr != SCCompressionStr)
  4177. {
  4178. CompressionStr += UnicodeString(L"/") + SCCompressionStr;
  4179. }
  4180. SessionStatusBar->Panels->Items[Offset + 2]->Hint = FMTLOAD(STATUS_COMPRESSION_HINT, (CompressionStr));
  4181. SessionStatusBar->Panels->Items[Offset + 3]->Hint = LoadStr(STATUS_DURATION_HINT);
  4182. UpdateStatusBar();
  4183. }
  4184. //---------------------------------------------------------------------------
  4185. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4186. {
  4187. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4188. DebugAssert(SessionStatusBar != NULL);
  4189. if (!Terminal || !Terminal->Active || Terminal->Status < ssOpened)
  4190. {
  4191. // note: (Terminal->Status < sshReady) currently never happens here,
  4192. // so STATUS_CONNECTING is never used
  4193. SessionStatusBar->SimplePanel = true;
  4194. SessionStatusBar->SimpleText = LoadStr(
  4195. !Terminal || !Terminal->Active ? STATUS_NOT_CONNECTED : STATUS_CONNECTING);
  4196. }
  4197. else
  4198. {
  4199. DebugAssert(Terminal);
  4200. SessionStatusBar->SimplePanel = false;
  4201. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4202. if (!FNote.IsEmpty())
  4203. {
  4204. SessionStatusBar->Panels->Items[0]->Caption = FNote;
  4205. }
  4206. else
  4207. {
  4208. UpdateStatusPanelText(SessionStatusBar->Panels->Items[0]);
  4209. }
  4210. SessionStatusBar->Panels->Items[0]->Hint = FNoteHints;
  4211. SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1]->Caption =
  4212. FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4213. }
  4214. }
  4215. //---------------------------------------------------------------------------
  4216. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4217. {
  4218. Panel->Caption = L"";
  4219. }
  4220. //---------------------------------------------------------------------------
  4221. void __fastcall TCustomScpExplorerForm::Idle()
  4222. {
  4223. if (FShowing || FStandaloneEditing)
  4224. {
  4225. FEditorManager->Check();
  4226. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4227. // that needs to know if queue view is visible (and it may be closed after queue update)
  4228. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4229. }
  4230. if (FShowing)
  4231. {
  4232. if (!NonVisualDataModule->Busy &&
  4233. // Menu is opened or drag&drop is going on
  4234. (Mouse->Capture == NULL))
  4235. {
  4236. if (FRefreshRemoteDirectory)
  4237. {
  4238. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4239. {
  4240. Terminal->RefreshDirectory();
  4241. }
  4242. FRefreshRemoteDirectory = false;
  4243. }
  4244. if (FRefreshLocalDirectory)
  4245. {
  4246. ReloadLocalDirectory();
  4247. FRefreshLocalDirectory = false;
  4248. }
  4249. if (WinConfiguration->RefreshRemotePanel)
  4250. {
  4251. TManagedTerminal * ManagedTerminal =
  4252. dynamic_cast<TManagedTerminal *>(Terminal);
  4253. if ((ManagedTerminal != NULL) && (Terminal->Status == ssOpened) &&
  4254. (Now() - ManagedTerminal->DirectoryLoaded >
  4255. WinConfiguration->RefreshRemotePanelInterval))
  4256. {
  4257. RemoteDirView->ReloadDirectory();
  4258. }
  4259. }
  4260. }
  4261. }
  4262. if (FShowing || FStandaloneEditing)
  4263. {
  4264. if (FQueueStatusInvalidated)
  4265. {
  4266. UpdateQueueStatus(false);
  4267. }
  4268. RefreshQueueItems();
  4269. }
  4270. if (FShowing)
  4271. {
  4272. UpdateStatusBar();
  4273. }
  4274. FIgnoreNextDialogChar = 0;
  4275. }
  4276. //---------------------------------------------------------------------------
  4277. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4278. {
  4279. try
  4280. {
  4281. FUserActionTimer->Enabled = false;
  4282. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4283. {
  4284. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4285. {
  4286. FPendingQueueActionItem->ProcessUserAction();
  4287. }
  4288. }
  4289. }
  4290. __finally
  4291. {
  4292. FPendingQueueActionItem = NULL;
  4293. }
  4294. }
  4295. //---------------------------------------------------------------------------
  4296. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4297. {
  4298. if (WinConfiguration->MinimizeToTray)
  4299. {
  4300. UpdateTrayIcon();
  4301. FTrayIcon->Visible = true;
  4302. if (Visible)
  4303. {
  4304. ShowWindow(Handle, SW_HIDE);
  4305. }
  4306. }
  4307. }
  4308. //---------------------------------------------------------------------------
  4309. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4310. {
  4311. // WORKAROUND
  4312. // When restoring maximized window from minimization,
  4313. // rarely some controls do not align properly.
  4314. // Two instances seen (both for Commander):
  4315. // - When restoring, window is temporarily narrower (not maximized),
  4316. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4317. // Once maximized already, top dock shinks back, but the session PageControl,
  4318. // do not align up, leaving space between TopDock and PageControl.
  4319. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4320. for (int Index = 0; Index < ControlCount; Index++)
  4321. {
  4322. RealignControl(Controls[Index]);
  4323. }
  4324. if (FTrayIcon->Visible)
  4325. {
  4326. FTrayIcon->Visible = false;
  4327. if (Visible)
  4328. {
  4329. ShowWindow(Handle, SW_SHOW);
  4330. }
  4331. }
  4332. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4333. {
  4334. FNeedSession = false;
  4335. NonVisualDataModule->StartBusy();
  4336. try
  4337. {
  4338. NeedSession(false);
  4339. }
  4340. __finally
  4341. {
  4342. NonVisualDataModule->EndBusy();
  4343. }
  4344. }
  4345. }
  4346. //---------------------------------------------------------------------------
  4347. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4348. {
  4349. FTrayIcon->Hint = GetMainForm()->Caption;
  4350. }
  4351. //---------------------------------------------------------------------------
  4352. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4353. {
  4354. UpdateTrayIcon();
  4355. }
  4356. //---------------------------------------------------------------------------
  4357. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4358. {
  4359. // workaround
  4360. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4361. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4362. // But WM_ACTIVATEAPP message can be received even when
  4363. // the main window is minimized to the tray and internal editor window is focused
  4364. // (after another application was previously active)
  4365. if (::IsIconic(Handle))
  4366. {
  4367. if (!IsAppIconic())
  4368. {
  4369. SetAppIconic(true);
  4370. }
  4371. }
  4372. ::ApplicationRestore();
  4373. Application->BringToFront();
  4374. }
  4375. //---------------------------------------------------------------------------
  4376. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4377. {
  4378. RestoreApp();
  4379. }
  4380. //---------------------------------------------------------------------------
  4381. void __fastcall TCustomScpExplorerForm::NewSession(bool FromSite, const UnicodeString & SessionUrl)
  4382. {
  4383. if (OpenInNewWindow())
  4384. {
  4385. // todo: Pass FromSite
  4386. ExecuteNewInstance(SessionUrl);
  4387. }
  4388. else
  4389. {
  4390. TTerminalManager::Instance()->NewSession(FromSite, SessionUrl);
  4391. }
  4392. }
  4393. //---------------------------------------------------------------------------
  4394. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4395. {
  4396. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->SessionName;
  4397. // current working directories become defaults here, what is not right
  4398. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4399. StoredSessions->NewSession(SessionName, SessionData.get());
  4400. // modified only, explicit
  4401. StoredSessions->Save(false, true);
  4402. // encode session name because of slashes in hierarchical sessions
  4403. return EncodeUrlString(SessionName);
  4404. }
  4405. //---------------------------------------------------------------------------
  4406. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4407. {
  4408. if (OpenInNewWindow())
  4409. {
  4410. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4411. }
  4412. else
  4413. {
  4414. // current working directories become defaults here, what is not right
  4415. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4416. TTerminalManager * Manager = TTerminalManager::Instance();
  4417. TTerminal * Terminal = Manager->NewTerminal(SessionData.get());
  4418. Manager->ActiveTerminal = Terminal;
  4419. }
  4420. }
  4421. //---------------------------------------------------------------------------
  4422. void __fastcall TCustomScpExplorerForm::RenameSession()
  4423. {
  4424. UnicodeString Name = Terminal->SessionData->SessionName;
  4425. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME))
  4426. {
  4427. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4428. TSessionData::ValidateName(Name);
  4429. // This is inconsistent with how color (for example) is handled.
  4430. Terminal->SessionData->Name = Name;
  4431. UpdateControls();
  4432. // Add/Remove distinguishing paths from sessions of the same name.
  4433. DoTerminalListChanged();
  4434. }
  4435. }
  4436. //---------------------------------------------------------------------------
  4437. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4438. {
  4439. DebugAssert(Queue != NULL);
  4440. bool Result = Queue->IsEmpty;
  4441. if (!Result)
  4442. {
  4443. SetFocus();
  4444. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4445. }
  4446. return Result;
  4447. }
  4448. //---------------------------------------------------------------------------
  4449. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4450. {
  4451. return CanCloseQueue(FQueue);
  4452. }
  4453. //---------------------------------------------------------------------------
  4454. void __fastcall TCustomScpExplorerForm::CloseSession()
  4455. {
  4456. if (CanCloseQueue())
  4457. {
  4458. TTerminalManager::Instance()->FreeActiveTerminal();
  4459. }
  4460. }
  4461. //---------------------------------------------------------------------------
  4462. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4463. {
  4464. if (OpenInNewWindow())
  4465. {
  4466. // encode session name because of slashes in hierarchical sessions
  4467. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4468. }
  4469. else
  4470. {
  4471. TTerminalManager * Manager = TTerminalManager::Instance();
  4472. TTerminal * Terminal = Manager->NewTerminal(Data);
  4473. Manager->ActiveTerminal = Terminal;
  4474. }
  4475. }
  4476. //---------------------------------------------------------------------------
  4477. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4478. {
  4479. if (OpenInNewWindow())
  4480. {
  4481. ExecuteNewInstance(Name);
  4482. }
  4483. else
  4484. {
  4485. TTerminalManager * Manager = TTerminalManager::Instance();
  4486. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4487. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4488. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  4489. }
  4490. }
  4491. //---------------------------------------------------------------------------
  4492. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4493. bool &CanClose)
  4494. {
  4495. if (Terminal != NULL)
  4496. {
  4497. if (Terminal->Active && WinConfiguration->ConfirmClosingSession)
  4498. {
  4499. unsigned int Result;
  4500. TMessageParams Params(mpNeverAskAgainCheck);
  4501. UnicodeString Message;
  4502. int Answers = qaOK | qaCancel;
  4503. if (TTerminalManager::Instance()->Count > 1)
  4504. {
  4505. if (!WinConfiguration->AutoSaveWorkspace)
  4506. {
  4507. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE2);
  4508. Answers = qaYes | qaNo | qaCancel;
  4509. }
  4510. else
  4511. {
  4512. Message = MainInstructions(LoadStr(CLOSE_SESSIONS));
  4513. }
  4514. }
  4515. else
  4516. {
  4517. Message = MainInstructions(FMTLOAD(CLOSE_SESSION, (Terminal->SessionData->SessionName)));
  4518. }
  4519. if (WinConfiguration->AutoSaveWorkspace)
  4520. {
  4521. Message =
  4522. FORMAT("%s\n\n%s", (Message,
  4523. FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()))));
  4524. }
  4525. SetFocus();
  4526. Result = MessageDialog(Message, qtConfirmation,
  4527. Answers, HELP_NONE, &Params);
  4528. if (Result == qaNeverAskAgain)
  4529. {
  4530. WinConfiguration->ConfirmClosingSession = false;
  4531. }
  4532. if (Result == qaNo)
  4533. {
  4534. CanClose = SaveWorkspace(true);
  4535. // note that the workspace will be saved redundatly again from FormClose
  4536. }
  4537. else
  4538. {
  4539. CanClose =
  4540. (Result == qaOK) ||
  4541. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4542. (Result == qaNeverAskAgain);
  4543. }
  4544. }
  4545. if (CanClose)
  4546. {
  4547. CanClose = CanCloseQueue();
  4548. }
  4549. }
  4550. if (CanClose)
  4551. {
  4552. CanClose =
  4553. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4554. FEditorManager->CloseExternalFilesWithoutProcess();
  4555. if (CanClose)
  4556. {
  4557. while (CanClose && (FLocalEditors->Count > 0))
  4558. {
  4559. int PrevCount = FLocalEditors->Count;
  4560. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4561. CanClose = (FLocalEditors->Count < PrevCount);
  4562. }
  4563. if (CanClose)
  4564. {
  4565. CanClose = FEditorManager->Empty(true);
  4566. if (!CanClose)
  4567. {
  4568. SetFocus();
  4569. CanClose =
  4570. (MessageDialog(
  4571. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4572. }
  4573. }
  4574. }
  4575. }
  4576. }
  4577. //---------------------------------------------------------------------------
  4578. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4579. {
  4580. TForm * Form = dynamic_cast<TForm *>(Sender);
  4581. DebugAssert(Form != NULL);
  4582. Form->Close();
  4583. }
  4584. //---------------------------------------------------------------------------
  4585. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4586. {
  4587. TForm * Form = dynamic_cast<TForm *>(Sender);
  4588. delete Form;
  4589. }
  4590. //---------------------------------------------------------------------------
  4591. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4592. {
  4593. while (FLocalEditors->Count > 0)
  4594. {
  4595. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4596. }
  4597. }
  4598. //---------------------------------------------------------------------------
  4599. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4600. TObject *Sender)
  4601. {
  4602. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4603. try
  4604. {
  4605. SetProperties(osRemote, FileList);
  4606. }
  4607. __finally
  4608. {
  4609. delete FileList;
  4610. }
  4611. }
  4612. //---------------------------------------------------------------------------
  4613. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4614. {
  4615. if (value)
  4616. {
  4617. if (Component == fcCustomCommandsBand)
  4618. {
  4619. UpdateCustomCommandsToolbar();
  4620. }
  4621. }
  4622. }
  4623. //---------------------------------------------------------------------------
  4624. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4625. {
  4626. TControl * Control = GetComponent(Component);
  4627. DebugAssert(Control);
  4628. bool Changed = (Control->Visible != value);
  4629. if (Changed)
  4630. {
  4631. ComponentShowing(Component, value);
  4632. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4633. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4634. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4635. if (value)
  4636. {
  4637. int RemainingHeight = Control->Parent->ClientHeight;
  4638. int RemainingWidth = Control->Parent->ClientWidth;
  4639. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4640. {
  4641. TControl * ChildControl = Control->Parent->Controls[i];
  4642. if (ChildControl->Visible)
  4643. {
  4644. switch (ChildControl->Align)
  4645. {
  4646. case alTop:
  4647. case alBottom:
  4648. RemainingHeight -= ChildControl->Height;
  4649. break;
  4650. case alLeft:
  4651. case alRight:
  4652. RemainingWidth -= ChildControl->Width;
  4653. break;
  4654. }
  4655. }
  4656. }
  4657. int Reserve = ScaleByTextHeight(this, 32);
  4658. // queue in explorer, trees in commander
  4659. if (Control->Height > RemainingHeight - Reserve)
  4660. {
  4661. Control->Height = RemainingHeight / 2;
  4662. }
  4663. if (Control->Width > RemainingWidth - Reserve)
  4664. {
  4665. Control->Width = RemainingWidth / 2;
  4666. }
  4667. }
  4668. Control->Visible = value;
  4669. if (WasFocused && Visible)
  4670. {
  4671. DirView(osCurrent)->SetFocus();
  4672. }
  4673. FixControlsPlacement();
  4674. }
  4675. }
  4676. //---------------------------------------------------------------------------
  4677. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4678. {
  4679. TControl * Control = GetComponent(Component);
  4680. if (Control == NULL)
  4681. {
  4682. return false;
  4683. }
  4684. else
  4685. {
  4686. return Control->Visible;
  4687. }
  4688. }
  4689. //---------------------------------------------------------------------------
  4690. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4691. {
  4692. bool Result;
  4693. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4694. {
  4695. Result = !IsUWP();
  4696. }
  4697. else
  4698. {
  4699. Result = true;
  4700. }
  4701. return Result;
  4702. }
  4703. //---------------------------------------------------------------------------
  4704. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4705. {
  4706. if (RemoteDirView->ItemFocused != NULL)
  4707. {
  4708. RemoteDirView->ItemFocused->MakeVisible(false);
  4709. }
  4710. QueueSplitter->Visible = QueuePanel->Visible;
  4711. RemotePanelSplitter->Visible = RemoteDriveView->Visible;
  4712. }
  4713. //---------------------------------------------------------------------------
  4714. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4715. {
  4716. switch (Component) {
  4717. case fcStatusBar: return RemoteStatusBar;
  4718. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4719. case fcQueueView: return QueuePanel;
  4720. case fcQueueToolbar: return QueueDock;
  4721. case fcRemoteTree: return RemoteDriveView;
  4722. case fcSessionsTabs: return SessionsPageControl;
  4723. default: return NULL;
  4724. }
  4725. }
  4726. //---------------------------------------------------------------------------
  4727. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4728. TObject *Sender, TListColumn *Column, TPoint &Point)
  4729. {
  4730. DebugAssert(NonVisualDataModule && Column && Sender);
  4731. NonVisualDataModule->ListColumn = Column;
  4732. TPopupMenu * DirViewColumnMenu;
  4733. if (Sender == RemoteDirView)
  4734. {
  4735. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4736. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4737. (Column->Index == uvName);
  4738. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4739. (Column->Index == uvSize);
  4740. }
  4741. else
  4742. {
  4743. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4744. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4745. (Column->Index == dvName);
  4746. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4747. (Column->Index == dvSize);
  4748. }
  4749. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  4750. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  4751. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  4752. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4753. }
  4754. //---------------------------------------------------------------------------
  4755. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4756. TObject *Sender, TListItem *Item, bool &AllowExec)
  4757. {
  4758. DoDirViewExecFile(Sender, Item, AllowExec);
  4759. }
  4760. //---------------------------------------------------------------------------
  4761. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4762. TListItem * Item, bool & AllowExec)
  4763. {
  4764. DebugAssert(Sender && Item && Configuration);
  4765. DebugAssert(AllowExec);
  4766. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  4767. bool Remote = (ADirView == DirView(osRemote));
  4768. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks;
  4769. TOperationSide Side = (Remote ? osRemote : osLocal);
  4770. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  4771. // on files only (not directories)
  4772. // and only when symlinks are resolved (apply to remote panel only)
  4773. if (!ADirView->ItemIsDirectory(Item) &&
  4774. (ResolvedSymlinks || FForceExecution || Terminal->IsEncryptingFiles()))
  4775. {
  4776. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  4777. !FForceExecution &&
  4778. (ResolvedSymlinks || Terminal->IsEncryptingFiles()))
  4779. {
  4780. if (WinConfiguration->DoubleClickAction == dcaCopy)
  4781. {
  4782. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  4783. UnlockWindow();
  4784. try
  4785. {
  4786. ExecuteFileOperation(
  4787. foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  4788. }
  4789. __finally
  4790. {
  4791. LockWindow();
  4792. }
  4793. AllowExec = false;
  4794. }
  4795. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  4796. {
  4797. if (!Remote || !WinConfiguration->DisableOpenEdit)
  4798. {
  4799. ExecuteFile(Side, efDefaultEditor);
  4800. AllowExec = false;
  4801. }
  4802. }
  4803. else
  4804. {
  4805. DebugFail();
  4806. }
  4807. }
  4808. // if we have not done anything special, fall back to default behavior
  4809. if (AllowExec)
  4810. {
  4811. if (Remote && !WinConfiguration->DisableOpenEdit)
  4812. {
  4813. ExecuteFile(osRemote, efShell);
  4814. AllowExec = false;
  4815. }
  4816. }
  4817. }
  4818. }
  4819. //---------------------------------------------------------------------------
  4820. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  4821. {
  4822. return ((Side == osRemote) || (Side == osCurrent));
  4823. }
  4824. //---------------------------------------------------------------------------
  4825. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  4826. {
  4827. DebugFail();
  4828. }
  4829. //---------------------------------------------------------------------------
  4830. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  4831. {
  4832. DebugFail();
  4833. }
  4834. //---------------------------------------------------------------------------
  4835. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  4836. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, bool UseDefaults)
  4837. {
  4838. TSynchronizeParamType Params;
  4839. Params.LocalDirectory = LocalDirectory;
  4840. Params.RemoteDirectory = RemoteDirectory;
  4841. int UnusedParams =
  4842. (GUIConfiguration->SynchronizeParams &
  4843. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize));
  4844. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  4845. Params.Options = GUIConfiguration->SynchronizeOptions;
  4846. bool SaveSettings = false;
  4847. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  4848. &DoSynchronizeTooManyDirectories);
  4849. DebugAssert(FSynchronizeController == NULL);
  4850. FSynchronizeController = &Controller;
  4851. bool Result;
  4852. try
  4853. {
  4854. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4855. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  4856. int Options =
  4857. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  4858. DebugAssert(FOnFeedSynchronizeError == NULL);
  4859. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  4860. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  4861. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  4862. if (Result)
  4863. {
  4864. if (SaveSettings)
  4865. {
  4866. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  4867. GUIConfiguration->SynchronizeOptions = Params.Options;
  4868. }
  4869. else
  4870. {
  4871. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  4872. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  4873. FLAGSET(Params.Options, soSynchronize))
  4874. {
  4875. GUIConfiguration->SynchronizeOptions =
  4876. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  4877. soSynchronize;
  4878. }
  4879. }
  4880. LocalDirectory = Params.LocalDirectory;
  4881. RemoteDirectory = Params.RemoteDirectory;
  4882. }
  4883. }
  4884. __finally
  4885. {
  4886. FSynchronizeController = NULL;
  4887. DebugAssert(FOnFeedSynchronizeError == NULL);
  4888. FOnFeedSynchronizeError = NULL;
  4889. }
  4890. return Result;
  4891. }
  4892. //---------------------------------------------------------------------------
  4893. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  4894. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  4895. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  4896. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  4897. TSynchronizeOptions * Options, bool Full)
  4898. {
  4899. // Keep-up-to-date mode
  4900. if (Terminal->Status == ssOpened)
  4901. {
  4902. try
  4903. {
  4904. int PParams = Params.Params;
  4905. if (!Full)
  4906. {
  4907. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  4908. if (FLAGSET(Params.Options, soRecurse))
  4909. {
  4910. PParams |= TTerminal::spSubDirs;
  4911. }
  4912. }
  4913. else
  4914. {
  4915. // if keepuptodate is non-recursive,
  4916. // full sync before has to be non-recursive as well
  4917. if (FLAGCLEAR(Params.Options, soRecurse))
  4918. {
  4919. PParams |= TTerminal::spNoRecurse;
  4920. }
  4921. }
  4922. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  4923. PParams, Checklist, Options);
  4924. }
  4925. catch (Exception & E)
  4926. {
  4927. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  4928. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  4929. {
  4930. // noop, already logged in MoreMessageDialog
  4931. }
  4932. else
  4933. {
  4934. ShowExtendedExceptionEx(Terminal, &E);
  4935. throw;
  4936. }
  4937. }
  4938. }
  4939. }
  4940. //---------------------------------------------------------------------------
  4941. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  4942. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  4943. const UnicodeString ErrorStr)
  4944. {
  4945. if (!Directory.IsEmpty())
  4946. {
  4947. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  4948. }
  4949. else
  4950. {
  4951. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  4952. }
  4953. }
  4954. //---------------------------------------------------------------------------
  4955. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  4956. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  4957. {
  4958. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  4959. {
  4960. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  4961. }
  4962. else
  4963. {
  4964. TMessageParams Params(mpNeverAskAgainCheck);
  4965. unsigned int Result = MessageDialog(
  4966. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  4967. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  4968. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  4969. {
  4970. MaxDirectories *= 2;
  4971. if (Result == qaNeverAskAgain)
  4972. {
  4973. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  4974. }
  4975. }
  4976. else
  4977. {
  4978. Abort();
  4979. }
  4980. }
  4981. }
  4982. //---------------------------------------------------------------------------
  4983. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  4984. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  4985. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  4986. TSynchronizeOptions * Options)
  4987. {
  4988. // Keep-up-to-date mode
  4989. DebugAssert(!FAutoOperation);
  4990. void * BatchStorage;
  4991. BatchStart(BatchStorage);
  4992. FAutoOperation = true;
  4993. bool AnyOperation = false;
  4994. TDateTime StartTime = Now();
  4995. TSynchronizeChecklist * AChecklist = NULL;
  4996. try
  4997. {
  4998. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  4999. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5000. {
  5001. FSynchronizeProgressForm->Start();
  5002. }
  5003. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5004. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5005. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5006. Options);
  5007. SAFE_DESTROY(FSynchronizeProgressForm);
  5008. AnyOperation = (AChecklist->CheckedCount > 0);
  5009. if (AnyOperation)
  5010. {
  5011. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5012. CreateProgressForm(&SynchronizeProgress);
  5013. try
  5014. {
  5015. Terminal->SynchronizeApply(
  5016. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5017. SynchronizeProcessedItem, NULL, NULL, NULL);
  5018. }
  5019. __finally
  5020. {
  5021. DestroyProgressForm();
  5022. }
  5023. }
  5024. }
  5025. __finally
  5026. {
  5027. if (Checklist == NULL)
  5028. {
  5029. delete AChecklist;
  5030. }
  5031. else
  5032. {
  5033. *Checklist = AChecklist;
  5034. }
  5035. FAutoOperation = false;
  5036. SAFE_DESTROY(FSynchronizeProgressForm);
  5037. BatchEnd(BatchStorage);
  5038. ReloadLocalDirectory();
  5039. if (AnyOperation)
  5040. {
  5041. OperationComplete(StartTime);
  5042. }
  5043. }
  5044. }
  5045. //---------------------------------------------------------------------------
  5046. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5047. {
  5048. // can be called from command line
  5049. return Visible &&
  5050. ((DirView(osRemote)->SelCount > 0) ||
  5051. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5052. }
  5053. //---------------------------------------------------------------------------
  5054. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5055. {
  5056. LogSynchronizeEvent(Terminal, Message);
  5057. }
  5058. //---------------------------------------------------------------------------
  5059. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5060. int Params, TSynchronizeOptions & Options)
  5061. {
  5062. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5063. {
  5064. Options.Filter = new TStringList();
  5065. Options.Filter->CaseSensitive = false;
  5066. Options.Filter->Duplicates = Types::dupAccept;
  5067. if (DirView(osRemote)->SelCount > 0)
  5068. {
  5069. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5070. }
  5071. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5072. {
  5073. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5074. }
  5075. Options.Filter->Sort();
  5076. }
  5077. }
  5078. //---------------------------------------------------------------------------
  5079. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5080. {
  5081. TCopyParamType Defaults;
  5082. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5083. ConfigStorage->AccessMode = smRead;
  5084. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false, false))
  5085. {
  5086. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5087. }
  5088. ConfigStorage.reset(NULL);
  5089. std::unique_ptr<TStringList> Options(new TStringList());
  5090. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5091. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5092. UnicodeString Result;
  5093. if (Options->Count > 0)
  5094. {
  5095. Result =
  5096. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5097. }
  5098. return Result;
  5099. }
  5100. //---------------------------------------------------------------------------
  5101. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5102. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5103. {
  5104. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5105. UnicodeString AdditionalParams =
  5106. FORMAT(L"%s \"%s\" \"%s\" %d %d %s%s", (
  5107. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5108. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options,
  5109. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5110. SerializeCopyParamForCommandLine(CopyParams)));
  5111. ExecuteNewInstance(SessionName, AdditionalParams);
  5112. }
  5113. //---------------------------------------------------------------------------
  5114. struct TSynchronizeParams
  5115. {
  5116. UnicodeString LocalDirectory;
  5117. UnicodeString RemoteDirectory;
  5118. TSynchronizeMode Mode;
  5119. TSynchronizeChecklist * Checklist;
  5120. int Params;
  5121. TCopyParamType * CopyParam;
  5122. TDateTime CollectElapsed;
  5123. TDateTime * StartTime;
  5124. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5125. };
  5126. //---------------------------------------------------------------------------
  5127. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5128. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5129. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5130. {
  5131. DebugAssert(!FAutoOperation);
  5132. void * BatchStorage;
  5133. BatchStart(BatchStorage);
  5134. FAutoOperation = true;
  5135. TFileOperationStatistics Statistics;
  5136. TDateTime Start = Now();
  5137. try
  5138. {
  5139. Params.OnProcessedItem = OnProcessedItem;
  5140. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5141. CreateProgressForm(&SynchronizeProgress);
  5142. Terminal->SynchronizeApply(
  5143. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5144. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5145. &Statistics);
  5146. }
  5147. __finally
  5148. {
  5149. Params.OnProcessedItem = NULL;
  5150. FAutoOperation = false;
  5151. DestroyProgressForm();
  5152. BatchEnd(BatchStorage);
  5153. ReloadLocalDirectory();
  5154. }
  5155. if (WinConfiguration->SynchronizeSummary)
  5156. {
  5157. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5158. // The statistics should be 0 anyway in this case
  5159. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5160. {
  5161. Message += L"\n";
  5162. if (Statistics.FilesUploaded > 0)
  5163. {
  5164. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5165. }
  5166. if (Statistics.FilesDownloaded > 0)
  5167. {
  5168. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5169. }
  5170. if (Statistics.FilesDeletedLocal > 0)
  5171. {
  5172. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5173. }
  5174. if (Statistics.FilesDeletedRemote > 0)
  5175. {
  5176. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5177. }
  5178. }
  5179. TDateTime Elapsed = (Now() - Start);
  5180. Message +=
  5181. L"\n" +
  5182. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5183. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5184. TMessageParams Params(mpNeverAskAgainCheck);
  5185. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5186. if (Result == qaNeverAskAgain)
  5187. {
  5188. WinConfiguration->SynchronizeSummary = false;
  5189. }
  5190. }
  5191. }
  5192. //---------------------------------------------------------------------------
  5193. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5194. {
  5195. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5196. {
  5197. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5198. }
  5199. // Not set in keep-up-to-date mode - Synchronize() method
  5200. if (Token != NULL)
  5201. {
  5202. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5203. if (Params.OnProcessedItem != NULL)
  5204. {
  5205. Params.OnProcessedItem(NULL, ChecklistItem);
  5206. }
  5207. }
  5208. }
  5209. //---------------------------------------------------------------------------
  5210. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5211. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5212. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5213. {
  5214. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5215. *Params.StartTime = Now();
  5216. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5217. }
  5218. //---------------------------------------------------------------------------
  5219. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5220. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5221. {
  5222. // terminal can be already closed (e.g. dropped connection)
  5223. if (Terminal != NULL)
  5224. {
  5225. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5226. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5227. }
  5228. }
  5229. //---------------------------------------------------------------------------
  5230. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5231. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5232. {
  5233. TAutoBatch AutoBatch(this);
  5234. TAutoFlag AutoOperationFlag(FAutoOperation);
  5235. if (Side == osRemote)
  5236. {
  5237. std::unique_ptr<TStrings> FileList(new TStringList());
  5238. FileList->AddObject(FileName, RemoteFile);
  5239. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5240. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5241. RemoteDirView->SaveSelection();
  5242. RemoteDirView->SaveSelectedNames();
  5243. try
  5244. {
  5245. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5246. }
  5247. catch(...)
  5248. {
  5249. RemoteDirView->DiscardSavedSelection();
  5250. throw;
  5251. }
  5252. RemoteDirView->RestoreSelection();
  5253. }
  5254. else if (DebugAlwaysTrue(Side == osLocal))
  5255. {
  5256. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5257. {
  5258. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5259. }
  5260. UnicodeString Directory = ExtractFileDir(FileName);
  5261. ReloadLocalDirectory(Directory);
  5262. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5263. if (!SamePaths(Directory, NewDirectory))
  5264. {
  5265. ReloadLocalDirectory(NewDirectory);
  5266. }
  5267. }
  5268. }
  5269. //---------------------------------------------------------------------------
  5270. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5271. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5272. const TCopyParamType * CopyParams)
  5273. {
  5274. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5275. UnicodeString AdditionalParams =
  5276. FORMAT(L"%s \"%s\" \"%s\" %d %d %s%s", (
  5277. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5278. LocalDirectory, RemoteDirectory, Mode, Params,
  5279. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5280. SerializeCopyParamForCommandLine(CopyParams)));
  5281. ExecuteNewInstance(SessionName, AdditionalParams);
  5282. }
  5283. //---------------------------------------------------------------------------
  5284. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5285. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5286. TSynchronizeMode & Mode, int Params, bool & SaveMode, bool UseDefaults)
  5287. {
  5288. int Result;
  5289. bool SaveSettings = false;
  5290. int Options =
  5291. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5292. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5293. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5294. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5295. bool Continue =
  5296. UseDefaults ||
  5297. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5298. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow);
  5299. if (Continue)
  5300. {
  5301. Configuration->Usage->Inc(L"Synchronizations");
  5302. UpdateCopyParamCounters(CopyParam);
  5303. TSynchronizeOptions SynchronizeOptions;
  5304. GetSynchronizeOptions(Params, SynchronizeOptions);
  5305. if (SaveSettings)
  5306. {
  5307. GUIConfiguration->SynchronizeParams = Params;
  5308. }
  5309. else
  5310. {
  5311. SaveMode = false;
  5312. }
  5313. TDateTime StartTime = Now();
  5314. TSynchronizeChecklist * Checklist = NULL;
  5315. try
  5316. {
  5317. DebugAssert(!FAutoOperation);
  5318. FAutoOperation = true;
  5319. try
  5320. {
  5321. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  5322. FSynchronizeProgressForm->Start();
  5323. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5324. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5325. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5326. &SynchronizeOptions);
  5327. }
  5328. __finally
  5329. {
  5330. FAutoOperation = false;
  5331. SAFE_DESTROY(FSynchronizeProgressForm);
  5332. }
  5333. TSynchronizeParams SynchronizeParams;
  5334. SynchronizeParams.LocalDirectory = LocalDirectory;
  5335. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5336. SynchronizeParams.Mode = Mode;
  5337. SynchronizeParams.CopyParam = &CopyParam;
  5338. SynchronizeParams.Params = Params;
  5339. SynchronizeParams.Checklist = Checklist;
  5340. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5341. SynchronizeParams.StartTime = &StartTime;
  5342. SynchronizeParams.OnProcessedItem = NULL;
  5343. Result = Checklist->Count;
  5344. if (Checklist->Count > 0)
  5345. {
  5346. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5347. {
  5348. if (!DoSynchronizeChecklistDialog(
  5349. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5350. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, &SynchronizeParams))
  5351. {
  5352. Result = -1;
  5353. }
  5354. }
  5355. else
  5356. {
  5357. FullSynchronize(SynchronizeParams, NULL, NULL);
  5358. }
  5359. }
  5360. else
  5361. {
  5362. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5363. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5364. }
  5365. }
  5366. __finally
  5367. {
  5368. delete Checklist;
  5369. }
  5370. OperationComplete(StartTime);
  5371. }
  5372. else
  5373. {
  5374. Result = -1;
  5375. }
  5376. return Result;
  5377. }
  5378. //---------------------------------------------------------------------------
  5379. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5380. const UnicodeString LocalDirectory, const UnicodeString RemoteDirectory,
  5381. bool & Continue, bool Collect)
  5382. {
  5383. if (Collect)
  5384. {
  5385. DebugAssert(FSynchronizeProgressForm != NULL);
  5386. if (!FSynchronizeProgressForm->Started)
  5387. {
  5388. FSynchronizeProgressForm->Start();
  5389. }
  5390. FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Continue);
  5391. }
  5392. else
  5393. {
  5394. DebugAssert(FSynchronizeProgressForm == NULL);
  5395. DebugAssert(FProgressForm != NULL);
  5396. }
  5397. }
  5398. //---------------------------------------------------------------------------
  5399. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5400. {
  5401. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  5402. TRemoteFile * File = NULL;
  5403. Terminal->ReadFile(FullFileName, File);
  5404. if (File != NULL)
  5405. {
  5406. std::unique_ptr<TRemoteFile> FileOwner(File);
  5407. TAutoFlag Flag(FStandaloneEditing);
  5408. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5409. Application->ShowMainForm = false;
  5410. Application->Run();
  5411. }
  5412. }
  5413. //---------------------------------------------------------------------------
  5414. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  5415. {
  5416. DebugFail();
  5417. }
  5418. //---------------------------------------------------------------------------
  5419. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5420. {
  5421. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5422. SessionData->Assign(Terminal->SessionData);
  5423. UpdateSessionData(SessionData.get());
  5424. Terminal->UpdateSessionCredentials(SessionData.get());
  5425. if (!Terminal->SessionData->HasSessionName())
  5426. {
  5427. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5428. // once IsWorkspace is cleared
  5429. SessionData->Name = UnicodeString();
  5430. SessionData->IsWorkspace = false;
  5431. }
  5432. return SessionData.release();
  5433. }
  5434. //---------------------------------------------------------------------------
  5435. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5436. {
  5437. TSessionData * SessionData = CloneCurrentSessionData();
  5438. try
  5439. {
  5440. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5441. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5442. }
  5443. __finally
  5444. {
  5445. delete SessionData;
  5446. }
  5447. }
  5448. //---------------------------------------------------------------------------
  5449. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5450. {
  5451. TTerminalManager * Manager = TTerminalManager::Instance();
  5452. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5453. if (DebugAlwaysTrue(Terminal != NULL))
  5454. {
  5455. // Update (Managed)Terminal->StateData
  5456. UpdateTerminal(Terminal);
  5457. }
  5458. Manager->SaveWorkspace(DataList.get());
  5459. return DataList.release();
  5460. }
  5461. //---------------------------------------------------------------------------
  5462. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5463. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5464. {
  5465. WinConfiguration->LastWorkspace = Name;
  5466. if (!SavePasswords)
  5467. {
  5468. for (int Index = 0; Index < DataList->Count; Index++)
  5469. {
  5470. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5471. if (SessionData->Link.IsEmpty())
  5472. {
  5473. SessionData->ClearSessionPasswords();
  5474. }
  5475. }
  5476. }
  5477. StoredSessions->NewWorkspace(Name, DataList);
  5478. // modified only
  5479. StoredSessions->Save(false, Explicit);
  5480. }
  5481. //---------------------------------------------------------------------------
  5482. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5483. {
  5484. return
  5485. DefaultStr(WinConfiguration->LastWorkspace,
  5486. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5487. }
  5488. //---------------------------------------------------------------------------
  5489. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5490. {
  5491. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5492. bool AnyNonStoredSessionWithPassword = false;
  5493. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5494. bool AllNonStoredSessionsAnonymous = true;
  5495. TTerminalManager * Manager = TTerminalManager::Instance();
  5496. for (int Index = 0; Index < DataList->Count; Index++)
  5497. {
  5498. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5499. if (SessionData->Link.IsEmpty())
  5500. {
  5501. if (SessionData->HasAnySessionPassword())
  5502. {
  5503. AnyNonStoredSessionWithPassword = true;
  5504. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  5505. {
  5506. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5507. }
  5508. }
  5509. if (!SameText(SessionData->UserName, AnonymousUserName))
  5510. {
  5511. AllNonStoredSessionsAnonymous = false;
  5512. }
  5513. }
  5514. }
  5515. bool SavePasswords;
  5516. bool * PSavePasswords;
  5517. bool NotRecommendedSavingPasswords =
  5518. !CustomWinConfiguration->UseMasterPassword &&
  5519. !AllNonStoredSessionsAnonymous;
  5520. if (Configuration->DisablePasswordStoring ||
  5521. !AnyNonStoredSessionWithPassword)
  5522. {
  5523. PSavePasswords = NULL;
  5524. SavePasswords = false;
  5525. }
  5526. else
  5527. {
  5528. PSavePasswords = &SavePasswords;
  5529. SavePasswords =
  5530. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5531. !NotRecommendedSavingPasswords;
  5532. }
  5533. UnicodeString Name = WorkspaceName();
  5534. bool CreateShortcut = false;
  5535. bool Result =
  5536. DoSaveWorkspaceDialog(
  5537. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5538. EnableAutoSave);
  5539. if (Result)
  5540. {
  5541. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5542. if (CreateShortcut)
  5543. {
  5544. TOperationVisualizer Visualizer;
  5545. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5546. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5547. }
  5548. if (EnableAutoSave)
  5549. {
  5550. WinConfiguration->AutoSaveWorkspace = true;
  5551. WinConfiguration->AutoWorkspace = Name;
  5552. if (PSavePasswords != NULL)
  5553. {
  5554. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5555. }
  5556. }
  5557. }
  5558. return Result;
  5559. }
  5560. //---------------------------------------------------------------------------
  5561. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TTerminal * Terminal)
  5562. {
  5563. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  5564. DebugAssert(ManagedTerminal != NULL);
  5565. SAFE_DESTROY(ManagedTerminal->RemoteExplorerState);
  5566. if (WinConfiguration->PreservePanelState)
  5567. {
  5568. ManagedTerminal->RemoteExplorerState = RemoteDirView->SaveState();
  5569. }
  5570. UpdateSessionData(ManagedTerminal->StateData);
  5571. }
  5572. //---------------------------------------------------------------------------
  5573. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5574. {
  5575. // Keep in sync with TSessionData::CopyStateData
  5576. DebugAssert(Data != NULL);
  5577. // This is inconsistent with how color (for example) is handled.
  5578. Data->Name = Terminal->SessionData->Name;
  5579. // cannot use RemoteDirView->Path, because it is empty if connection
  5580. // was already closed
  5581. // also only peek, we may not be connected at all atm
  5582. // so make sure we do not try retrieving current directory from the server
  5583. // (particularly with FTP)
  5584. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5585. if (!ACurrentDirectory.IsEmpty())
  5586. {
  5587. Data->RemoteDirectory = ACurrentDirectory;
  5588. }
  5589. Data->Color = SessionColor;
  5590. }
  5591. //---------------------------------------------------------------------------
  5592. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5593. {
  5594. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5595. DebugAssert(Toolbar != NULL);
  5596. for (int i = 0; i < Toolbar->Items->Count; i++)
  5597. {
  5598. TTBXCustomDropDownItem * DropDownItem;
  5599. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5600. if (DropDownItem != NULL)
  5601. {
  5602. ToolbarItemResize(DropDownItem,
  5603. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5604. Toolbar->NonClientWidth);
  5605. break;
  5606. }
  5607. }
  5608. }
  5609. //---------------------------------------------------------------------------
  5610. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5611. {
  5612. Item->EditWidth = Width;
  5613. }
  5614. //---------------------------------------------------------------------------
  5615. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5616. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5617. {
  5618. for (int i = 0; i < Toolbar->Items->Count; i++)
  5619. {
  5620. TTBXCustomDropDownItem * DropDownItem;
  5621. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5622. if (DropDownItem != NULL)
  5623. {
  5624. ASize.x -= DropDownItem->EditWidth;
  5625. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5626. }
  5627. }
  5628. }
  5629. //---------------------------------------------------------------------------
  5630. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5631. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5632. {
  5633. if (WinConfiguration->DDWarnLackOfTempSpace)
  5634. {
  5635. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5636. if (!ADrive.IsEmpty())
  5637. {
  5638. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5639. DebugAssert(RequiredSpace >= 0);
  5640. __int64 RequiredWithReserve;
  5641. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5642. if (FreeSpace < RequiredWithReserve)
  5643. {
  5644. unsigned int Result;
  5645. TMessageParams Params(mpNeverAskAgainCheck);
  5646. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5647. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5648. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5649. if (Result == qaNeverAskAgain)
  5650. {
  5651. WinConfiguration->DDWarnLackOfTempSpace = false;
  5652. }
  5653. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5654. }
  5655. }
  5656. }
  5657. }
  5658. //---------------------------------------------------------------------------
  5659. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5660. {
  5661. DoOpenDirectoryDialog(odAddBookmark, Side);
  5662. }
  5663. //---------------------------------------------------------------------------
  5664. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5665. {
  5666. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5667. TStringList * VisitedDirectories = CreateSortedStringList((Side == osRemote));
  5668. try
  5669. {
  5670. TCustomDirView * DView = DirView(Side);
  5671. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5672. {
  5673. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5674. }
  5675. }
  5676. catch (...)
  5677. {
  5678. delete VisitedDirectories;
  5679. throw;
  5680. }
  5681. return VisitedDirectories;
  5682. }
  5683. //---------------------------------------------------------------------------
  5684. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5685. TOpenDirectoryMode Mode, TOperationSide Side)
  5686. {
  5687. bool Continue = true;
  5688. if (Mode == odAddBookmark)
  5689. {
  5690. TMessageParams Params(mpNeverAskAgainCheck);
  5691. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5692. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5693. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  5694. unsigned int Answer =
  5695. MessageDialog(Message,
  5696. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  5697. if (Answer == qaNeverAskAgain)
  5698. {
  5699. Continue = true;
  5700. WinConfiguration->UseSharedBookmarks = true;
  5701. }
  5702. else if (Answer == qaOK)
  5703. {
  5704. Continue = true;
  5705. WinConfiguration->UseSharedBookmarks = false;
  5706. }
  5707. else
  5708. {
  5709. Continue = false;
  5710. }
  5711. }
  5712. if (Continue)
  5713. {
  5714. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  5715. try
  5716. {
  5717. UnicodeString Name = DirView(Side)->PathName;
  5718. bool Repeat;
  5719. do
  5720. {
  5721. Repeat = false;
  5722. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  5723. // do not allow switching to location profiles,
  5724. // if we are not connected
  5725. HasDirView[osLocal] && (Terminal != NULL)))
  5726. {
  5727. TWindowLock Lock(this);
  5728. if (!TryOpenDirectory(Side, Name))
  5729. {
  5730. Repeat = true;
  5731. Mode = odBrowse;
  5732. }
  5733. }
  5734. }
  5735. while (Repeat);
  5736. }
  5737. __finally
  5738. {
  5739. delete VisitedDirectories;
  5740. }
  5741. }
  5742. }
  5743. //---------------------------------------------------------------------------
  5744. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  5745. {
  5746. DebugAssert(!FTerminal->CommandSessionOpened);
  5747. return TTerminalManager::Instance()->ConnectTerminal(FTerminal->CommandSession);
  5748. }
  5749. //---------------------------------------------------------------------------
  5750. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  5751. {
  5752. bool Result = FTerminal->IsCapable[Capability] ||
  5753. FTerminal->CommandSessionOpened;
  5754. if (!Result)
  5755. {
  5756. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  5757. if (!GUIConfiguration->ConfirmCommandSession)
  5758. {
  5759. Result = true;
  5760. }
  5761. else
  5762. {
  5763. TMessageParams Params(mpNeverAskAgainCheck);
  5764. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  5765. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  5766. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  5767. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  5768. if (Answer == qaNeverAskAgain)
  5769. {
  5770. GUIConfiguration->ConfirmCommandSession = false;
  5771. Result = true;
  5772. }
  5773. else if (Answer == qaOK)
  5774. {
  5775. Result = true;
  5776. }
  5777. }
  5778. if (Result)
  5779. {
  5780. Result = CommandSessionFallback();
  5781. }
  5782. }
  5783. return Result;
  5784. }
  5785. //---------------------------------------------------------------------------
  5786. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  5787. {
  5788. if (EnsureCommandSessionFallback(fcAnyCommand))
  5789. {
  5790. DoConsoleDialog(Terminal, Command);
  5791. }
  5792. }
  5793. //---------------------------------------------------------------------------
  5794. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  5795. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  5796. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  5797. {
  5798. if (IsFileControl(DropSourceControl, osRemote) &&
  5799. (FDDExtMapFile != NULL))
  5800. {
  5801. Accept = true;
  5802. }
  5803. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  5804. DebugAssert(FDDTargetControl != NULL);
  5805. }
  5806. //---------------------------------------------------------------------------
  5807. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  5808. _di_IDataObject DataObj, int KeyState,
  5809. const TPoint & Point, int & Effect, bool & Accept)
  5810. {
  5811. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  5812. }
  5813. //---------------------------------------------------------------------------
  5814. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  5815. _di_IDataObject DataObj, int KeyState,
  5816. const TPoint & Point, int & Effect, bool & Accept)
  5817. {
  5818. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  5819. }
  5820. //---------------------------------------------------------------------------
  5821. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  5822. TObject * Sender)
  5823. {
  5824. DebugUsedParam(Sender);
  5825. DebugAssert(FDDTargetControl == Sender);
  5826. FDDTargetControl = NULL;
  5827. }
  5828. //---------------------------------------------------------------------------
  5829. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  5830. {
  5831. FileControlDDDragLeave(SessionsPageControl);
  5832. }
  5833. //---------------------------------------------------------------------------
  5834. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  5835. {
  5836. FileControlDDDragLeave(QueueView3);
  5837. }
  5838. //---------------------------------------------------------------------------
  5839. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  5840. {
  5841. DebugAssert(GetSide(Side) == osRemote);
  5842. DebugUsedParam(Side);
  5843. bool Edit = false;
  5844. TRemoteFile * File = NULL;
  5845. UnicodeString FileName;
  5846. UnicodeString PointTo;
  5847. bool SymbolicLink = true;
  5848. if (RemoteDirView->ItemFocused)
  5849. {
  5850. DebugAssert(RemoteDirView->ItemFocused->Data);
  5851. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  5852. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  5853. if (Edit)
  5854. {
  5855. FileName = File->FileName;
  5856. PointTo = File->LinkTo;
  5857. }
  5858. else
  5859. {
  5860. PointTo = File->FileName;
  5861. }
  5862. }
  5863. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  5864. Terminal->IsCapable[fcHardLink]))
  5865. {
  5866. Configuration->Usage->Inc(L"RemoteLinksCreated");
  5867. if (Edit)
  5868. {
  5869. DebugAssert(File->FileName == FileName);
  5870. int Params = dfNoRecursive;
  5871. Terminal->ExceptionOnFail = true;
  5872. try
  5873. {
  5874. Terminal->DeleteFile(L"", File, &Params);
  5875. }
  5876. __finally
  5877. {
  5878. Terminal->ExceptionOnFail = false;
  5879. }
  5880. }
  5881. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  5882. }
  5883. }
  5884. //---------------------------------------------------------------------------
  5885. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  5886. {
  5887. return
  5888. (Terminal != NULL) &&
  5889. ((GetSide(Side) != osRemote) ||
  5890. (Terminal->ResolvingSymlinks &&
  5891. Terminal->IsCapable[fcSymbolicLink]));
  5892. }
  5893. //---------------------------------------------------------------------------
  5894. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  5895. {
  5896. return
  5897. (FCurrentSide == osRemote) &&
  5898. (RemoteDirView->ItemFocused != NULL) &&
  5899. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  5900. Terminal->SessionData->ResolveSymlinks;
  5901. }
  5902. //---------------------------------------------------------------------------
  5903. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  5904. {
  5905. DebugAssert(!WinConfiguration->DisableOpenEdit);
  5906. FForceExecution = true;
  5907. try
  5908. {
  5909. DirView(osCurrent)->ExecuteCurrentFile();
  5910. }
  5911. __finally
  5912. {
  5913. FForceExecution = false;
  5914. }
  5915. }
  5916. //---------------------------------------------------------------------------
  5917. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  5918. {
  5919. TEditorData ExternalEditor;
  5920. ExternalEditor.Editor = edExternal;
  5921. bool Remember = false;
  5922. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc,
  5923. (GetSide(osCurrent) == osRemote)))
  5924. {
  5925. if (Remember)
  5926. {
  5927. TEditorList * EditorList = new TEditorList;
  5928. try
  5929. {
  5930. *EditorList = *WinConfiguration->EditorList;
  5931. bool Found = false;
  5932. int i = 0;
  5933. while (!Found && (i < EditorList->Count))
  5934. {
  5935. const TEditorPreferences * Editor = EditorList->Editors[i];
  5936. if ((Editor->Data->Editor == edExternal) &&
  5937. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  5938. {
  5939. Found = true;
  5940. }
  5941. i++;
  5942. }
  5943. if (!Found)
  5944. {
  5945. EditorList->Add(new TEditorPreferences(ExternalEditor));
  5946. WinConfiguration->EditorList = EditorList;
  5947. }
  5948. }
  5949. __finally
  5950. {
  5951. delete EditorList;
  5952. }
  5953. }
  5954. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  5955. }
  5956. }
  5957. //---------------------------------------------------------------------------
  5958. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  5959. {
  5960. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  5961. if (FFileFindTerminal == Sender)
  5962. {
  5963. FFileFindTerminal = NULL;
  5964. HideFileFindDialog();
  5965. }
  5966. if (FClipboardTerminal == Sender)
  5967. {
  5968. ClipboardClear(); // implies ClipboardStop
  5969. }
  5970. }
  5971. //---------------------------------------------------------------------------
  5972. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  5973. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  5974. {
  5975. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  5976. DebugAssert(Terminal != NULL);
  5977. if (Data->Terminal == Terminal)
  5978. {
  5979. Data->Terminal = NULL;
  5980. }
  5981. }
  5982. //---------------------------------------------------------------------------
  5983. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  5984. {
  5985. UpdateControls();
  5986. SessionColor = TColor(0);
  5987. UpdateRemotePathComboBox(false);
  5988. NeedSession(true);
  5989. }
  5990. //---------------------------------------------------------------------------
  5991. void __fastcall TCustomScpExplorerForm::NeedSession(bool ReloadSessions)
  5992. {
  5993. try
  5994. {
  5995. try
  5996. {
  5997. TTerminalManager::Instance()->NewSession(false, L"", ReloadSessions, this);
  5998. }
  5999. __finally
  6000. {
  6001. if (!WinConfiguration->KeepOpenWhenNoSession &&
  6002. (!Terminal || !Terminal->Active))
  6003. {
  6004. TerminateApplication();
  6005. }
  6006. }
  6007. }
  6008. catch (EAbort &)
  6009. {
  6010. // swallow
  6011. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6012. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6013. }
  6014. }
  6015. //---------------------------------------------------------------------------
  6016. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged()
  6017. {
  6018. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  6019. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6020. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  6021. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6022. try
  6023. {
  6024. while ((SessionsPageControl->PageCount > TerminalList->Count + 1) ||
  6025. // Clear the design time unthemed tab
  6026. ((SessionsPageControl->PageCount > 0) &&
  6027. (dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[SessionsPageControl->PageCount - 1]) == NULL)))
  6028. {
  6029. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6030. }
  6031. for (int Index = 0; Index <= TerminalList->Count; Index++)
  6032. {
  6033. TThemeTabSheet * TabSheet;
  6034. if (Index >= SessionsPageControl->PageCount)
  6035. {
  6036. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6037. TabSheet->PageControl = SessionsPageControl;
  6038. }
  6039. else
  6040. {
  6041. TabSheet = DebugNotNull(dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[Index]));
  6042. }
  6043. bool IsSessionTab = (Index < TerminalList->Count);
  6044. if (IsSessionTab)
  6045. {
  6046. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  6047. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6048. UpdateSessionTab(TabSheet);
  6049. }
  6050. else
  6051. {
  6052. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  6053. TabSheet->Tag = 0; // not really needed
  6054. // We know that we are at the last page, sotherwise we could not call this (it assumes that new session tab is the last one)
  6055. UpdateNewSessionTab();
  6056. }
  6057. TabSheet->ShowCloseButton = IsSessionTab;
  6058. }
  6059. }
  6060. __finally
  6061. {
  6062. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6063. }
  6064. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  6065. }
  6066. //---------------------------------------------------------------------------
  6067. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  6068. {
  6069. DoTerminalListChanged();
  6070. }
  6071. //---------------------------------------------------------------------------
  6072. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  6073. {
  6074. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6075. DebugAssert(TabSheet->ImageIndex == 0);
  6076. TabSheet->Caption =
  6077. WinConfiguration->SelectiveToolbarText ?
  6078. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  6079. UnicodeString();
  6080. }
  6081. //---------------------------------------------------------------------------
  6082. TTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  6083. {
  6084. return reinterpret_cast<TTerminal *>(TabSheet->Tag);
  6085. }
  6086. //---------------------------------------------------------------------------
  6087. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  6088. {
  6089. if (DebugAlwaysTrue(TabSheet != NULL))
  6090. {
  6091. TManagedTerminal * ManagedTerminal =
  6092. dynamic_cast<TManagedTerminal *>(GetSessionTabTerminal(TabSheet));
  6093. if (DebugAlwaysTrue(ManagedTerminal != NULL))
  6094. {
  6095. TColor Color = (ManagedTerminal == FTerminal) ? FSessionColor : ManagedTerminal->StateData->Color;
  6096. TabSheet->ImageIndex = AddSessionColor(Color);
  6097. UnicodeString TabCaption = TTerminalManager::Instance()->GetTerminalTitle(ManagedTerminal, true);
  6098. TabSheet->Caption = SessionsPageControl->FormatCaptionWithCloseButton(TabCaption);
  6099. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  6100. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  6101. {
  6102. ThemeTabSheet->Shadowed = !ManagedTerminal->Active;
  6103. }
  6104. }
  6105. }
  6106. }
  6107. //---------------------------------------------------------------------------
  6108. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6109. {
  6110. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6111. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  6112. bool Result = (Terminal != NULL);
  6113. if (Result)
  6114. {
  6115. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6116. }
  6117. else
  6118. {
  6119. try
  6120. {
  6121. NewSession(false);
  6122. }
  6123. __finally
  6124. {
  6125. DoTerminalListChanged();
  6126. }
  6127. FSessionsPageControlNewSessionTime = Now();
  6128. }
  6129. return Result;
  6130. }
  6131. //---------------------------------------------------------------------------
  6132. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6133. {
  6134. SessionTabSwitched();
  6135. }
  6136. //---------------------------------------------------------------------------
  6137. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6138. {
  6139. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6140. DebugAssert(TransferList != NULL);
  6141. UnicodeString Name;
  6142. if (TransferList->ItemIndex <= 0)
  6143. {
  6144. Name = L"";
  6145. }
  6146. else
  6147. {
  6148. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6149. }
  6150. if (FCopyParamAutoSelected.IsEmpty())
  6151. {
  6152. // if previous preset was not autoselected, make new preset the "default"
  6153. FCopyParamDefault = Name;
  6154. }
  6155. GUIConfiguration->CopyParamCurrent = Name;
  6156. }
  6157. //---------------------------------------------------------------------------
  6158. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6159. {
  6160. // sanity check
  6161. bool ExistingPreset =
  6162. (FTransferListHoverIndex >= 0) &&
  6163. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6164. DebugAssert(ExistingPreset);
  6165. if (ExistingPreset)
  6166. {
  6167. HDC DC = GetDC(0);
  6168. TCanvas * Canvas = new TCanvas();
  6169. try
  6170. {
  6171. Canvas->Handle = DC;
  6172. Canvas->Font = GetToolbarFont(this);
  6173. UnicodeString Name;
  6174. if (FTransferListHoverIndex == 0)
  6175. {
  6176. Name = L"";
  6177. }
  6178. else
  6179. {
  6180. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6181. }
  6182. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6183. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6184. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6185. static_cast<TObject*>(GetComponent(fcTransferList)));
  6186. UnicodeString InfoStr =
  6187. GUIConfiguration->CopyParamPreset[Name].
  6188. GetInfoStr(L"; ",
  6189. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General));
  6190. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6191. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6192. {
  6193. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6194. {
  6195. InfoStr.SetLength(InfoStr.Length() - 1);
  6196. }
  6197. InfoStr += Ellipsis;
  6198. }
  6199. // UpdateCaption does not cause invalidation of whole submenu, while
  6200. // setting Caption property does.
  6201. // also it probably does not resize the label, even if necessary
  6202. // (we do not want that anyway)
  6203. TransferLabel->UpdateCaption(InfoStr);
  6204. }
  6205. __finally
  6206. {
  6207. Canvas->Handle = NULL;
  6208. ReleaseDC(0, DC);
  6209. delete Canvas;
  6210. }
  6211. }
  6212. }
  6213. //---------------------------------------------------------------------------
  6214. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6215. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6216. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6217. {
  6218. if (FTransferListHoverIndex != AHoverIndex)
  6219. {
  6220. FTransferListHoverIndex = AHoverIndex;
  6221. UpdateTransferLabel();
  6222. }
  6223. }
  6224. //---------------------------------------------------------------------------
  6225. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6226. {
  6227. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6228. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6229. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6230. {
  6231. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6232. {
  6233. OpenDirectory(GetSide(osCurrent));
  6234. Message.Result = 1;
  6235. }
  6236. else
  6237. {
  6238. TForm::Dispatch(&Message);
  6239. }
  6240. }
  6241. else
  6242. {
  6243. TForm::Dispatch(&Message);
  6244. }
  6245. }
  6246. //---------------------------------------------------------------------------
  6247. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6248. {
  6249. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6250. if ((FIgnoreNextDialogChar != 0) &&
  6251. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6252. {
  6253. Message.Result = 1;
  6254. }
  6255. else
  6256. {
  6257. TForm::Dispatch(&Message);
  6258. }
  6259. FIgnoreNextDialogChar = 0;
  6260. }
  6261. //---------------------------------------------------------------------------
  6262. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6263. {
  6264. // The four low-order bits are used internally by Windows
  6265. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6266. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6267. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6268. // 61730 - restore thru double click - undocumented
  6269. // 61490 - maximize thru double click - undocumented
  6270. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6271. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6272. {
  6273. SysResizing(Cmd);
  6274. }
  6275. TForm::Dispatch(&Message);
  6276. }
  6277. //---------------------------------------------------------------------------
  6278. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6279. {
  6280. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6281. // Also there no point returning TRUE as we are not able to
  6282. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6283. // Hence the process termination might be safer :)
  6284. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6285. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6286. {
  6287. Message.Result = FALSE;
  6288. }
  6289. else
  6290. {
  6291. Message.Result = TRUE;
  6292. }
  6293. // Do not call default handling as that triggers OnCloseQuery,
  6294. // where our implementation will popup confirmation dialogs, what we do not want,
  6295. // as per Vista guidelines:
  6296. // https://docs.microsoft.com/en-us/windows/desktop/shutdown/shutting-down
  6297. }
  6298. //---------------------------------------------------------------------------
  6299. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6300. {
  6301. if (Message.EndSession && IsApplicationMinimized())
  6302. {
  6303. // WORKAROUND
  6304. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6305. // when it receives WM_ENDSESSION,
  6306. // but that sometimes (particularly when application is minimized) cause crashes.
  6307. // Crash popups message that blocks log off.
  6308. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6309. // so we call ExitProcess() immediately, not even trying to cleanup.
  6310. // It still causes beep, so there's likely some popup, but it does not block
  6311. // log off.
  6312. ExitProcess(0);
  6313. }
  6314. TForm::Dispatch(&Message);
  6315. }
  6316. #ifdef _DEBUG
  6317. //---------------------------------------------------------------------------
  6318. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6319. {
  6320. TForm::Dispatch(&Message);
  6321. }
  6322. #endif
  6323. //---------------------------------------------------------------------------
  6324. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6325. {
  6326. }
  6327. //---------------------------------------------------------------------------
  6328. void __fastcall TCustomScpExplorerForm::DoShow()
  6329. {
  6330. // only now are the controls resized finally, so the size constraints
  6331. // will not conflict with possibly very small window size
  6332. RestoreFormParams();
  6333. // This indicates that the form was rescaled after parameters were loaded and
  6334. // as most probably the parameters were actually saved with the current form scaling
  6335. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6336. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6337. // and no rounding problems will occur.
  6338. // See also RestoreForm.
  6339. // (we should be safe to call RestoreParams unconditionally)
  6340. if (PixelsPerInch != Screen->PixelsPerInch)
  6341. {
  6342. RestoreParams();
  6343. }
  6344. FixControlsPlacement();
  6345. if (Position == poDefaultPosOnly)
  6346. {
  6347. CutFormToDesktop(this);
  6348. }
  6349. TForm::DoShow();
  6350. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6351. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6352. if (Terminal == NULL)
  6353. {
  6354. StartingDisconnected();
  6355. }
  6356. UpdatePixelsPerInchMainWindowCounter();
  6357. FShowing = true;
  6358. }
  6359. //---------------------------------------------------------------------------
  6360. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6361. {
  6362. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6363. }
  6364. //---------------------------------------------------------------------------
  6365. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  6366. {
  6367. DoTerminalListChanged();
  6368. }
  6369. //---------------------------------------------------------------------------
  6370. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6371. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6372. TNotifyEvent OnBalloonClick, TObject * UserData)
  6373. {
  6374. bool Do;
  6375. UnicodeString Message;
  6376. if (E == NULL)
  6377. {
  6378. Message = Str;
  6379. Do = true;
  6380. }
  6381. else
  6382. {
  6383. Do = ExceptionMessage(E, Message);
  6384. }
  6385. if (Do && WinConfiguration->BalloonNotifications)
  6386. {
  6387. UnicodeString Title;
  6388. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6389. {
  6390. Message = TrimLeft(Message);
  6391. }
  6392. else
  6393. {
  6394. Message = UnformatMessage(Message);
  6395. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6396. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6397. Title = LoadResourceString(Captions[Type]);
  6398. if (Terminal != NULL)
  6399. {
  6400. Title = FORMAT(L"%s - %s",
  6401. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), Title));
  6402. }
  6403. }
  6404. if (Seconds == 0)
  6405. {
  6406. Seconds = WinConfiguration->NotificationsTimeout;
  6407. }
  6408. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6409. }
  6410. }
  6411. //---------------------------------------------------------------------------
  6412. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6413. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6414. UnicodeString HelpKeyword, const TMessageParams * Params,
  6415. TTerminal * Terminal)
  6416. {
  6417. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6418. (FOnFeedSynchronizeError != NULL)) &&
  6419. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6420. {
  6421. if (FOnFeedSynchronizeError != NULL)
  6422. {
  6423. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6424. }
  6425. else
  6426. {
  6427. DebugAssert(FErrorList != NULL);
  6428. TStringList * MoreMessagesCopy = NULL;
  6429. if (MoreMessages)
  6430. {
  6431. MoreMessagesCopy = new TStringList();
  6432. MoreMessagesCopy->Assign(MoreMessages);
  6433. }
  6434. FErrorList->AddObject(Message, MoreMessagesCopy);
  6435. }
  6436. PopupTrayBalloon(Terminal, Message, Type);
  6437. return ContinueAnswer(Answers);
  6438. }
  6439. else
  6440. {
  6441. bool UseBalloon = FTrayIcon->Visible;
  6442. if (UseBalloon)
  6443. {
  6444. PopupTrayBalloon(Terminal, Message, Type);
  6445. }
  6446. unsigned int Result;
  6447. try
  6448. {
  6449. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6450. }
  6451. __finally
  6452. {
  6453. // cancel only balloon we popped up, otherwise we may cancel notification
  6454. // balloon that was there before the message dialog
  6455. // (such as "dd confirmation opt in balloon)
  6456. if (UseBalloon)
  6457. {
  6458. FTrayIcon->CancelBalloon();
  6459. }
  6460. }
  6461. return Result;
  6462. }
  6463. }
  6464. //---------------------------------------------------------------------------
  6465. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6466. TTerminal * Terminal, Exception * E)
  6467. {
  6468. if (FTrayIcon->Visible)
  6469. {
  6470. PopupTrayBalloon(Terminal, L"", qtError, E);
  6471. }
  6472. // particularly prevent opening new session from jump list,
  6473. // while exception is shown
  6474. NonVisualDataModule->StartBusy();
  6475. try
  6476. {
  6477. ShowExtendedExceptionEx(Terminal, E);
  6478. }
  6479. __finally
  6480. {
  6481. NonVisualDataModule->EndBusy();
  6482. FTrayIcon->CancelBalloon();
  6483. }
  6484. }
  6485. //---------------------------------------------------------------------------
  6486. void __fastcall TCustomScpExplorerForm::TerminalReady()
  6487. {
  6488. // cannot rely on active page being page for active terminal,
  6489. // as it can happen that active page is the "new session" page
  6490. // (e.g. when reconnecting active terminal, while login dialog
  6491. // invoked from "new session" page is modal)
  6492. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6493. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  6494. }
  6495. //---------------------------------------------------------------------------
  6496. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6497. TTerminal * Terminal, Exception * E)
  6498. {
  6499. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6500. if (!Terminal->Active)
  6501. {
  6502. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6503. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6504. {
  6505. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6506. }
  6507. }
  6508. }
  6509. //---------------------------------------------------------------------------
  6510. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6511. UnicodeString Message, TQueryType Type,
  6512. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6513. {
  6514. if ((E == NULL) ||
  6515. ExceptionMessage(E, Message))
  6516. {
  6517. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6518. if (Important)
  6519. {
  6520. Seconds *= 5;
  6521. }
  6522. UnicodeString NoteMessage(UnformatMessage(Message));
  6523. if (Terminal != NULL)
  6524. {
  6525. NoteMessage = FORMAT(L"%s: %s",
  6526. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), NoteMessage));
  6527. }
  6528. if (WinConfiguration->BalloonNotifications)
  6529. {
  6530. AddNote(NoteMessage);
  6531. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6532. }
  6533. else
  6534. {
  6535. FlashOnBackground();
  6536. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6537. }
  6538. }
  6539. }
  6540. //---------------------------------------------------------------------------
  6541. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6542. {
  6543. RestoreApp();
  6544. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6545. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6546. !NonVisualDataModule->Busy)
  6547. {
  6548. TTerminal * Terminal = TerminalNoteData->Terminal;
  6549. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6550. if (!ComponentVisible[fcQueueView])
  6551. {
  6552. ToggleQueueVisibility();
  6553. GoToQueue();
  6554. }
  6555. }
  6556. }
  6557. //---------------------------------------------------------------------------
  6558. void __fastcall TCustomScpExplorerForm::QueueEvent(TTerminal * ATerminal,
  6559. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6560. {
  6561. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  6562. UnicodeString Message;
  6563. TNotifyEvent OnClick = NULL;
  6564. TObject * UserData = NULL;
  6565. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6566. switch (Event)
  6567. {
  6568. case qeEmptyButMonitored:
  6569. if ((ATerminal != Terminal) || QueueInvisible)
  6570. {
  6571. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6572. OnClick = QueueEmptyNoteClicked;
  6573. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6574. TerminalNoteData->Terminal = ATerminal;
  6575. UserData = TerminalNoteData;
  6576. }
  6577. break;
  6578. case qeEmpty:
  6579. OperationComplete(ManagedTerminal->QueueOperationStart);
  6580. break;
  6581. case qePendingUserAction:
  6582. if ((ATerminal != Terminal) ||
  6583. (QueueInvisible && !IsQueueAutoPopup()))
  6584. {
  6585. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6586. }
  6587. break;
  6588. default:
  6589. DebugFail();
  6590. }
  6591. if (!Message.IsEmpty())
  6592. {
  6593. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6594. }
  6595. }
  6596. //---------------------------------------------------------------------------
  6597. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6598. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6599. {
  6600. if (FDDExtMapFile != NULL)
  6601. {
  6602. CloseHandle(FDDExtMapFile);
  6603. FDDExtMapFile = NULL;
  6604. }
  6605. if (WinConfiguration->DDFakeFile)
  6606. {
  6607. DDFakeFileInitDrag(FileList, Created);
  6608. }
  6609. }
  6610. //---------------------------------------------------------------------------
  6611. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  6612. {
  6613. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  6614. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  6615. {
  6616. FFakeFileDropTarget = FileName;
  6617. }
  6618. }
  6619. //---------------------------------------------------------------------------
  6620. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  6621. {
  6622. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6623. if (!ForceDirectories(ApiPath(Result)))
  6624. {
  6625. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  6626. }
  6627. FileSetAttr(ApiPath(Result), faHidden);
  6628. return Result;
  6629. }
  6630. //---------------------------------------------------------------------------
  6631. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  6632. bool & Created)
  6633. {
  6634. FFakeFileDropTarget = UnicodeString();
  6635. FDragFakeDirectory = CreateFakeTransferDirectory();
  6636. FileList->AddItem(NULL, FDragFakeDirectory);
  6637. Created = true;
  6638. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  6639. {
  6640. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  6641. }
  6642. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6643. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6644. {
  6645. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6646. TDragExtCommStruct* CommStruct;
  6647. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6648. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6649. DebugAssert(CommStruct != NULL);
  6650. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  6651. CommStruct->Dragging = true;
  6652. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  6653. LENOF(CommStruct->DropDest));
  6654. NULL_TERMINATE(CommStruct->DropDest);
  6655. UnmapViewOfFile(CommStruct);
  6656. }
  6657. }
  6658. //---------------------------------------------------------------------------
  6659. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  6660. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  6661. {
  6662. bool Result;
  6663. if (Sender == RemoteDirView)
  6664. {
  6665. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  6666. }
  6667. else
  6668. {
  6669. DebugAssert(Sender == RemoteDriveView);
  6670. TStrings * FileList = RemoteDriveView->DragFileList();
  6671. try
  6672. {
  6673. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  6674. }
  6675. __finally
  6676. {
  6677. delete FileList;
  6678. }
  6679. }
  6680. if (FDDTargetControl == RemoteDriveView)
  6681. {
  6682. RemoteDriveView->UpdateDropTarget();
  6683. }
  6684. // foRemoteMove happens when file/dir is dragged within the remote tree view
  6685. // or from tree view to dir view.
  6686. // foMove happens when file/dir is dragged from remote tree view outside of
  6687. // application via dragex
  6688. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  6689. (DropSourceControl == RemoteDriveView))
  6690. {
  6691. RemoteDriveView->UpdateDropSource();
  6692. }
  6693. return Result;
  6694. }
  6695. //---------------------------------------------------------------------------
  6696. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  6697. {
  6698. // This also handles drops of remote files to queue.
  6699. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  6700. SAFE_DESTROY(FDDFileList);
  6701. if (!FFakeFileDropTarget.IsEmpty())
  6702. {
  6703. RemoveDir(ApiPath(FFakeFileDropTarget));
  6704. }
  6705. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  6706. {
  6707. try
  6708. {
  6709. TDragResult DDResult = (Sender == RemoteDirView) ?
  6710. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  6711. // Focus is moved to the target application,
  6712. // but as we are going to present the UI, we need to steal the focus back.
  6713. // This most likely won't work though (windows does not allow application
  6714. // to steal focus most of the time)
  6715. Application->BringToFront();
  6716. // On older version of Windows we never got drMove here, see also comment below.
  6717. // On Windows 10, we get the "move".
  6718. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  6719. {
  6720. UnicodeString TargetDirectory;
  6721. TFileOperation Operation;
  6722. // drInvalid may mean drMove, see comment below
  6723. switch (DDResult)
  6724. {
  6725. case drCopy:
  6726. Operation = foCopy;
  6727. break;
  6728. case drMove:
  6729. Operation = foMove;
  6730. break;
  6731. default:
  6732. DebugFail();
  6733. case drInvalid:
  6734. // prefer "copy" for safety
  6735. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  6736. break;
  6737. }
  6738. TTransferOperationParam Param;
  6739. UnicodeString CounterName;
  6740. bool ForceQueue;
  6741. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  6742. {
  6743. // we get drInvalid both if d&d was intercepted by ddext,
  6744. // and when users drops on no-drop location.
  6745. // we tell the difference by existence of response from ddext,
  6746. // so we ignore absence of response in this case
  6747. if (DDResult != drInvalid)
  6748. {
  6749. // here we know that the extension is installed,
  6750. // as it is checked as soon as drag&drop starts from
  6751. // RemoteFileControlDDCreateDragFileList
  6752. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  6753. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  6754. {
  6755. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  6756. }
  6757. else
  6758. {
  6759. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  6760. }
  6761. }
  6762. }
  6763. else
  6764. {
  6765. // download using fake file
  6766. Param.Temp = false;
  6767. Param.DragDrop = true;
  6768. if (ForceQueue)
  6769. {
  6770. Param.Queue = asOn;
  6771. }
  6772. if (Sender == RemoteDirView)
  6773. {
  6774. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  6775. }
  6776. if (RemoteFileControlFileOperation(Sender, Operation,
  6777. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  6778. {
  6779. Configuration->Usage->Inc(CounterName);
  6780. }
  6781. }
  6782. }
  6783. }
  6784. __finally
  6785. {
  6786. CloseHandle(FDDExtMapFile);
  6787. FDDExtMapFile = NULL;
  6788. delete FDragFakeMonitors;
  6789. FDragFakeMonitors = NULL;
  6790. RemoveDir(ApiPath(FDragFakeDirectory));
  6791. FDragFakeDirectory = L"";
  6792. FFakeFileDropTarget = UnicodeString();
  6793. }
  6794. }
  6795. if (FDragDropSshTerminate.get() != NULL)
  6796. {
  6797. FDragDropSshTerminate->Rethrow();
  6798. }
  6799. }
  6800. //---------------------------------------------------------------------------
  6801. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  6802. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  6803. {
  6804. // Remember drop effect so we know (when user drops files), if we copy or move
  6805. FLastDropEffect = dwEffect;
  6806. }
  6807. //---------------------------------------------------------------------------
  6808. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  6809. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  6810. {
  6811. bool Result;
  6812. if (FDDTargetControl == QueueView3)
  6813. {
  6814. Directory = DefaultDownloadTargetDirectory();
  6815. Result = true;
  6816. CounterName = L"DownloadsDragDropQueue";
  6817. ForceQueue = true;
  6818. }
  6819. else if (!FFakeFileDropTarget.IsEmpty())
  6820. {
  6821. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  6822. Result = true;
  6823. ForceQueue = false;
  6824. CounterName = L"DownloadsDragDropFakeFile";
  6825. }
  6826. else
  6827. {
  6828. ForceQueue = false;
  6829. Enabled = false;
  6830. try
  6831. {
  6832. int Timer = 0;
  6833. Result = false;
  6834. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  6835. {
  6836. {
  6837. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6838. TDragExtCommStruct* CommStruct;
  6839. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6840. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6841. DebugAssert(CommStruct != NULL);
  6842. Result = !CommStruct->Dragging;
  6843. if (Result)
  6844. {
  6845. Directory = ExtractFilePath(CommStruct->DropDest);
  6846. CounterName = L"DownloadsDragDropExternalExt";
  6847. }
  6848. UnmapViewOfFile(CommStruct);
  6849. }
  6850. if (!Result)
  6851. {
  6852. Sleep(50);
  6853. Timer += 50;
  6854. Application->ProcessMessages();
  6855. }
  6856. }
  6857. }
  6858. __finally
  6859. {
  6860. Enabled = true;
  6861. }
  6862. }
  6863. return Result;
  6864. }
  6865. //---------------------------------------------------------------------------
  6866. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  6867. const UnicodeString TempDir, int SecDelay)
  6868. {
  6869. TDateTime Alarm = Now() + (double)((double)SecDelay*OneMillisecond);
  6870. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  6871. if (FDelayedDeletionTimer == NULL)
  6872. {
  6873. DebugAssert(HandleAllocated());
  6874. FDelayedDeletionTimer = new TTimer(this);
  6875. FDelayedDeletionTimer->Interval = 10000;
  6876. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  6877. }
  6878. else
  6879. {
  6880. FDelayedDeletionTimer->Enabled = true;
  6881. }
  6882. }
  6883. //---------------------------------------------------------------------------
  6884. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  6885. {
  6886. DebugAssert(FDelayedDeletionList != NULL);
  6887. TDateTime N = Now();
  6888. TDateTime Alert;
  6889. UnicodeString Directory;
  6890. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  6891. {
  6892. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  6893. if ((N >= Alert) || (Sender == NULL))
  6894. {
  6895. Directory = FDelayedDeletionList->Strings[Index];
  6896. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  6897. {
  6898. FDelayedDeletionList->Delete(Index);
  6899. }
  6900. }
  6901. }
  6902. if (FDelayedDeletionList->Count == 0)
  6903. {
  6904. FDelayedDeletionTimer->Enabled = false;
  6905. }
  6906. }
  6907. //---------------------------------------------------------------------------
  6908. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  6909. {
  6910. if (IsFileControl(FDDTargetControl, osRemote) ||
  6911. (FDDTargetControl == SessionsPageControl))
  6912. {
  6913. TTerminal * TargetTerminal = NULL;
  6914. TFileOperation Operation = foNone;
  6915. if (FDDTargetControl == SessionsPageControl)
  6916. {
  6917. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  6918. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  6919. // do not allow dropping on the "+" tab
  6920. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  6921. if (TargetTerminal != NULL)
  6922. {
  6923. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  6924. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  6925. {
  6926. Operation = foRemoteMove;
  6927. }
  6928. else
  6929. {
  6930. Operation = foRemoteCopy;
  6931. }
  6932. }
  6933. }
  6934. else
  6935. {
  6936. if (FLastDropEffect == DROPEFFECT_MOVE)
  6937. {
  6938. Operation = foRemoteMove;
  6939. }
  6940. else if (FLastDropEffect == DROPEFFECT_COPY)
  6941. {
  6942. Operation = foRemoteCopy;
  6943. }
  6944. }
  6945. if (Operation != foNone)
  6946. {
  6947. RemoteFileControlFileOperation(DropSourceControl,
  6948. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  6949. }
  6950. // abort drag&drop
  6951. Abort();
  6952. }
  6953. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  6954. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  6955. (FDDTargetControl != QueueView3))
  6956. {
  6957. DebugAssert(!FDragTempDir.IsEmpty());
  6958. TTransferType Type;
  6959. UnicodeString TempDir = FDragTempDir;
  6960. // We clear FDragTempDir before calling
  6961. // just in case it fail (raises exception)
  6962. FDragTempDir = L"";
  6963. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  6964. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  6965. // empty directory parameter means temp directory -> don't display it!
  6966. UnicodeString TargetDir = L"";
  6967. int Options = 0;
  6968. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  6969. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  6970. {
  6971. Abort();
  6972. }
  6973. // TargetDir is set when dropped on local file control
  6974. // (this was workaround for legacy dirview event handling, now it should be
  6975. // made prettier)
  6976. if (TargetDir.IsEmpty())
  6977. {
  6978. TargetDir = TempDir;
  6979. if (ForceDirectories(ApiPath(TargetDir)))
  6980. {
  6981. DebugAssert(Terminal && !TargetDir.IsEmpty());
  6982. FPendingTempSpaceWarn = true;
  6983. try
  6984. {
  6985. FDragDropOperation = true;
  6986. int Params = cpTemporary |
  6987. (Type == ttMove ? cpDelete : 0);
  6988. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  6989. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  6990. }
  6991. __finally
  6992. {
  6993. FDragDropOperation = false;
  6994. FPendingTempSpaceWarn = false;
  6995. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  6996. }
  6997. }
  6998. else
  6999. {
  7000. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7001. }
  7002. }
  7003. }
  7004. }
  7005. //---------------------------------------------------------------------------
  7006. void __fastcall TCustomScpExplorerForm::DDDownload(
  7007. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  7008. {
  7009. TAutoBatch AutoBatch(this);
  7010. UpdateCopyParamCounters(*CopyParam);
  7011. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7012. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7013. {
  7014. RemoteDriveView->UpdateDropSource();
  7015. }
  7016. }
  7017. //---------------------------------------------------------------------------
  7018. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7019. {
  7020. public:
  7021. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7022. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7023. {
  7024. }
  7025. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7026. {
  7027. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7028. TDataObjectFilesEx::AllowData(FormatEtc);
  7029. }
  7030. };
  7031. //---------------------------------------------------------------------------
  7032. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7033. TObject * Sender, TDataObject *& DataObject)
  7034. {
  7035. if (FDDExtMapFile != NULL)
  7036. {
  7037. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7038. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7039. {
  7040. Abort();
  7041. }
  7042. if (FileList->Count > 0)
  7043. {
  7044. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7045. if (!FilesObject->IsValid(true, true))
  7046. {
  7047. FilesObject->_Release();
  7048. }
  7049. else
  7050. {
  7051. DataObject = FilesObject;
  7052. }
  7053. }
  7054. }
  7055. }
  7056. //---------------------------------------------------------------------------
  7057. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7058. {
  7059. DebugFail();
  7060. }
  7061. //---------------------------------------------------------------------------
  7062. void __fastcall TCustomScpExplorerForm::GoToTree()
  7063. {
  7064. ComponentVisible[fcRemoteTree] = true;
  7065. RemoteDriveView->SetFocus();
  7066. }
  7067. //---------------------------------------------------------------------------
  7068. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7069. TPanelExport Export)
  7070. {
  7071. TCustomDirView * DirView = this->DirView(Side);
  7072. std::unique_ptr<TStrings> ExportData(new TStringList());
  7073. switch (Export)
  7074. {
  7075. case pePath:
  7076. ExportData->Add(DirView->PathName);
  7077. break;
  7078. case peFileList:
  7079. case peFullFileList:
  7080. {
  7081. bool FullPath = (Export == peFullFileList);
  7082. DirView->CreateFileList(false, FullPath, ExportData.get());
  7083. for (int Index = 0; Index < ExportData->Count; Index++)
  7084. {
  7085. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7086. {
  7087. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7088. }
  7089. }
  7090. }
  7091. break;
  7092. default:
  7093. DebugFail();
  7094. }
  7095. return ExportData.release();
  7096. }
  7097. //---------------------------------------------------------------------------
  7098. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7099. TPanelExport Export, TPanelExportDestination Destination)
  7100. {
  7101. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7102. PanelExportStore(Side, Export, Destination, ExportData.get());
  7103. }
  7104. //---------------------------------------------------------------------------
  7105. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7106. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7107. TStrings * ExportData)
  7108. {
  7109. if (Destination == pedClipboard)
  7110. {
  7111. TInstantOperationVisualizer Visualizer;
  7112. CopyToClipboard(ExportData);
  7113. }
  7114. else
  7115. {
  7116. DebugFail();
  7117. }
  7118. }
  7119. //---------------------------------------------------------------------------
  7120. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7121. {
  7122. TCustomDirView * DirView = this->DirView(Side);
  7123. UnicodeString Mask = DirView->Mask;
  7124. if (DoFilterMaskDialog(DirView, Mask))
  7125. {
  7126. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7127. Configuration->Usage->Inc(L"Filters");
  7128. }
  7129. }
  7130. //---------------------------------------------------------------------------
  7131. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7132. {
  7133. return FQueueController->DefaultOperation();
  7134. }
  7135. //---------------------------------------------------------------------------
  7136. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7137. TQueueOperation Operation, void ** Param)
  7138. {
  7139. switch (Operation)
  7140. {
  7141. case qoPreferences:
  7142. return true;
  7143. case qoGoTo:
  7144. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7145. case qoOnceEmpty:
  7146. return !FQueueController->Empty;
  7147. default:
  7148. return FQueueController->AllowOperation(Operation, Param);
  7149. }
  7150. }
  7151. //---------------------------------------------------------------------------
  7152. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7153. {
  7154. if (DebugAlwaysTrue(QueueView3->Visible))
  7155. {
  7156. QueueView3->SetFocus();
  7157. }
  7158. }
  7159. //---------------------------------------------------------------------------
  7160. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7161. TQueueOperation Operation, void * Param)
  7162. {
  7163. if (Operation == qoGoTo)
  7164. {
  7165. GoToQueue();
  7166. }
  7167. else if (Operation == qoPreferences)
  7168. {
  7169. PreferencesDialog(pmQueue);
  7170. }
  7171. else
  7172. {
  7173. FQueueController->ExecuteOperation(Operation, Param);
  7174. }
  7175. }
  7176. //---------------------------------------------------------------------------
  7177. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7178. {
  7179. return (Queue != NULL) && Queue->Enabled;
  7180. }
  7181. //---------------------------------------------------------------------------
  7182. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7183. {
  7184. DebugAssert(Queue != NULL);
  7185. if (Queue != NULL)
  7186. {
  7187. Queue->Enabled = !Queue->Enabled;
  7188. }
  7189. }
  7190. //---------------------------------------------------------------------------
  7191. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7192. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7193. {
  7194. FQueueActedItem = QueueView3->ItemFocused;
  7195. }
  7196. //---------------------------------------------------------------------------
  7197. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7198. {
  7199. return TopDock->Height + QueueSplitter->Height;
  7200. }
  7201. //---------------------------------------------------------------------------
  7202. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7203. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7204. {
  7205. // when queue is hidden by double-clicking splitter, stray attempt to
  7206. // resize the panel with strange value arrives, make sure it is ignored
  7207. if (ComponentVisible[fcQueueView])
  7208. {
  7209. int HeightLimit = ClientHeight - GetStaticComponentsHeight() -
  7210. RemotePanel->Constraints->MinHeight;
  7211. if (NewSize > HeightLimit)
  7212. {
  7213. NewSize = HeightLimit;
  7214. }
  7215. }
  7216. else
  7217. {
  7218. Accept = false;
  7219. }
  7220. }
  7221. //---------------------------------------------------------------------------
  7222. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7223. TDragObject *& /*DragObject*/)
  7224. {
  7225. FQueueActedItem = QueueView3->ItemFocused;
  7226. }
  7227. //---------------------------------------------------------------------------
  7228. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7229. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7230. {
  7231. Accept = true;
  7232. if (Source == QueueView3)
  7233. {
  7234. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7235. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7236. if (Accept)
  7237. {
  7238. TQueueItemProxy * QueueItem;
  7239. TQueueItemProxy * DestQueueItem;
  7240. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7241. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7242. Accept = (QueueItem != DestQueueItem) &&
  7243. (QueueItem->Status == TQueueItem::qsPending) &&
  7244. (DestQueueItem->Status == TQueueItem::qsPending);
  7245. }
  7246. }
  7247. }
  7248. //---------------------------------------------------------------------------
  7249. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7250. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7251. {
  7252. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7253. {
  7254. TQueueItemProxy * QueueItem;
  7255. TQueueItemProxy * DestQueueItem;
  7256. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7257. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7258. QueueItem->Move(DestQueueItem);
  7259. }
  7260. }
  7261. //---------------------------------------------------------------------------
  7262. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7263. {
  7264. if ((QueueView3->ItemFocused == NULL) &&
  7265. (QueueView3->Items->Count > 0))
  7266. {
  7267. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7268. }
  7269. QueueLabelUpdateStatus();
  7270. }
  7271. //---------------------------------------------------------------------------
  7272. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7273. {
  7274. QueueLabelUpdateStatus();
  7275. }
  7276. //---------------------------------------------------------------------------
  7277. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7278. {
  7279. QueueLabel->UpdateStatus();
  7280. }
  7281. //---------------------------------------------------------------------------
  7282. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7283. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7284. {
  7285. if (Selected)
  7286. {
  7287. NonVisualDataModule->UpdateNonVisibleActions();
  7288. }
  7289. }
  7290. //---------------------------------------------------------------------------
  7291. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7292. {
  7293. TDragDropFilesEx * Result = NULL;
  7294. if (Sender == SessionsPageControl)
  7295. {
  7296. Result = FSessionsDragDropFilesEx;
  7297. }
  7298. else if (Sender == QueueView3)
  7299. {
  7300. Result = FQueueDragDropFilesEx;
  7301. }
  7302. else
  7303. {
  7304. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7305. if (DirView != NULL)
  7306. {
  7307. Result = DirView->DragDropFilesEx;
  7308. }
  7309. else
  7310. {
  7311. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7312. if (DriveView != NULL)
  7313. {
  7314. Result = DriveView->DragDropFilesEx;
  7315. }
  7316. }
  7317. }
  7318. DebugAssert(Result != NULL);
  7319. return Result;
  7320. }
  7321. //---------------------------------------------------------------------------
  7322. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7323. {
  7324. return (DView->SelCount == DView->FilesCount);
  7325. }
  7326. //---------------------------------------------------------------------------
  7327. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7328. {
  7329. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7330. }
  7331. //---------------------------------------------------------------------------
  7332. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7333. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7334. {
  7335. TFileOperation Operation;
  7336. switch (Effect)
  7337. {
  7338. case DROPEFFECT_MOVE:
  7339. Operation = foMove;
  7340. break;
  7341. case DROPEFFECT_COPY:
  7342. // occures on WinXP (reported by user)
  7343. default:
  7344. Operation = foCopy;
  7345. break;
  7346. };
  7347. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7348. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7349. if (DragDropFilesEx->FileList->Count > 0)
  7350. {
  7351. TStrings * FileList = new TStringList();
  7352. try
  7353. {
  7354. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7355. {
  7356. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7357. }
  7358. FDragDropOperation = true;
  7359. TTransferOperationParam Param;
  7360. Param.TargetDirectory = TargetPath;
  7361. // upload, no temp dirs
  7362. Param.Temp = false;
  7363. Param.DragDrop = DragDrop;
  7364. Param.Options =
  7365. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7366. if (ForceQueue)
  7367. {
  7368. Param.Queue = asOn;
  7369. }
  7370. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7371. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7372. {
  7373. if (IsFileControl(DropSourceControl, osLocal))
  7374. {
  7375. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7376. }
  7377. else
  7378. {
  7379. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7380. }
  7381. }
  7382. }
  7383. __finally
  7384. {
  7385. FDragDropOperation = false;
  7386. delete FileList;
  7387. }
  7388. }
  7389. }
  7390. //---------------------------------------------------------------------------
  7391. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7392. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7393. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7394. {
  7395. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7396. }
  7397. //---------------------------------------------------------------------------
  7398. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7399. TObject * Sender, int grfKeyState, int & dwEffect)
  7400. {
  7401. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7402. if ((dwEffect != DROPEFFECT_NONE) &&
  7403. IsFileControl(DropSourceControl, osRemote))
  7404. {
  7405. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7406. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7407. (RemoteDirView->DropTarget == NULL))
  7408. {
  7409. dwEffect = DROPEFFECT_NONE;
  7410. }
  7411. else
  7412. {
  7413. if (dwEffect == DROPEFFECT_COPY)
  7414. {
  7415. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  7416. // currently we support copying always (at least via temporary directory);
  7417. // remove associated checks once this all proves stable and working
  7418. bool CopyCapable = true;
  7419. // if we do not support neither of operations, there's no discussion
  7420. if (!MoveCapable && !CopyCapable)
  7421. {
  7422. dwEffect = DROPEFFECT_NONE;
  7423. }
  7424. else
  7425. {
  7426. // The default effect inside remote panel is move,
  7427. // unless we do not support it, but support copy
  7428. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7429. {
  7430. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  7431. }
  7432. else
  7433. {
  7434. // with ctrl-down, we want copy unless it is not supported
  7435. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  7436. }
  7437. }
  7438. }
  7439. }
  7440. }
  7441. }
  7442. //---------------------------------------------------------------------------
  7443. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7444. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7445. {
  7446. if (FDDTotalSize >= 0)
  7447. {
  7448. if (File->IsDirectory)
  7449. {
  7450. FDDTotalSize = -1;
  7451. }
  7452. else
  7453. {
  7454. FDDTotalSize += File->Size;
  7455. }
  7456. }
  7457. DebugAssert(!FDragTempDir.IsEmpty());
  7458. // TODO: this is quite ineffective
  7459. // TODO: what if invalid character replacement is disabled?
  7460. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7461. UnicodeString TransferFileName;
  7462. if (Sender == RemoteDriveView)
  7463. {
  7464. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7465. }
  7466. else
  7467. {
  7468. TransferFileName = File->FileName;
  7469. }
  7470. FDDFileList->AddObject(TransferFileName, File);
  7471. }
  7472. //---------------------------------------------------------------------------
  7473. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7474. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7475. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7476. {
  7477. // sometimes we do not get DDEnd so the list is not released
  7478. SAFE_DESTROY(FDDFileList);
  7479. FDDFileList = new TStringList();
  7480. FDragTempDir = WinConfiguration->TemporaryDir();
  7481. FDDTotalSize = 0;
  7482. FDragDropSshTerminate.reset(NULL);
  7483. }
  7484. //---------------------------------------------------------------------------
  7485. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7486. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7487. HRESULT & Result)
  7488. {
  7489. if (Result == DRAGDROP_S_DROP)
  7490. {
  7491. try
  7492. {
  7493. GlobalDragImageList->HideDragImage();
  7494. try
  7495. {
  7496. RemoteFileControlDDTargetDrop();
  7497. }
  7498. catch(ESshTerminate & E)
  7499. {
  7500. FDragDropSshTerminate.reset(E.Clone());
  7501. }
  7502. }
  7503. catch (Exception &E)
  7504. {
  7505. // If downloading fails we need to cancel drag&drop, otherwise
  7506. // Explorer shows error
  7507. // But by the way exception probably never reach this point as
  7508. // it's catched on way.
  7509. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7510. Result = DRAGDROP_S_CANCEL;
  7511. ShowExtendedException(Terminal, &E);
  7512. }
  7513. }
  7514. }
  7515. //---------------------------------------------------------------------------
  7516. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7517. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7518. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7519. {
  7520. TFileMasks::TParams MaskParams;
  7521. MaskParams.Size = Size;
  7522. MaskParams.Modification = Modification;
  7523. // this does not re-parse the mask if it is the same as the last time
  7524. FDirViewMatchMask = Masks;
  7525. bool ImplicitMatch;
  7526. Matches =
  7527. // RecurseInclude parameter has no effect as the Path is empty
  7528. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7529. (AllowImplicitMatches || !ImplicitMatch);
  7530. }
  7531. //---------------------------------------------------------------------------
  7532. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7533. TObject * Sender, TListItem * Item, WORD & Indexes)
  7534. {
  7535. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7536. UnicodeString Ext;
  7537. if (DirView == RemoteDirView)
  7538. {
  7539. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7540. }
  7541. else
  7542. {
  7543. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7544. }
  7545. if (SameText(Ext, Configuration->PartialExt))
  7546. {
  7547. Indexes |= oiPartial;
  7548. }
  7549. }
  7550. //---------------------------------------------------------------------------
  7551. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7552. {
  7553. return
  7554. DirViewEnabled(osCurrent) &&
  7555. DirView(osCurrent)->CanPasteFromClipBoard();
  7556. }
  7557. //---------------------------------------------------------------------------
  7558. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7559. {
  7560. bool Result = false;
  7561. if (CanPasteToDirViewFromClipBoard())
  7562. {
  7563. Result = true;
  7564. }
  7565. else
  7566. {
  7567. UnicodeString ClipboardText;
  7568. if (NonEmptyTextFromClipboard(ClipboardText))
  7569. {
  7570. if (StoredSessions->IsUrl(ClipboardText))
  7571. {
  7572. Result = true;
  7573. }
  7574. else
  7575. {
  7576. Result = DirViewEnabled(osCurrent);
  7577. }
  7578. }
  7579. }
  7580. return Result;
  7581. }
  7582. //---------------------------------------------------------------------------
  7583. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7584. {
  7585. if (DoesClipboardContainOurFiles())
  7586. {
  7587. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  7588. {
  7589. TTerminalManager * Manager = TTerminalManager::Instance();
  7590. TTerminal * TergetTerminal = Manager->ActiveTerminal;
  7591. Manager->ActiveTerminal = FClipboardTerminal;
  7592. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TergetTerminal);
  7593. }
  7594. }
  7595. else if (CanPasteToDirViewFromClipBoard())
  7596. {
  7597. DirView(osCurrent)->PasteFromClipBoard();
  7598. }
  7599. else
  7600. {
  7601. UnicodeString ClipboardText;
  7602. if (NonEmptyTextFromClipboard(ClipboardText))
  7603. {
  7604. if (StoredSessions->IsUrl(ClipboardText))
  7605. {
  7606. NewSession(false, ClipboardText);
  7607. }
  7608. else
  7609. {
  7610. DirView(osCurrent)->Path = ClipboardText;
  7611. }
  7612. }
  7613. }
  7614. }
  7615. //---------------------------------------------------------------------------
  7616. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  7617. {
  7618. // TBD
  7619. }
  7620. //---------------------------------------------------------------------------
  7621. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  7622. {
  7623. TCustomDirView * ADirView = DirView(Side);
  7624. ADirView->SelectAll(Mode);
  7625. ADirView->SetFocus();
  7626. }
  7627. //---------------------------------------------------------------------------
  7628. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  7629. {
  7630. TCustomDirView * ADirView = DirView(Side);
  7631. TFileFilter Filter;
  7632. DefaultFileFilter(Filter);
  7633. if (DoSelectMaskDialog(ADirView, Select, Filter))
  7634. {
  7635. Configuration->Usage->Inc(L"MaskSelections");
  7636. ADirView->SelectFiles(Filter, Select);
  7637. ADirView->SetFocus();
  7638. }
  7639. }
  7640. //---------------------------------------------------------------------------
  7641. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  7642. {
  7643. TCustomDirView * ADirView = DirView(Side);
  7644. ADirView->RestoreSelectedNames();
  7645. ADirView->SetFocus();
  7646. }
  7647. //---------------------------------------------------------------------------
  7648. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  7649. {
  7650. TCustomDirView * CurrentDirView = DirView(osCurrent);
  7651. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  7652. {
  7653. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  7654. UnicodeString Ext;
  7655. if (GetSide(osCurrent) == osRemote)
  7656. {
  7657. Ext = UnixExtractFileExt(FileName);
  7658. }
  7659. else
  7660. {
  7661. Ext = ExtractFileExt(FileName);
  7662. }
  7663. if (Ext.IsEmpty())
  7664. {
  7665. Ext = L".";
  7666. }
  7667. TFileFilter Filter;
  7668. Filter.Masks = FORMAT(L"*%s", (Ext));
  7669. Filter.Directories = false;
  7670. CurrentDirView->SelectFiles(Filter, Select);
  7671. }
  7672. }
  7673. //---------------------------------------------------------------------------
  7674. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  7675. const TStatusFileInfo & FileInfo, TOperationSide Side)
  7676. {
  7677. UnicodeString Result;
  7678. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  7679. {
  7680. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  7681. }
  7682. else if ((Side == osRemote) && (Terminal == NULL))
  7683. {
  7684. // noop
  7685. }
  7686. else
  7687. {
  7688. Result =
  7689. FMTLOAD(FILE_INFO_FORMAT,
  7690. (FormatBytes(FileInfo.SelectedSize),
  7691. FormatBytes(FileInfo.FilesSize),
  7692. FormatNumber(FileInfo.SelectedCount),
  7693. FormatNumber(FileInfo.FilesCount)));
  7694. }
  7695. return Result;
  7696. }
  7697. //---------------------------------------------------------------------------
  7698. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  7699. TTBXStatusPanel * Panel, TOperationSide Side)
  7700. {
  7701. if (Panel->Index == 1)
  7702. {
  7703. ToggleShowHiddenFiles();
  7704. }
  7705. else if (Panel->Index == 2)
  7706. {
  7707. Filter(Side);
  7708. }
  7709. }
  7710. //---------------------------------------------------------------------------
  7711. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  7712. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  7713. {
  7714. DebugAssert(!StatusBar->SimplePanel);
  7715. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  7716. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  7717. }
  7718. //---------------------------------------------------------------------------
  7719. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  7720. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  7721. {
  7722. DebugAssert(StatusBar->Panels->Count >= 3);
  7723. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  7724. if (FileInfo.HiddenCount > 0)
  7725. {
  7726. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  7727. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7728. }
  7729. else
  7730. {
  7731. HiddenFilesPanel->ViewPriority = 0;
  7732. // not really necessary, just to cleanup no-longer-valid data
  7733. HiddenFilesPanel->Caption = L"";
  7734. }
  7735. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  7736. if (FileInfo.FilteredCount > 0)
  7737. {
  7738. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  7739. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7740. }
  7741. else
  7742. {
  7743. FilteredFilesPanel->ViewPriority = 0;
  7744. // not really necessary, just to cleanup no-longer-valid data
  7745. FilteredFilesPanel->Caption = L"";
  7746. }
  7747. }
  7748. //---------------------------------------------------------------------------
  7749. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  7750. TObject * /*Sender*/)
  7751. {
  7752. if (RemoteDirView->Enabled)
  7753. {
  7754. RemoteDirView->SetFocus();
  7755. }
  7756. }
  7757. //---------------------------------------------------------------------------
  7758. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  7759. {
  7760. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  7761. switch (Config.Show)
  7762. {
  7763. case qvShow:
  7764. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  7765. {
  7766. Config.Show = qvHide;
  7767. }
  7768. else
  7769. {
  7770. Config.Show = Config.LastHideShow;
  7771. }
  7772. break;
  7773. case qvHideWhenEmpty:
  7774. if (ComponentVisible[fcQueueView])
  7775. {
  7776. Config.Show = qvHide;
  7777. }
  7778. else
  7779. {
  7780. Config.LastHideShow = Config.Show;
  7781. Config.Show = qvShow;
  7782. }
  7783. break;
  7784. case qvHide:
  7785. Config.LastHideShow = Config.Show;
  7786. Config.Show = qvShow;
  7787. break;
  7788. }
  7789. WinConfiguration->QueueView = Config;
  7790. }
  7791. //---------------------------------------------------------------------------
  7792. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  7793. {
  7794. UnicodeString Result;
  7795. if (FTerminal != NULL)
  7796. {
  7797. switch (WinConfiguration->PathInCaption)
  7798. {
  7799. case picShort:
  7800. Result = TTerminalManager::Instance()->GetTerminalShortPath(FTerminal);
  7801. break;
  7802. case picFull:
  7803. Result = FTerminal->CurrentDirectory;
  7804. break;
  7805. }
  7806. }
  7807. return Result;
  7808. }
  7809. //---------------------------------------------------------------------------
  7810. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  7811. {
  7812. TColor Result = GetWindowColor(FSessionColor);
  7813. return Result;
  7814. }
  7815. //---------------------------------------------------------------------------
  7816. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  7817. {
  7818. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  7819. return Result;
  7820. }
  7821. //---------------------------------------------------------------------------
  7822. void __fastcall TCustomScpExplorerForm::UpdateControls()
  7823. {
  7824. TTerminalManager::Instance()->UpdateAppTitle();
  7825. // WORAKRDOUND: Disabling list view when it is not showing yet does not set its
  7826. // background to gray on Windows 7 (works on Windows 10).
  7827. // See also EnableControl
  7828. if (Showing)
  7829. {
  7830. bool HasTerminal = (Terminal != NULL) && Terminal->Active;
  7831. if (HasTerminal)
  7832. {
  7833. // Update path when it changes
  7834. if ((SessionsPageControl->ActivePage != NULL) && (GetSessionTabTerminal(SessionsPageControl->ActivePage) == Terminal))
  7835. {
  7836. UpdateSessionTab(SessionsPageControl->ActivePage);
  7837. }
  7838. if (!RemoteDirView->Enabled)
  7839. {
  7840. RemoteDirView->Enabled = true;
  7841. if (FRemoteDirViewWasFocused)
  7842. {
  7843. ActiveControl = RemoteDirView;
  7844. }
  7845. }
  7846. RemoteDirView->Color = PanelColor();
  7847. }
  7848. else
  7849. {
  7850. if (RemoteDirView->Enabled)
  7851. {
  7852. // This is first called when the form is being constructed
  7853. // (not anymore due to Showing test above)
  7854. // but the false is overriden in the constructor later.
  7855. // An even later in TScpCommanderForm::DoShow()
  7856. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  7857. RemoteDirView->Enabled = false;
  7858. RemoteDirView->Color = DisabledPanelColor();
  7859. }
  7860. }
  7861. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  7862. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  7863. RemoteDriveView->Color = RemoteDirView->Color;
  7864. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  7865. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fsBackgroundTransfers];
  7866. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  7867. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  7868. QueueLabelUpdateStatus();
  7869. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  7870. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  7871. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  7872. }
  7873. }
  7874. //---------------------------------------------------------------------------
  7875. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  7876. {
  7877. UpdateControls();
  7878. }
  7879. //---------------------------------------------------------------------------
  7880. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  7881. TObject * Sender)
  7882. {
  7883. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7884. DebugAssert(DirView != NULL);
  7885. DoDirViewLoaded(DirView);
  7886. TransferPresetAutoSelect();
  7887. }
  7888. //---------------------------------------------------------------------------
  7889. void __fastcall TCustomScpExplorerForm::StartUpdates()
  7890. {
  7891. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  7892. // first run after installation
  7893. if (double(Updates.LastCheck) == 0)
  7894. {
  7895. // make sure next time there will be an update (if enabled)
  7896. Updates.LastCheck = TDateTime(1);
  7897. WinConfiguration->Updates = Updates;
  7898. }
  7899. else if ((double(Updates.Period) > 0) &&
  7900. (Now() - Updates.LastCheck >= Updates.Period))
  7901. {
  7902. TThreadMethod OnUpdatesChecked = NULL;
  7903. if (!IsUWP())
  7904. {
  7905. OnUpdatesChecked = UpdatesChecked;
  7906. }
  7907. StartUpdateThread(OnUpdatesChecked);
  7908. }
  7909. }
  7910. //---------------------------------------------------------------------------
  7911. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  7912. {
  7913. UnicodeString Message;
  7914. bool New;
  7915. TQueryType Type;
  7916. GetUpdatesMessage(Message, New, Type, false);
  7917. if (!Message.IsEmpty())
  7918. {
  7919. if (New)
  7920. {
  7921. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  7922. }
  7923. if (!New && (Type != qtWarning))
  7924. {
  7925. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  7926. }
  7927. else
  7928. {
  7929. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  7930. }
  7931. if (New)
  7932. {
  7933. Configuration->Usage->Inc(L"UpdateNotifications");
  7934. }
  7935. }
  7936. }
  7937. //---------------------------------------------------------------------------
  7938. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  7939. {
  7940. RestoreApp();
  7941. bool CanDisplay = !NonVisualDataModule->Busy;
  7942. if (!CanDisplay && (Screen->ActiveForm != NULL))
  7943. {
  7944. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  7945. }
  7946. if (CanDisplay)
  7947. {
  7948. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  7949. CheckForUpdates(true);
  7950. }
  7951. else
  7952. {
  7953. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  7954. }
  7955. }
  7956. //---------------------------------------------------------------------------
  7957. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  7958. TCopyParamRuleData & Data)
  7959. {
  7960. DebugAssert(Terminal != NULL);
  7961. Data.HostName = Terminal->SessionData->HostNameExpanded;
  7962. Data.UserName = Terminal->SessionData->UserNameExpanded;
  7963. Data.RemoteDirectory = RemoteDirView->PathName;
  7964. }
  7965. //---------------------------------------------------------------------------
  7966. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  7967. {
  7968. // Terminal can be null when we are changing local directory implicitly,
  7969. // because it has been deleted, while no session is connected
  7970. // (Login dialog is open)
  7971. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  7972. {
  7973. TCopyParamRuleData Data;
  7974. GetTransferPresetAutoSelectData(Data);
  7975. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  7976. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  7977. if (CopyParamIndex < 0)
  7978. {
  7979. // there is no preset that matches autoselection
  7980. // set preset that we consider "default"
  7981. FCopyParamAutoSelected = L""; // forget last autoselected preset
  7982. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  7983. }
  7984. else
  7985. {
  7986. // there is preset matching autoselection
  7987. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  7988. if (CopyParamName == FCopyParamAutoSelected)
  7989. {
  7990. // autoselected the same preset as the last time
  7991. // make no change (i.e. preserve custom user preset, if any)
  7992. }
  7993. else
  7994. {
  7995. // autoselected the different preset then the last time (or there
  7996. // was default preset set)
  7997. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  7998. GUIConfiguration->CopyParamCurrent = CopyParamName;
  7999. }
  8000. }
  8001. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8002. {
  8003. if (CopyParamIndex >= 0)
  8004. {
  8005. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8006. }
  8007. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8008. try
  8009. {
  8010. int Fmt =
  8011. (CopyParamIndex < 0) ?
  8012. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8013. COPY_PARAM_AUTOSELECTED;
  8014. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8015. Data->Message = MainInstructions(Message);
  8016. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8017. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8018. CopyParamAttrs);
  8019. if (CopyParamIndex >= 0)
  8020. {
  8021. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8022. Info = FORMAT(L"%s\n \n%s", (Info,
  8023. FMTLOAD(COPY_PARAM_RULE,
  8024. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8025. }
  8026. Data->Message += L"\n\n" + Info;
  8027. if (WinConfiguration->CopyParamAutoSelectNotice)
  8028. {
  8029. TransferPresetNoteMessage(Data, true);
  8030. }
  8031. else
  8032. {
  8033. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8034. Data = NULL; // ownership passed
  8035. }
  8036. }
  8037. __finally
  8038. {
  8039. delete Data;
  8040. }
  8041. }
  8042. }
  8043. }
  8044. //---------------------------------------------------------------------------
  8045. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8046. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8047. {
  8048. DebugAssert(NoteData != NULL);
  8049. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8050. TQueryButtonAlias Aliases[1];
  8051. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8052. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8053. Params.Aliases = Aliases;
  8054. Params.AliasesCount = LENOF(Aliases);
  8055. unsigned int Result =
  8056. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8057. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8058. switch (Result)
  8059. {
  8060. case qaNeverAskAgain:
  8061. DebugAssert(AllowNeverAskAgain);
  8062. WinConfiguration->CopyParamAutoSelectNotice = false;
  8063. break;
  8064. case qaIgnore:
  8065. PreferencesDialog(pmPresets);
  8066. break;
  8067. }
  8068. }
  8069. //---------------------------------------------------------------------------
  8070. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8071. {
  8072. // as of now useless, as this is used for notes only, never for balloons, ...
  8073. RestoreApp();
  8074. // .. and we should never be busy here
  8075. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8076. {
  8077. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8078. }
  8079. }
  8080. //---------------------------------------------------------------------------
  8081. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8082. TPreferencesMode APreferencesMode)
  8083. {
  8084. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8085. TCopyParamRuleData Data;
  8086. if (Terminal != NULL)
  8087. {
  8088. PreferencesData.reset(new TPreferencesDialogData());
  8089. GetTransferPresetAutoSelectData(Data);
  8090. PreferencesData->CopyParamRuleData = &Data;
  8091. }
  8092. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8093. }
  8094. //---------------------------------------------------------------------------
  8095. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8096. const TCustomCommandType & Command)
  8097. {
  8098. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8099. {
  8100. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (Command.Command)));
  8101. }
  8102. }
  8103. //---------------------------------------------------------------------------
  8104. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8105. {
  8106. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8107. TCustomCommandType Command;
  8108. // make sure we use local custom command when remote are not supported
  8109. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8110. {
  8111. Command = FLastCustomCommand;
  8112. }
  8113. else
  8114. {
  8115. Command.Params = Command.Params | ccLocal;
  8116. }
  8117. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8118. FEditingFocusedAdHocCommand = OnFocused;
  8119. bool LocalSide = (FCurrentSide == osLocal);
  8120. int Options =
  8121. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8122. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8123. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8124. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8125. {
  8126. FLastCustomCommand = Command;
  8127. UpdateCustomCommandsToolbar();
  8128. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8129. }
  8130. }
  8131. //---------------------------------------------------------------------------
  8132. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8133. {
  8134. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8135. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8136. DebugAssert(State > 0);
  8137. if (State <= 0)
  8138. {
  8139. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (FLastCustomCommand.Command)));
  8140. }
  8141. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8142. }
  8143. //---------------------------------------------------------------------------
  8144. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8145. TCustomCommandType & Command, int & State)
  8146. {
  8147. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8148. if (Result)
  8149. {
  8150. Command = FLastCustomCommand;
  8151. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8152. }
  8153. return Result;
  8154. }
  8155. //---------------------------------------------------------------------------
  8156. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8157. {
  8158. if (RemoteDirView->ItemFocused != NULL)
  8159. {
  8160. RemoteDirView->ItemFocused->CancelEdit();
  8161. }
  8162. ResetIncrementalSearch();
  8163. }
  8164. //---------------------------------------------------------------------------
  8165. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8166. {
  8167. DebugAssert(ComponentVisible[Component]);
  8168. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8169. }
  8170. //---------------------------------------------------------------------------
  8171. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8172. {
  8173. // when queue panel is resized here directly, the status bar is stretched
  8174. // over whole space the panel occupied
  8175. PostComponentHide(fcQueueView);
  8176. }
  8177. //---------------------------------------------------------------------------
  8178. void __fastcall TCustomScpExplorerForm::WMWinIniChange(TMessage & Message)
  8179. {
  8180. WinConfiguration->ResetSysDarkTheme();
  8181. ConfigurationChanged();
  8182. ConfigureInterface();
  8183. TForm::Dispatch(&Message);
  8184. }
  8185. //---------------------------------------------------------------------------
  8186. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8187. {
  8188. TMessage * M = static_cast<TMessage*>(Message);
  8189. switch (M->Msg)
  8190. {
  8191. case CM_DIALOGCHAR:
  8192. CMDialogChar(*M);
  8193. break;
  8194. case WM_APPCOMMAND:
  8195. WMAppCommand(*M);
  8196. break;
  8197. case WM_SYSCOMMAND:
  8198. WMSysCommand(*M);
  8199. break;
  8200. case WM_QUERYENDSESSION:
  8201. WMQueryEndSession(*M);
  8202. break;
  8203. case WM_ENDSESSION:
  8204. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8205. break;
  8206. #ifdef _DEBUG
  8207. case WM_WINDOWPOSCHANGED:
  8208. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8209. break;
  8210. case CM_CUSTOMSTYLECHANGED:
  8211. TForm::Dispatch(Message);
  8212. break;
  8213. #endif
  8214. case WM_COMPONENT_HIDE:
  8215. {
  8216. Byte Component = static_cast<Byte>(M->WParam);
  8217. // sanity check
  8218. if (ComponentVisible[Component])
  8219. {
  8220. // special treatment
  8221. if (Component == fcQueueView)
  8222. {
  8223. ToggleQueueVisibility();
  8224. DebugAssert(!ComponentVisible[fcQueueView]);
  8225. }
  8226. else
  8227. {
  8228. ComponentVisible[Component] = false;
  8229. }
  8230. }
  8231. }
  8232. break;
  8233. case WM_COPYDATA:
  8234. WMCopyData(*M);
  8235. break;
  8236. case WM_MANAGES_CAPTION:
  8237. // caption managed in TTerminalManager::UpdateAppTitle()
  8238. M->Result = 1;
  8239. break;
  8240. case WM_WANTS_MOUSEWHEEL:
  8241. M->Result = 1;
  8242. break;
  8243. case CM_SHOWINGCHANGED:
  8244. CMShowingChanged(*M);
  8245. break;
  8246. case WM_CLOSE:
  8247. WMClose(*M);
  8248. break;
  8249. case WM_DPICHANGED:
  8250. WMDpiChanged(*M);
  8251. break;
  8252. case CM_DPICHANGED:
  8253. CMDpiChanged(*M);
  8254. break;
  8255. case WM_WININICHANGE:
  8256. WMWinIniChange(*M);
  8257. break;
  8258. case CM_DIALOGKEY:
  8259. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8260. break;
  8261. default:
  8262. TForm::Dispatch(Message);
  8263. break;
  8264. }
  8265. }
  8266. //---------------------------------------------------------------------------
  8267. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8268. {
  8269. // noop
  8270. }
  8271. //---------------------------------------------------------------------------
  8272. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8273. {
  8274. TForm::Dispatch(&Message);
  8275. FSessionColors->Clear();
  8276. AddFixedSessionImages();
  8277. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8278. UpdateImages();
  8279. }
  8280. //---------------------------------------------------------------------------
  8281. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8282. {
  8283. TForm::Dispatch(&Message);
  8284. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8285. UpdatePixelsPerInchMainWindowCounter();
  8286. }
  8287. //---------------------------------------------------------------------------
  8288. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8289. {
  8290. // Cannot close window while we are busy.
  8291. // We cannot test this in FormCloseQuery as that is called also from
  8292. // Close(), which is called by CloseApplicationAction. So we can be busy
  8293. // there already even, when it is legitimate to close the application.
  8294. // Possibly a better place to handle this would be WMSysCommand.
  8295. if (NonVisualDataModule->Busy)
  8296. {
  8297. Message.Result = 1;
  8298. }
  8299. else
  8300. {
  8301. TForm::Dispatch(&Message);
  8302. }
  8303. }
  8304. //---------------------------------------------------------------------------
  8305. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8306. {
  8307. TForm::Dispatch(&Message);
  8308. if (Showing && (Terminal == NULL))
  8309. {
  8310. // When we are starting minimized (i.e. from an installer),
  8311. // postpone showing Login dialog until we get restored.
  8312. // Otherwise the Login dialog (and Authentication window) show restored
  8313. // over invidible (minimized) main window.
  8314. if (WindowState == wsMinimized)
  8315. {
  8316. FNeedSession = true;
  8317. }
  8318. else
  8319. {
  8320. // This happens before application ever goes idle, so the toolbars would
  8321. // stay enabled (initial state) until the Login dialog is dismissed.
  8322. UpdateActions();
  8323. NonVisualDataModule->StartBusy();
  8324. try
  8325. {
  8326. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8327. // otherwise the dialog does not receive focus.
  8328. // With Commander interface the ProcessMessages is called already
  8329. // by TDriveView, but with Explorer interface, we need to call it explicily
  8330. Application->ProcessMessages();
  8331. // do not reload sessions, they have been loaded just now (optimization)
  8332. NeedSession(false);
  8333. }
  8334. __finally
  8335. {
  8336. NonVisualDataModule->EndBusy();
  8337. }
  8338. }
  8339. }
  8340. }
  8341. //---------------------------------------------------------------------------
  8342. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8343. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8344. int & MaxHeight)
  8345. {
  8346. // workaround for bug in TWinControl.CalcConstraints
  8347. // Check for empty rect (restore from iconic state) is done there only after
  8348. // call to AdjustClientRect, which enlarges the rect (for forms).
  8349. TRect R = GetClientRect();
  8350. // when restoring from iconic state, place no restrictions
  8351. if (IsRectEmpty(R))
  8352. {
  8353. MinWidth = 0;
  8354. MinHeight = 0;
  8355. MaxWidth = 32000;
  8356. MaxHeight = 32000;
  8357. }
  8358. }
  8359. //---------------------------------------------------------------------------
  8360. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8361. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8362. {
  8363. // terminal can be already closed (e.g. dropped connection)
  8364. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8365. {
  8366. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8367. try
  8368. {
  8369. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8370. }
  8371. catch(...)
  8372. {
  8373. if (!Terminal->Active)
  8374. {
  8375. Close = true;
  8376. }
  8377. throw;
  8378. }
  8379. }
  8380. }
  8381. //---------------------------------------------------------------------------
  8382. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8383. {
  8384. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8385. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8386. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8387. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8388. {
  8389. OnGetSpaceAvailable = GetSpaceAvailable;
  8390. }
  8391. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8392. OnGetSpaceAvailable);
  8393. }
  8394. //---------------------------------------------------------------------------
  8395. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8396. {
  8397. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8398. Terminal->FillSessionDataForCode(Data.get());
  8399. return Data.release();
  8400. }
  8401. //---------------------------------------------------------------------------
  8402. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8403. {
  8404. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8405. DoGenerateUrlDialog(Data.get(), Paths);
  8406. }
  8407. //---------------------------------------------------------------------------
  8408. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8409. {
  8410. GenerateUrl(NULL);
  8411. }
  8412. //---------------------------------------------------------------------------
  8413. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8414. {
  8415. std::unique_ptr<TStrings> Paths(new TStringList());
  8416. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8417. GenerateUrl(Paths.get());
  8418. }
  8419. //---------------------------------------------------------------------------
  8420. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  8421. {
  8422. FSessionColor = value;
  8423. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8424. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8425. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8426. DebugAssert(ColorItem != NULL);
  8427. ColorItem->Color = C;
  8428. NonVisualDataModule->ColorMenuItem->Color = C;
  8429. UpdateControls();
  8430. }
  8431. //---------------------------------------------------------------------------
  8432. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8433. {
  8434. if (value != FSessionColor)
  8435. {
  8436. UpdateSessionColor(value);
  8437. // Is null when called from LastTerminalClosed
  8438. if (Terminal != NULL)
  8439. {
  8440. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8441. }
  8442. }
  8443. }
  8444. //---------------------------------------------------------------------------
  8445. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8446. {
  8447. bool Result = FNoteTimer->Enabled;
  8448. if (Result)
  8449. {
  8450. // cannot cancel note too early
  8451. bool NotEarly =
  8452. (Now() - FNoteShown >
  8453. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8454. if (Force || NotEarly)
  8455. {
  8456. FNoteTimer->Enabled = false;
  8457. FNote = L"";
  8458. SAFE_DESTROY(FNoteData);
  8459. FOnNoteClick = NULL;
  8460. FNoteHints = FNotes->Text;
  8461. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8462. UpdateStatusBar();
  8463. }
  8464. }
  8465. return Result;
  8466. }
  8467. //---------------------------------------------------------------------------
  8468. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8469. {
  8470. DebugAssert(FNoteTimer->Enabled);
  8471. CancelNote(true);
  8472. }
  8473. //---------------------------------------------------------------------------
  8474. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8475. {
  8476. int P = Note.Pos(L"\n");
  8477. if (P > 0)
  8478. {
  8479. Note.SetLength(P - 1);
  8480. }
  8481. FNotes->Add(FORMAT(L"[%s] %s",
  8482. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8483. while (FNotes->Count > 10)
  8484. {
  8485. FNotes->Delete(0);
  8486. }
  8487. if (UpdateNow)
  8488. {
  8489. FNoteHints = FNotes->Text;
  8490. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8491. UpdateStatusBar();
  8492. }
  8493. }
  8494. //---------------------------------------------------------------------------
  8495. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8496. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8497. {
  8498. int P = Note.Pos(L"\n");
  8499. if (P > 0)
  8500. {
  8501. Note.SetLength(P - 1);
  8502. }
  8503. FNoteHints = FNotes->Text;
  8504. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8505. FNote = Note;
  8506. SAFE_DESTROY(FNoteData);
  8507. FNoteData = NoteData;
  8508. FOnNoteClick = OnNoteClick;
  8509. AddNote(Note, false);
  8510. UpdateStatusBar();
  8511. FNoteShown = Now();
  8512. FNoteTimer->Enabled = false;
  8513. if (Seconds == 0)
  8514. {
  8515. Seconds = WinConfiguration->NotificationsTimeout;
  8516. }
  8517. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8518. FNoteTimer->Enabled = true;
  8519. }
  8520. //---------------------------------------------------------------------------
  8521. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8522. {
  8523. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8524. }
  8525. //---------------------------------------------------------------------------
  8526. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8527. {
  8528. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  8529. {
  8530. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8531. }
  8532. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  8533. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  8534. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  8535. 0, NULL, NULL);
  8536. }
  8537. //---------------------------------------------------------------------------
  8538. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  8539. {
  8540. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  8541. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  8542. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  8543. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  8544. }
  8545. //---------------------------------------------------------------------------
  8546. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  8547. {
  8548. WinConfiguration->FormatSizeBytes = Style;
  8549. }
  8550. //---------------------------------------------------------------------------
  8551. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  8552. {
  8553. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  8554. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  8555. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  8556. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  8557. }
  8558. //---------------------------------------------------------------------------
  8559. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  8560. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  8561. {
  8562. if (Panel->Index == 0)
  8563. {
  8564. if (FOnNoteClick != NULL)
  8565. {
  8566. // prevent the user data from being freed by possible call
  8567. // to CancelNote or PostNote during call to OnNoteClick
  8568. std::unique_ptr<TObject> NoteData(FNoteData);
  8569. TNotifyEvent OnNoteClick = FOnNoteClick;
  8570. FNoteData = NULL;
  8571. // need to cancel the note as we are going to delete its user data
  8572. CancelNote(true);
  8573. OnNoteClick(NoteData.get());
  8574. }
  8575. }
  8576. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  8577. {
  8578. FileSystemInfo();
  8579. }
  8580. }
  8581. //---------------------------------------------------------------------------
  8582. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  8583. {
  8584. // workaround:
  8585. // 1) for unknown reason, disabling window, while minimized,
  8586. // prevents it from restoring, even if it was enabled again meanwhile
  8587. // 2) when disabling the main window, while another has focus
  8588. // minimize is no longer possible
  8589. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  8590. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  8591. // Shouldn't we use IsApplicationMinimized() here?
  8592. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  8593. {
  8594. Enabled = false;
  8595. }
  8596. FLockLevel++;
  8597. }
  8598. //---------------------------------------------------------------------------
  8599. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  8600. {
  8601. DebugAssert(FLockLevel > 0);
  8602. FLockLevel--;
  8603. if (FLockLevel == 0)
  8604. {
  8605. DebugAssert(FLockSuspendLevel == 0);
  8606. Enabled = true;
  8607. }
  8608. }
  8609. //---------------------------------------------------------------------------
  8610. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  8611. {
  8612. // We need to make sure that window is enabled when the last modal window closes
  8613. // otherwise focus is not restored correctly.
  8614. // So we re-enable the window when modal window opens and
  8615. // disable it back after is closes
  8616. if (FLockLevel > 0)
  8617. {
  8618. // while we have nesting counter, we know that we never be called
  8619. // recursivelly as Application->OnModalBegin is called only
  8620. // for the top-level modal window
  8621. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8622. {
  8623. // won't be disabled when conditions in LockWindow() were not satisfied
  8624. FDisabledOnLockSuspend = !Enabled;
  8625. // When minimized to tray (or actually when set to SW_HIDE),
  8626. // setting Enabled makes the window focusable even when there's
  8627. // modal window over it
  8628. if (!FTrayIcon->Visible)
  8629. {
  8630. Enabled = true;
  8631. }
  8632. }
  8633. FLockSuspendLevel++;
  8634. }
  8635. }
  8636. //---------------------------------------------------------------------------
  8637. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  8638. {
  8639. if (FLockSuspendLevel > 0)
  8640. {
  8641. DebugAssert(FLockLevel > 0);
  8642. FLockSuspendLevel--;
  8643. // see comment in SuspendWindowLock
  8644. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8645. {
  8646. // Note that window can be enabled here, when we were minized to tray when
  8647. // was SuspendWindowLock() called.
  8648. // We should possibly do the same check as in LockWindow(),
  8649. // if it is ever possible that the consitions change between
  8650. // SuspendWindowLock() and ResumeWindowLock()
  8651. Enabled = !FDisabledOnLockSuspend;
  8652. }
  8653. }
  8654. }
  8655. //---------------------------------------------------------------------------
  8656. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  8657. {
  8658. if (!TextOnly)
  8659. {
  8660. TTBXComboBoxItem * RemotePathComboBox =
  8661. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  8662. TStrings * Items = RemotePathComboBox->Strings;
  8663. Items->BeginUpdate();
  8664. try
  8665. {
  8666. Items->Clear();
  8667. if (Terminal != NULL)
  8668. {
  8669. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8670. while (!IsUnixRootPath(APath))
  8671. {
  8672. int P = APath.LastDelimiter(L'/');
  8673. DebugAssert(P >= 0);
  8674. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  8675. APath.SetLength(P - 1);
  8676. }
  8677. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  8678. }
  8679. }
  8680. __finally
  8681. {
  8682. RemotePathComboBox->ItemIndex = Items->Count - 1;
  8683. // Setting ItemIndex to -1 does not reset its text
  8684. if (Items->Count == 0)
  8685. {
  8686. RemotePathComboBox->Text = L"";
  8687. }
  8688. Items->EndUpdate();
  8689. }
  8690. }
  8691. }
  8692. //---------------------------------------------------------------------------
  8693. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  8694. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  8695. int & ImageIndex)
  8696. {
  8697. if (AIndex < 0)
  8698. {
  8699. AIndex = Sender->ItemIndex;
  8700. }
  8701. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  8702. }
  8703. //---------------------------------------------------------------------------
  8704. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  8705. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  8706. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  8707. {
  8708. ARect.Left += (10 * AIndex);
  8709. }
  8710. //---------------------------------------------------------------------------
  8711. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  8712. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  8713. {
  8714. AWidth += (10 * AIndex);
  8715. }
  8716. //---------------------------------------------------------------------------
  8717. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  8718. TObject * Sender)
  8719. {
  8720. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  8721. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8722. int Index = RemotePathComboBox->ItemIndex;
  8723. while (Index < RemotePathComboBox->Strings->Count - 1)
  8724. {
  8725. APath = UnixExtractFileDir(APath);
  8726. Index++;
  8727. }
  8728. // VanDyke style paths
  8729. if (APath.IsEmpty())
  8730. {
  8731. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  8732. APath = ROOTDIRECTORY;
  8733. }
  8734. if (RemoteDirView->Path != APath)
  8735. {
  8736. RemoteDirView->Path = APath;
  8737. }
  8738. }
  8739. //---------------------------------------------------------------------------
  8740. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  8741. {
  8742. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  8743. DebugUsedParam(Sender);
  8744. UpdateRemotePathComboBox(true);
  8745. }
  8746. //---------------------------------------------------------------------------
  8747. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  8748. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  8749. {
  8750. ResetIncrementalSearch();
  8751. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8752. DebugAssert(DirView != NULL);
  8753. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  8754. {
  8755. HWND Edit = ListView_GetEditControl(DirView->Handle);
  8756. // OnEditing is called also from TCustomListView::CanEdit
  8757. if (Edit != NULL)
  8758. {
  8759. EditSelectBaseName(Edit);
  8760. }
  8761. }
  8762. }
  8763. //---------------------------------------------------------------------------
  8764. //---------------------------------------------------------------------------
  8765. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  8766. {
  8767. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  8768. Result->AutoDetectDnD = false;
  8769. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  8770. Result->RenderDataOn = rdoEnterAndDropSync;
  8771. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  8772. return Result;
  8773. }
  8774. //---------------------------------------------------------------------------
  8775. void __fastcall TCustomScpExplorerForm::CreateWnd()
  8776. {
  8777. TForm::CreateWnd();
  8778. if (FSessionsDragDropFilesEx == NULL)
  8779. {
  8780. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  8781. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  8782. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  8783. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  8784. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  8785. }
  8786. if (FQueueDragDropFilesEx == NULL)
  8787. {
  8788. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  8789. // No need to set OnDragOver as we do not have any restrictions
  8790. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  8791. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  8792. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  8793. }
  8794. }
  8795. //---------------------------------------------------------------------------
  8796. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  8797. {
  8798. TForm::DestroyWnd();
  8799. FSessionsDragDropFilesEx->DragDropControl = NULL;
  8800. FQueueDragDropFilesEx->DragDropControl = NULL;
  8801. }
  8802. //---------------------------------------------------------------------------
  8803. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  8804. {
  8805. SideEnter(FCurrentSide);
  8806. FEverShown = true;
  8807. }
  8808. //---------------------------------------------------------------------------
  8809. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  8810. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  8811. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  8812. {
  8813. if (!NonVisualDataModule->Busy)
  8814. {
  8815. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8816. Configuration->Usage->Inc(L"FileFinds");
  8817. LockWindow(true);
  8818. NonVisualDataModule->StartBusy();
  8819. WinConfiguration->LockedInterface = true;
  8820. try
  8821. {
  8822. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  8823. }
  8824. __finally
  8825. {
  8826. WinConfiguration->LockedInterface = false;
  8827. NonVisualDataModule->EndBusy();
  8828. UnlockWindow();
  8829. }
  8830. }
  8831. }
  8832. //---------------------------------------------------------------------------
  8833. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  8834. {
  8835. if (!NonVisualDataModule->Busy)
  8836. {
  8837. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8838. SetFocus();
  8839. RemoteDirView->Path = UnixExtractFilePath(Path);
  8840. UnicodeString FileName = UnixExtractFileName(Path);
  8841. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  8842. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  8843. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  8844. if (Item == NULL)
  8845. {
  8846. ReloadDirectory(osRemote);
  8847. Item = RemoteDirView->FindFileItem(FileName);
  8848. }
  8849. if (Item != NULL)
  8850. {
  8851. RemoteDirView->ItemFocused = Item;
  8852. RemoteDirView->ItemFocused->MakeVisible(false);
  8853. RemoteDirView->SetFocus();
  8854. }
  8855. }
  8856. }
  8857. //---------------------------------------------------------------------------
  8858. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  8859. {
  8860. bool Result = !NonVisualDataModule->Busy;
  8861. if (Result)
  8862. {
  8863. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8864. }
  8865. return Result;
  8866. }
  8867. //---------------------------------------------------------------------------
  8868. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  8869. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8870. {
  8871. if (CanOperateOnFoundFiles(ATerminal))
  8872. {
  8873. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  8874. FOnFileOperationFinished = OnFileOperationFinished;
  8875. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  8876. }
  8877. }
  8878. //---------------------------------------------------------------------------
  8879. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  8880. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8881. {
  8882. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  8883. }
  8884. //---------------------------------------------------------------------------
  8885. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  8886. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8887. {
  8888. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  8889. }
  8890. //---------------------------------------------------------------------------
  8891. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  8892. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8893. {
  8894. if (CanOperateOnFoundFiles(ATerminal))
  8895. {
  8896. for (int Index = 0; Index < FileList->Count; Index++)
  8897. {
  8898. UnicodeString FileName = FileList->Strings[Index];
  8899. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  8900. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  8901. OnFileOperationFinished(FileName, true);
  8902. }
  8903. }
  8904. }
  8905. //---------------------------------------------------------------------------
  8906. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  8907. {
  8908. FFileFindTerminal = Terminal;
  8909. ShowFileFindDialog(
  8910. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  8911. DoDownloadFoundFiles, DoEditFoundFiles);
  8912. }
  8913. //---------------------------------------------------------------------------
  8914. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  8915. {
  8916. FTaskbarList = TaskbarList;
  8917. }
  8918. //---------------------------------------------------------------------------
  8919. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  8920. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  8921. {
  8922. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8923. if (Index >= 0)
  8924. {
  8925. if (Button == mbLeft)
  8926. {
  8927. // "Mouse down" is raised only after tab is switched.
  8928. // If switching tab (switching session) takes long enough for user
  8929. // to actually release the button, "mouse down" is still raised,
  8930. // but we do not get "mouse up" event, so dragging is not cancelled,
  8931. // prevent that by not beginning dragging in the first place.
  8932. if (IsKeyPressed(VK_LBUTTON))
  8933. {
  8934. // when user clicks the "+", we get mouse down only after the session
  8935. // is closed, when new session tab is already on X:Y, so dragging
  8936. // starts, prevent that
  8937. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  8938. {
  8939. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8940. if (Terminal != NULL)
  8941. {
  8942. SessionsPageControl->BeginDrag(false);
  8943. }
  8944. }
  8945. }
  8946. }
  8947. else if (Button == mbMiddle)
  8948. {
  8949. // ignore middle-click for "New session tab"
  8950. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8951. if (Terminal != NULL)
  8952. {
  8953. CloseSessionTab(Index);
  8954. }
  8955. }
  8956. }
  8957. }
  8958. //---------------------------------------------------------------------------
  8959. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  8960. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  8961. {
  8962. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8963. // do not allow dropping on the "+" tab
  8964. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8965. if ((TargetTerminal != NULL) &&
  8966. (SessionsPageControl->ActivePage->PageIndex != Index))
  8967. {
  8968. Configuration->Usage->Inc(L"SessionTabMoves");
  8969. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  8970. // but we want to actually prevent that to avoid flicker
  8971. SessionsPageControl->ActivePage->PageIndex = Index;
  8972. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  8973. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  8974. }
  8975. }
  8976. //---------------------------------------------------------------------------
  8977. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  8978. TObject * Sender, TObject * Source, int X, int Y,
  8979. TDragState /*State*/, bool & Accept)
  8980. {
  8981. Accept = (Sender == Source);
  8982. if (Accept)
  8983. {
  8984. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8985. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8986. // do not allow dragging to the "+" tab
  8987. Accept = (Terminal != NULL);
  8988. }
  8989. }
  8990. //---------------------------------------------------------------------------
  8991. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(int /*KeyState*/,
  8992. const TPoint & Point, int & Effect)
  8993. {
  8994. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  8995. if (Index < 0)
  8996. {
  8997. Effect = DROPEFFECT_NONE;
  8998. }
  8999. else
  9000. {
  9001. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9002. // do not allow dropping on the "+" tab
  9003. if (TargetTerminal == NULL)
  9004. {
  9005. Effect = DROPEFFECT_NONE;
  9006. }
  9007. else if ((TargetTerminal != Terminal) && (Effect == DROPEFFECT_MOVE))
  9008. {
  9009. Effect = DROPEFFECT_COPY;
  9010. }
  9011. }
  9012. }
  9013. //---------------------------------------------------------------------------
  9014. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9015. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9016. {
  9017. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9018. // do not allow dropping on the "+" tab
  9019. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9020. if (TargetTerminal != NULL)
  9021. {
  9022. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9023. if (!IsFileControl(DropSourceControl, osRemote))
  9024. {
  9025. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  9026. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9027. // Why don't we use Terminal->CurrentDirectory directly?
  9028. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9029. // do not force queue, drag drop
  9030. false, true);
  9031. }
  9032. }
  9033. }
  9034. //---------------------------------------------------------------------------
  9035. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9036. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9037. {
  9038. // Downloads are handled in RemoteFileControlDDEnd
  9039. if (!IsFileControl(DropSourceControl, osRemote))
  9040. {
  9041. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9042. Terminal->CurrentDirectory,
  9043. // force queue, drag drop
  9044. true, true);
  9045. }
  9046. }
  9047. //---------------------------------------------------------------------------
  9048. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9049. {
  9050. FShowing = false;
  9051. // Do not save empty workspace
  9052. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  9053. {
  9054. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9055. UnicodeString Name = WorkspaceName();
  9056. DoSaveWorkspace(
  9057. Name, DataList.get(),
  9058. !Configuration->DisablePasswordStoring &&
  9059. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9060. WinConfiguration->LastStoredSession = Name;
  9061. }
  9062. }
  9063. //---------------------------------------------------------------------------
  9064. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9065. {
  9066. TManagedTerminal * ManagedTerminal =
  9067. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9068. if (ManagedTerminal != NULL)
  9069. {
  9070. ManagedTerminal->DirectoryLoaded = Now();
  9071. }
  9072. }
  9073. //---------------------------------------------------------------------------
  9074. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9075. TListItem * /*Item*/, bool /*Selected*/)
  9076. {
  9077. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9078. switch (DirView->LastSelectMethod)
  9079. {
  9080. case smKeyboard:
  9081. Configuration->Usage->Inc(L"KeyboardSelections");
  9082. break;
  9083. case smMouse:
  9084. Configuration->Usage->Inc(L"MouseSelections");
  9085. break;
  9086. }
  9087. }
  9088. //---------------------------------------------------------------------------
  9089. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9090. {
  9091. if (Color != 0)
  9092. {
  9093. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9094. }
  9095. else
  9096. {
  9097. return FSessionTabImageIndex;
  9098. }
  9099. }
  9100. //---------------------------------------------------------------------------
  9101. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9102. {
  9103. TPngImageCollectionItem * Item =
  9104. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9105. FSessionColors->AddPng(Item->PngImage);
  9106. return FSessionColors->Count - 1;
  9107. }
  9108. //---------------------------------------------------------------------------
  9109. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9110. {
  9111. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9112. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  9113. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9114. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9115. }
  9116. //---------------------------------------------------------------------------
  9117. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9118. {
  9119. for (int Index = 0; Index < Control->ControlCount; Index++)
  9120. {
  9121. TControl * AControl = Control->Controls[Index];
  9122. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9123. if (WinControl != NULL)
  9124. {
  9125. CollectItemsWithTextDisplayMode(WinControl);
  9126. }
  9127. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9128. if (Toolbar != NULL)
  9129. {
  9130. // we care for top-level items only
  9131. TTBCustomItem * Items = Toolbar->Items;
  9132. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9133. {
  9134. TTBCustomItem * Item = Items->Items[ItemIndex];
  9135. if (((Item->DisplayMode == nbdmImageAndText) ||
  9136. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9137. EligibleForImageDisplayMode(Item))
  9138. {
  9139. FItemsWithTextDisplayMode.insert(Item);
  9140. }
  9141. }
  9142. }
  9143. }
  9144. }
  9145. //---------------------------------------------------------------------------
  9146. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9147. {
  9148. return true;
  9149. }
  9150. //---------------------------------------------------------------------------
  9151. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9152. {
  9153. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9154. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9155. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9156. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9157. bool Result = true;
  9158. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9159. {
  9160. TTBCustomItem * Item = *i;
  9161. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9162. if (Label != NULL)
  9163. {
  9164. // optimization
  9165. if (Label->Visible == SelectiveToolbarText)
  9166. {
  9167. Result = false;
  9168. }
  9169. else
  9170. {
  9171. Label->Visible = SelectiveToolbarText;
  9172. }
  9173. }
  9174. else
  9175. {
  9176. // optimization
  9177. if (Item->DisplayMode == DisplayMode)
  9178. {
  9179. Result = false;
  9180. }
  9181. else
  9182. {
  9183. Item->DisplayMode = DisplayMode;
  9184. }
  9185. }
  9186. ++i;
  9187. }
  9188. if (Result)
  9189. {
  9190. UpdateNewSessionTab();
  9191. }
  9192. return Result;
  9193. }
  9194. //---------------------------------------------------------------------------
  9195. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9196. {
  9197. DebugFail();
  9198. }
  9199. //---------------------------------------------------------------------------
  9200. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9201. {
  9202. // Among other, a lock level is non zero, while directory is being loaded,
  9203. // even when it happens as a result of dirview navigation,
  9204. // i.e. when TNonVisualDataModule is NOT FBusy.
  9205. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9206. // Among other this prevents a panel auto update to occur while
  9207. // directory is changing.
  9208. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9209. }
  9210. //---------------------------------------------------------------------------
  9211. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  9212. {
  9213. // While the window is disabled, we still seem to process menu shortcuts at least,
  9214. // so stop it at least here.
  9215. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9216. return
  9217. (Allowed == aaUpdate) ||
  9218. !NonVisualDataModule->Busy;
  9219. }
  9220. //---------------------------------------------------------------------------
  9221. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9222. {
  9223. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9224. }
  9225. //---------------------------------------------------------------------------
  9226. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9227. {
  9228. // This is somewhat redundant to LockWindow() call from
  9229. // TTerminalManager::TerminalReadDirectoryProgress.
  9230. // But disabling window is known not to block keyboard shorcuts
  9231. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9232. if (Busy > 0)
  9233. {
  9234. if (NonVisualDataModule->Busy)
  9235. {
  9236. State = false;
  9237. }
  9238. else
  9239. {
  9240. NonVisualDataModule->StartBusy();
  9241. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9242. LockWindow();
  9243. }
  9244. }
  9245. else if (Busy < 0)
  9246. {
  9247. UnlockWindow();
  9248. NonVisualDataModule->EndBusy();
  9249. }
  9250. else
  9251. {
  9252. State = NonVisualDataModule->Busy;
  9253. }
  9254. }
  9255. //---------------------------------------------------------------------------
  9256. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9257. {
  9258. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9259. // no context menu for "New session tab"
  9260. if ((Index >= 0) && (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != NULL))
  9261. {
  9262. SessionsPageControl->ActivePageIndex = Index;
  9263. if (DebugAlwaysTrue(SessionTabSwitched()))
  9264. {
  9265. // copied from TControl.WMContextMenu
  9266. SendCancelMode(SessionsPageControl);
  9267. // explicit popup instead of using PopupMenu property
  9268. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9269. // while connecting yet not-connected session and hence
  9270. // allowing an access to commands over not-completelly connected session
  9271. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9272. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  9273. PopupMenu->PopupComponent = SessionsPageControl;
  9274. PopupMenu->Popup(Point.x, Point.y);
  9275. }
  9276. }
  9277. Handled = true;
  9278. }
  9279. //---------------------------------------------------------------------------
  9280. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9281. {
  9282. return (Terminal != NULL) && Terminal->Active && Terminal->IsCapable[fcChangePassword];
  9283. }
  9284. //---------------------------------------------------------------------------
  9285. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9286. {
  9287. Configuration->Usage->Inc(L"PasswordChanges");
  9288. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9289. ChangePasswordData->DisableAuthentationsExceptPassword();
  9290. ChangePasswordData->ChangePassword = true;
  9291. std::unique_ptr<TTerminal> ChangePasswordSession(
  9292. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9293. ChangePasswordSession->AutoReadDirectory = false;
  9294. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9295. {
  9296. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9297. }
  9298. else
  9299. {
  9300. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9301. }
  9302. }
  9303. //---------------------------------------------------------------------------
  9304. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9305. {
  9306. // No nice way to assert SSH2
  9307. return (Terminal != NULL) && Terminal->Active && (Terminal->FSProtocol == cfsSFTP);
  9308. }
  9309. //---------------------------------------------------------------------------
  9310. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9311. {
  9312. TOperationVisualizer OperationVisualizer;
  9313. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9314. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9315. }
  9316. //---------------------------------------------------------------------------
  9317. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9318. {
  9319. TForm::ChangeScale(M, D);
  9320. int APixelsPerInch = GetControlPixelsPerInch(this);
  9321. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9322. try
  9323. {
  9324. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9325. }
  9326. __finally
  9327. {
  9328. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9329. }
  9330. }
  9331. //---------------------------------------------------------------------------
  9332. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9333. {
  9334. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9335. }
  9336. //---------------------------------------------------------------------
  9337. class TPublicControl : public TControl
  9338. {
  9339. friend class TCustomScpExplorerForm;
  9340. };
  9341. //---------------------------------------------------------------------------
  9342. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9343. {
  9344. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9345. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9346. }
  9347. //---------------------------------------------------------------------------
  9348. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9349. {
  9350. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9351. OpenBookmark(Side, Bookmark);
  9352. }
  9353. //---------------------------------------------------------------------------
  9354. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9355. {
  9356. DoBookmarkClick(osLocal, Sender);
  9357. }
  9358. //---------------------------------------------------------------------------
  9359. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9360. {
  9361. DoBookmarkClick(osRemote, Sender);
  9362. }
  9363. //---------------------------------------------------------------------------
  9364. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9365. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9366. {
  9367. if (BookmarkList != NULL)
  9368. {
  9369. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9370. if (!WinConfiguration->UseLocationProfiles)
  9371. {
  9372. std::unique_ptr<TStringList> Directories(new TStringList());
  9373. Directories->CaseSensitive = (Side == osRemote);
  9374. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9375. {
  9376. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9377. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9378. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9379. {
  9380. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9381. Item->Caption = EscapeHotkey(Directory);
  9382. Item->ShortCut = Bookmark->ShortCut;
  9383. Item->OnClick = OnBookmarkClick;
  9384. Item->Tag = reinterpret_cast<int>(Bookmark);
  9385. Directories->Add(Directory);
  9386. Menu->Add(Item.release());
  9387. }
  9388. }
  9389. }
  9390. else
  9391. {
  9392. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9393. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9394. {
  9395. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9396. if (!Bookmark->Node.IsEmpty())
  9397. {
  9398. Folders->Add(Bookmark->Node);
  9399. }
  9400. }
  9401. for (int Index = 0; Index < Folders->Count; Index++)
  9402. {
  9403. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9404. Item->Caption = Folders->Strings[Index];
  9405. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction->ImageIndex;
  9406. Folders->Objects[Index] = Item.get();
  9407. Menu->Add(Item.release());
  9408. }
  9409. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9410. {
  9411. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9412. TTBCustomItem * Parent;
  9413. if (!Bookmark->Node.IsEmpty())
  9414. {
  9415. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9416. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9417. }
  9418. else
  9419. {
  9420. Parent = Menu;
  9421. }
  9422. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9423. Item->Caption = Bookmark->Name;
  9424. Item->ShortCut = Bookmark->ShortCut;
  9425. Item->OnClick = OnBookmarkClick;
  9426. Item->Tag = reinterpret_cast<int>(Bookmark);
  9427. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9428. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9429. {
  9430. // No hint for location profiles that are actually mere "bookmarks"
  9431. }
  9432. else
  9433. {
  9434. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  9435. UnicodeString LongHint;
  9436. if (!Bookmark->Local.IsEmpty())
  9437. {
  9438. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  9439. }
  9440. if (!Bookmark->Remote.IsEmpty())
  9441. {
  9442. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  9443. }
  9444. AddToList(Hint, LongHint, L"|");
  9445. Item->Hint = Hint;
  9446. }
  9447. Parent->Add(Item.release());
  9448. }
  9449. }
  9450. AddMenuSeparator(Menu);
  9451. }
  9452. }
  9453. //---------------------------------------------------------------------------
  9454. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  9455. {
  9456. Menu->Clear();
  9457. std::unique_ptr<TTBCustomItem> Item;
  9458. Item.reset(new TTBXItem(Owner));
  9459. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  9460. Item->Options = Item->Options << tboDefault;
  9461. Menu->Add(Item.release());
  9462. AddMenuSeparator(Menu);
  9463. if (Terminal != NULL)
  9464. {
  9465. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  9466. }
  9467. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  9468. Item.reset(new TTBXItem(Owner));
  9469. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction : NonVisualDataModule->RemoteAddBookmarkAction;
  9470. Menu->Add(Item.release());
  9471. }
  9472. //---------------------------------------------------------------------------
  9473. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  9474. {
  9475. bool Result = true;
  9476. try
  9477. {
  9478. if (Side == osRemote)
  9479. {
  9480. Terminal->ExceptionOnFail = true;
  9481. }
  9482. try
  9483. {
  9484. DirView(Side)->Path = Path;
  9485. }
  9486. __finally
  9487. {
  9488. if (Side == osRemote)
  9489. {
  9490. Terminal->ExceptionOnFail = false;
  9491. }
  9492. }
  9493. }
  9494. catch (Exception & E)
  9495. {
  9496. if ((Side != osRemote) || Terminal->Active)
  9497. {
  9498. ShowExtendedExceptionEx(Terminal, &E);
  9499. Result = false;
  9500. }
  9501. else
  9502. {
  9503. throw;
  9504. }
  9505. }
  9506. return Result;
  9507. }
  9508. //---------------------------------------------------------------------------
  9509. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  9510. {
  9511. // Make sure there some feedback even when loading tine local folders
  9512. TInstantOperationVisualizer Visualizer;
  9513. DirView(Side)->ReloadDirectory();
  9514. }
  9515. //---------------------------------------------------------------------------
  9516. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  9517. {
  9518. if (Index == SessionsPageControl->TabIndex)
  9519. {
  9520. CloseSession();
  9521. }
  9522. else
  9523. {
  9524. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  9525. TTerminal * Terminal = GetSessionTabTerminal(TabSheet);
  9526. TTerminalManager * Manager = TTerminalManager::Instance();
  9527. if (!Terminal->Active)
  9528. {
  9529. Manager->FreeTerminal(Terminal);
  9530. }
  9531. else
  9532. {
  9533. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  9534. if (CanCloseQueue(Queue))
  9535. {
  9536. Manager->FreeTerminal(Terminal);
  9537. }
  9538. }
  9539. }
  9540. }
  9541. //---------------------------------------------------------------------------
  9542. void __fastcall TCustomScpExplorerForm::SessionsPageControlCloseButtonClick(TPageControl * /*Sender*/, int Index)
  9543. {
  9544. CloseSessionTab(Index);
  9545. }
  9546. //---------------------------------------------------------------------------
  9547. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side)
  9548. {
  9549. if (DebugAlwaysTrue(GetSide(Side) == osRemote))
  9550. {
  9551. TInstantOperationVisualizer Visualizer;
  9552. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  9553. // before we create a new data object
  9554. ClipboardClear();
  9555. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  9556. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  9557. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  9558. FClipboardFakeMonitors.reset(StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated));
  9559. if (FClipboardDragDropFilesEx.get() == NULL)
  9560. {
  9561. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  9562. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  9563. }
  9564. FClipboardDragDropFilesEx->FileList->Clear();
  9565. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  9566. FClipboardDragDropFilesEx->CopyToClipboard();
  9567. FClipboardTerminal = FTerminal;
  9568. // Need full paths, as cwd can be different once files are pasted
  9569. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(false, true)));
  9570. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  9571. {
  9572. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  9573. }
  9574. }
  9575. }
  9576. //---------------------------------------------------------------------------
  9577. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  9578. {
  9579. if (OpenClipboard(0))
  9580. {
  9581. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  9582. CloseClipboard();
  9583. }
  9584. }
  9585. //---------------------------------------------------------------------------
  9586. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  9587. {
  9588. return !FClipboardFakeDirectory.IsEmpty();
  9589. }
  9590. //---------------------------------------------------------------------------
  9591. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  9592. {
  9593. RemoveDir(ApiPath(FClipboardFakeDirectory));
  9594. FClipboardFakeDirectory = UnicodeString();
  9595. FClipboardTerminal = NULL;
  9596. // Also called in Idle()
  9597. FClipboardFakeMonitors.reset(NULL);
  9598. }
  9599. //---------------------------------------------------------------------------
  9600. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  9601. {
  9602. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  9603. {
  9604. ClipboardStop();
  9605. }
  9606. }
  9607. //---------------------------------------------------------------------------
  9608. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  9609. {
  9610. TTerminalManager * Manager = TTerminalManager::Instance();
  9611. Manager->ActiveTerminal = FClipboardTerminal;
  9612. TTransferOperationParam Params;
  9613. Params.TargetDirectory = TargetDirectory;
  9614. Params.DragDrop = DragDrop;
  9615. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  9616. }
  9617. //---------------------------------------------------------------------------
  9618. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  9619. {
  9620. // It can actually rarelly happen that some random file is created, while we are shuttting down the monitor
  9621. // (as it pumps a Windows message queue while beign shutted down)
  9622. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  9623. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  9624. {
  9625. // Can fail as it can be e.g. locked by AV, so we retry that later
  9626. bool Removed = RemoveDir(ApiPath(FileName));
  9627. try
  9628. {
  9629. if (!NonVisualDataModule->Busy)
  9630. {
  9631. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  9632. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  9633. }
  9634. }
  9635. __finally
  9636. {
  9637. if (!Removed)
  9638. {
  9639. RemoveDir(ApiPath(FileName));
  9640. }
  9641. }
  9642. }
  9643. }
  9644. //---------------------------------------------------------------------------
  9645. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  9646. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  9647. {
  9648. TFileMasks::TParams MaskParams;
  9649. MaskParams.Size = Size;
  9650. MaskParams.Modification = Modification;
  9651. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  9652. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  9653. {
  9654. bool ImplicitMatch;
  9655. if (Iter->FileMask.Matches(FileName, Directory, DirView->PathName, &MaskParams, false, ImplicitMatch) &&
  9656. !ImplicitMatch)
  9657. {
  9658. Color = Iter->Color;
  9659. break;
  9660. }
  9661. }
  9662. }
  9663. //---------------------------------------------------------------------------
  9664. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  9665. {
  9666. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9667. if (!DirView->IsEditing())
  9668. {
  9669. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  9670. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  9671. // while searching
  9672. if (Key == VK_BACK)
  9673. {
  9674. if (!FIncrementalSearch.IsEmpty())
  9675. {
  9676. if (FIncrementalSearch.Length() == 1)
  9677. {
  9678. ResetIncrementalSearch();
  9679. }
  9680. else
  9681. {
  9682. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  9683. IncrementalSearch(NewText, false, false);
  9684. }
  9685. Key = 0;
  9686. }
  9687. }
  9688. }
  9689. }
  9690. //---------------------------------------------------------------------------
  9691. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  9692. {
  9693. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9694. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  9695. {
  9696. // Filter control sequences.
  9697. // When not searching yet, prefer use of the space for toggling file selection
  9698. // (so we cannot incrementally search for a string starting with a space).
  9699. if ((Key > VK_SPACE) ||
  9700. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  9701. {
  9702. IncrementalSearch(FIncrementalSearch + Key, false, false);
  9703. Key = 0;
  9704. }
  9705. }
  9706. }
  9707. //---------------------------------------------------------------------------
  9708. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  9709. {
  9710. if (!FIncrementalSearch.IsEmpty())
  9711. {
  9712. FIncrementalSearch = L"";
  9713. DirView(osCurrent)->UpdateStatusBar();
  9714. }
  9715. }
  9716. //---------------------------------------------------------------------------
  9717. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9718. {
  9719. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  9720. if (Item == NULL)
  9721. {
  9722. MessageBeep(MB_ICONHAND);
  9723. }
  9724. else
  9725. {
  9726. TCustomDirView * ADirView = DirView(osCurrent);
  9727. {
  9728. TAutoNestingCounter Guard(FIncrementalSearching);
  9729. ADirView->FocusItem(Item);
  9730. if (ADirView->NortonLike == nlOff)
  9731. {
  9732. if ((ADirView->Selected != Item) ||
  9733. (ADirView->SelCount > 1))
  9734. {
  9735. ADirView->ClearSelection();
  9736. ADirView->Selected = Item;
  9737. }
  9738. }
  9739. }
  9740. FIncrementalSearch = Text;
  9741. Item->MakeVisible(false);
  9742. TListItem * NextItem = SearchFile(Text, true, Reverse);
  9743. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  9744. ADirView->UpdateStatusBar();
  9745. }
  9746. }
  9747. //---------------------------------------------------------------------------
  9748. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  9749. {
  9750. int Index = Item->Index;
  9751. if (!Reverse)
  9752. {
  9753. Index++;
  9754. if (Index >= Item->Owner->Count)
  9755. {
  9756. Index = 0;
  9757. }
  9758. }
  9759. else
  9760. {
  9761. Index--;
  9762. if (Index < 0)
  9763. {
  9764. Index = Item->Owner->Count - 1;
  9765. }
  9766. }
  9767. return Item->Owner->Item[Index];
  9768. }
  9769. //---------------------------------------------------------------------------
  9770. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9771. {
  9772. TCustomDirView * ADirView = DirView(osCurrent);
  9773. if (ADirView->Items->Count == 0)
  9774. {
  9775. return NULL;
  9776. }
  9777. else
  9778. {
  9779. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  9780. TListItem * Item = CurrentItem;
  9781. if (SkipCurrent)
  9782. {
  9783. Item = DebugNotNull(GetNextFile(Item, Reverse));
  9784. }
  9785. while (true)
  9786. {
  9787. bool Matches = false;
  9788. switch (WinConfiguration->PanelSearch)
  9789. {
  9790. case isNameStartOnly:
  9791. Matches = ContainsTextSemiCaseSensitive(Item->Caption.SubString(1, Text.Length()), Text);
  9792. break;
  9793. case isName:
  9794. Matches = ContainsTextSemiCaseSensitive(Item->Caption, Text);
  9795. break;
  9796. case isAll:
  9797. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  9798. int Index = 0;
  9799. while ((Index < ColCount) && !Matches)
  9800. {
  9801. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  9802. if (Visible)
  9803. {
  9804. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  9805. }
  9806. Index++;
  9807. }
  9808. break;
  9809. }
  9810. if (Matches)
  9811. {
  9812. return Item;
  9813. }
  9814. Item = GetNextFile(Item, Reverse);
  9815. if (Item == CurrentItem)
  9816. {
  9817. return NULL;
  9818. }
  9819. }
  9820. }
  9821. }
  9822. //---------------------------------------------------------------------------
  9823. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  9824. {
  9825. ResetIncrementalSearch();
  9826. }
  9827. //---------------------------------------------------------------------------
  9828. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  9829. {
  9830. if (Message.CharCode == VK_TAB)
  9831. {
  9832. if (!FIncrementalSearch.IsEmpty())
  9833. {
  9834. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  9835. bool Reverse = Shift.Contains(ssShift);
  9836. IncrementalSearch(FIncrementalSearch, true, Reverse);
  9837. Message.Result = 1;
  9838. return;
  9839. }
  9840. }
  9841. else if (Message.CharCode == VK_ESCAPE)
  9842. {
  9843. if (!FIncrementalSearch.IsEmpty())
  9844. {
  9845. ResetIncrementalSearch();
  9846. Message.Result = 1;
  9847. return;
  9848. }
  9849. }
  9850. TForm::Dispatch(&Message);
  9851. }
  9852. //---------------------------------------------------------------------------
  9853. void __fastcall TCustomScpExplorerForm::Deactivate()
  9854. {
  9855. TForm::Deactivate();
  9856. // E.g. when switching to an internal editor window
  9857. ResetIncrementalSearch();
  9858. }
  9859. //---------------------------------------------------------------------------
  9860. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  9861. {
  9862. // When switching to another application
  9863. ResetIncrementalSearch();
  9864. }
  9865. //---------------------------------------------------------------------------
  9866. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  9867. {
  9868. ResetIncrementalSearch();
  9869. }
  9870. //---------------------------------------------------------------------------
  9871. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  9872. {
  9873. if (FIncrementalSearching <= 0)
  9874. {
  9875. ResetIncrementalSearch();
  9876. }
  9877. }
  9878. //---------------------------------------------------------------------------
  9879. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  9880. {
  9881. CountClicksForWindowPrint(this);
  9882. }
  9883. //---------------------------------------------------------------------------