CustomScpExplorer.cpp 351 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709
  1. //---------------------------------------------------------------------------
  2. #define NO_WIN32_LEAN_AND_MEAN
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include <Common.h>
  6. #include "CustomScpExplorer.h"
  7. #include <Bookmarks.h>
  8. #include <Interface.h>
  9. #include <Exceptions.h>
  10. #include <CoreMain.h>
  11. #include <FileSystems.h>
  12. #include <TextsCore.h>
  13. #include <TextsWin.h>
  14. #include <HelpWin.h>
  15. #include <VCLCommon.h>
  16. #include <Progress.h>
  17. #include <SynchronizeProgress.h>
  18. #include <DragExt.h>
  19. #include <WinApi.h>
  20. #include "GUITools.h"
  21. #include "NonVisual.h"
  22. #include "Glyphs.h"
  23. #include "Tools.h"
  24. #include "WinConfiguration.h"
  25. #include "TerminalManager.h"
  26. #include "EditorManager.h"
  27. #include "ProgParams.h"
  28. #include "Setup.h"
  29. #include <Consts.hpp>
  30. #include <DateUtils.hpp>
  31. #include <TB2Common.hpp>
  32. #include <DirectoryMonitor.hpp>
  33. #include <System.IOUtils.hpp>
  34. //---------------------------------------------------------------------------
  35. #pragma package(smart_init)
  36. #pragma link "CustomDirView"
  37. #pragma link "CustomUnixDirView"
  38. #pragma link "IEListView"
  39. #pragma link "NortonLikeListView"
  40. #pragma link "UnixDirView"
  41. #pragma link "CustomDriveView"
  42. #pragma link "UnixDriveView"
  43. #pragma link "CustomDriveView"
  44. #pragma link "TB2Dock"
  45. #pragma link "TBXStatusBars"
  46. #pragma link "TB2Item"
  47. #pragma link "TB2Toolbar"
  48. #pragma link "ThemePageControl"
  49. #pragma link "PathLabel"
  50. #ifndef NO_RESOURCES
  51. #pragma resource "*.dfm"
  52. #endif
  53. //---------------------------------------------------------------------------
  54. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  55. static const int SessionPanelCount = 4;
  56. //---------------------------------------------------------------------------
  57. class TMutexGuard
  58. {
  59. public:
  60. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  61. int Timeout = 5000)
  62. {
  63. FMutex = NULL;
  64. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  65. if (WaitResult == WAIT_TIMEOUT)
  66. {
  67. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  68. }
  69. else
  70. {
  71. FMutex = AMutex;
  72. }
  73. }
  74. ~TMutexGuard()
  75. {
  76. if (FMutex != NULL)
  77. {
  78. ReleaseMutex(FMutex);
  79. }
  80. }
  81. private:
  82. HANDLE FMutex;
  83. };
  84. //---------------------------------------------------------------------------
  85. class TWindowLock
  86. {
  87. public:
  88. TWindowLock(TCustomScpExplorerForm * Form) :
  89. FForm(Form)
  90. {
  91. FForm->LockWindow();
  92. }
  93. ~TWindowLock()
  94. {
  95. FForm->UnlockWindow();
  96. }
  97. private:
  98. TCustomScpExplorerForm * FForm;
  99. };
  100. //---------------------------------------------------------------------------
  101. class TAutoBatch
  102. {
  103. public:
  104. TAutoBatch(TCustomScpExplorerForm * Form) :
  105. FForm(Form)
  106. {
  107. FForm->BatchStart(FBatchStorage);
  108. }
  109. ~TAutoBatch()
  110. {
  111. FForm->BatchEnd(FBatchStorage);
  112. }
  113. private:
  114. TCustomScpExplorerForm * FForm;
  115. void * FBatchStorage;
  116. };
  117. //---------------------------------------------------------------------------
  118. struct TTransferOperationParam
  119. {
  120. TTransferOperationParam();
  121. UnicodeString TargetDirectory;
  122. bool Temp;
  123. bool DragDrop;
  124. int Options;
  125. TAutoSwitch Queue;
  126. };
  127. //---------------------------------------------------------------------------
  128. TTransferOperationParam::TTransferOperationParam()
  129. {
  130. Temp = false;
  131. DragDrop = false;
  132. Options = 0;
  133. Queue = asAuto;
  134. }
  135. //---------------------------------------------------------------------------
  136. class TTransferPresetNoteData : public TObject
  137. {
  138. public:
  139. UnicodeString Message;
  140. };
  141. //---------------------------------------------------------------------------
  142. class TTerminalNoteData : public TObject
  143. {
  144. public:
  145. TManagedTerminal * Terminal;
  146. };
  147. //---------------------------------------------------------------------------
  148. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  149. FFormRestored(false),
  150. TForm(Owner)
  151. {
  152. FCurrentSide = osRemote;
  153. FEverShown = false;
  154. FDocks = new TList();
  155. InitControls();
  156. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  157. // So that UpdateCustomCommandsToolbar called from RestoreParams works
  158. NonVisualDataModule->ScpExplorer = this;
  159. RestoreParams();
  160. ConfigurationChanged();
  161. RemoteDirView->Invalidate();
  162. FAutoOperation = false;
  163. FOnFileOperationFinished = NULL;
  164. FForceExecution = false;
  165. FIgnoreNextDialogChar = 0;
  166. FErrorList = NULL;
  167. FSynchronizeProgressForm = NULL;
  168. FProgressForm = NULL;
  169. FRefreshLocalDirectory = false;
  170. FRefreshRemoteDirectory = false;
  171. FDDExtMapFile = NULL;
  172. // CreateMutexW keeps failing with ERROR_NOACCESS
  173. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  174. DebugAssert(FDDExtMutex != NULL);
  175. FDDTargetControl = NULL;
  176. FDelayedDeletionTimer = NULL;
  177. FDelayedDeletionList = new TStringList();
  178. FDDFileList = NULL;
  179. FPendingTempSpaceWarn = false;
  180. FCapturedLog = NULL;
  181. FDragDropOperation = false;
  182. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  183. FAllowTransferPresetAutoSelect = true;
  184. FCopyParamDefault = L"";
  185. FSynchronizeController = NULL;
  186. FPendingQueueActionItem = NULL;
  187. FLockLevel = 0;
  188. FLockSuspendLevel = 0;
  189. FDisabledOnLockSuspend = false;
  190. FAlternativeDelete = false;
  191. FTrayIcon = new ::TTrayIcon(0);
  192. FTrayIcon->OnClick = TrayIconClick;
  193. FMaxQueueLength = 0;
  194. FLastContextPopupScreenPoint = TPoint(-1, -1);
  195. FTransferResumeList = NULL;
  196. FMoveToQueue = false;
  197. FStandaloneEditing = false;
  198. FOnFeedSynchronizeError = NULL;
  199. FNeedSession = false;
  200. FDoNotIdleCurrentTerminal = 0;
  201. FIncrementalSearching = 0;
  202. FIncrementalSearchHaveNext = false;
  203. FEditorManager = new TEditorManager();
  204. FEditorManager->OnFileChange = ExecutedFileChanged;
  205. FEditorManager->OnFileReload = ExecutedFileReload;
  206. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  207. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  208. FLocalEditors = new TList();
  209. FQueueStatus = NULL;
  210. FQueueStatusSection = new TCriticalSection();
  211. FQueueStatusInvalidated = false;
  212. FQueueItemInvalidated = false;
  213. FQueueActedItem = NULL;
  214. FQueueController = new TQueueController(QueueView3);
  215. FUserActionTimer = new TTimer(this);
  216. FUserActionTimer->Enabled = false;
  217. FUserActionTimer->Interval = 10;
  218. FUserActionTimer->OnTimer = UserActionTimer;
  219. FNotes = new TStringList();
  220. FNoteTimer = new TTimer(this);
  221. FNoteTimer->Enabled = false;
  222. FNoteTimer->OnTimer = NoteTimer;
  223. FOnNoteClick = NULL;
  224. FOle32Library = LoadLibrary(L"Ole32.dll");
  225. FDragMoveCursor = FOle32Library != NULL ?
  226. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  227. UseSystemSettings(this);
  228. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  229. static_cast<TObject*>(GetComponent(fcTransferList)));
  230. DebugAssert(TransferList != NULL);
  231. FTransferListHoverIndex = -1;
  232. TransferList->OnChange = TransferListChange;
  233. TransferList->OnDrawItem = TransferListDrawItem;
  234. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))));
  235. SetSubmenu(NonVisualDataModule->ColorMenuItem);
  236. UseDesktopFont(SessionsPageControl);
  237. UpdateSessionsPageControlHeight();
  238. UseDesktopFont(RemoteDirView);
  239. UseDesktopFont(RemoteDriveView);
  240. UseDesktopFont(QueueView3);
  241. UseDesktopFont(QueueLabel);
  242. UseDesktopFont(RemoteStatusBar);
  243. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  244. QueueSplitter->ShowHint = true;
  245. RemotePanelSplitter->ShowHint = true;
  246. UpdateImages();
  247. FCustomCommandMenu = CreateTBXPopupMenu(this);
  248. FCustomCommandLocalFileList = NULL;
  249. FCustomCommandRemoteFileList = NULL;
  250. FSessionColors = new TPngImageList(this);
  251. FSessionColors->ColorDepth = cd32Bit;
  252. AddFixedSessionImages();
  253. SessionsPageControl->Images = FSessionColors;
  254. QueueLabel->FocusControl = QueueView3;
  255. UpdateQueueLabel();
  256. FRemoteDirViewWasFocused = true;
  257. CreateHiddenWindow();
  258. StartUpdates();
  259. }
  260. //---------------------------------------------------------------------------
  261. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  262. {
  263. // this has to be one of the very first things to do
  264. StopUpdateThread();
  265. // This is needed when shuting down Windows only. Otherwise it's already set NULL from Execute()
  266. TTerminalManager::Instance()->ScpExplorer = NULL;
  267. delete FCustomCommandLocalFileList;
  268. delete FCustomCommandRemoteFileList;
  269. delete FCustomCommandMenu;
  270. delete FTrayIcon;
  271. FTrayIcon = NULL;
  272. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  273. delete FEditorManager;
  274. ForceCloseLocalEditors();
  275. delete FLocalEditors;
  276. HideFileFindDialog();
  277. if (FDelayedDeletionTimer)
  278. {
  279. DoDelayedDeletion(NULL);
  280. SAFE_DESTROY(FDelayedDeletionTimer);
  281. }
  282. SAFE_DESTROY(FDelayedDeletionList);
  283. // sometimes we do not get DDEnd so the list is not released
  284. SAFE_DESTROY(FDDFileList);
  285. DebugAssert(FSynchronizeController == NULL);
  286. CloseHandle(FDDExtMutex);
  287. FDDExtMutex = NULL;
  288. FreeLibrary(FOle32Library);
  289. FOle32Library = NULL;
  290. FDragMoveCursor = NULL;
  291. DebugAssert(!FErrorList);
  292. if (FEverShown)
  293. {
  294. // when window is never shown (like when running command-line operation),
  295. // particularly window site is not restored correctly (BoundsRect value set
  296. // in RestoreForm gets lost during handle allocation), so we do not want
  297. // it to be stored
  298. StoreParams();
  299. }
  300. Terminal = NULL;
  301. Queue = NULL;
  302. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  303. NonVisualDataModule->ScpExplorer = NULL;
  304. delete FQueueController;
  305. FQueueController = NULL;
  306. delete FQueueStatusSection;
  307. FQueueStatusSection = NULL;
  308. delete FQueueStatus;
  309. FQueueStatus = NULL;
  310. delete FUserActionTimer;
  311. FUserActionTimer = NULL;
  312. delete FNoteTimer;
  313. delete FNotes;
  314. delete FNoteData;
  315. SAFE_DESTROY(FDocks);
  316. SessionsPageControl->Images = NULL;
  317. // only after clearing Terminal (after DoTerminalListChanged => ... => AddSessionColor is called)
  318. SAFE_DESTROY(FSessionColors);
  319. SAFE_DESTROY(FSessionsDragDropFilesEx);
  320. SAFE_DESTROY(FHistoryMenu[0][0]);
  321. SAFE_DESTROY(FHistoryMenu[0][1]);
  322. SAFE_DESTROY(FHistoryMenu[1][0]);
  323. SAFE_DESTROY(FHistoryMenu[1][1]);
  324. if (FHiddenWindow != NULL)
  325. {
  326. DestroyWindow(FHiddenWindow);
  327. FHiddenWindow = NULL;
  328. }
  329. }
  330. //---------------------------------------------------------------------------
  331. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  332. {
  333. std::unique_ptr<TSessionData> Data;
  334. if (!Session.IsEmpty())
  335. {
  336. bool DefaultsOnly;
  337. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  338. }
  339. TTerminalManager * Manager = TTerminalManager::Instance();
  340. for (int Index = 0; Index < Manager->Count; Index++)
  341. {
  342. TTerminal * Terminal = Manager->Terminals[Index];
  343. if (Session.IsEmpty() ||
  344. Terminal->SessionData->IsSameSite(Data.get()))
  345. {
  346. if (Path.IsEmpty())
  347. {
  348. Terminal->ClearCaches();
  349. }
  350. else
  351. {
  352. Terminal->DirectoryModified(Path, true);
  353. }
  354. }
  355. }
  356. // We should flag a pending refresh for the background terminals or busy foreground terminals
  357. if ((Terminal != NULL) && Terminal->Active &&
  358. CanCommandLineFromAnotherInstance() &&
  359. (Session.IsEmpty() || Terminal->SessionData->IsSameSite(Data.get())) &&
  360. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  361. {
  362. Terminal->ReloadDirectory();
  363. }
  364. }
  365. //---------------------------------------------------------------------------
  366. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  367. {
  368. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  369. size_t MessageSize = sizeof(TCopyDataMessage);
  370. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  371. if (Result)
  372. {
  373. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  374. Result = (Message.Version == TCopyDataMessage::Version1);
  375. if (Result)
  376. {
  377. switch (Message.Command)
  378. {
  379. case TCopyDataMessage::CommandCanCommandLine:
  380. Result = CanCommandLineFromAnotherInstance();
  381. break;
  382. case TCopyDataMessage::CommandCommandLine:
  383. {
  384. UnicodeString CommandLine(Message.CommandLine);
  385. Result = CommandLineFromAnotherInstance(CommandLine);
  386. }
  387. break;
  388. case TCopyDataMessage::RefreshPanel:
  389. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  390. break;
  391. case TCopyDataMessage::MainWindowCheck:
  392. Result = true;
  393. break;
  394. default:
  395. Result = false;
  396. break;
  397. }
  398. }
  399. }
  400. Message.Result = Result ? 1 : 0;
  401. }
  402. //---------------------------------------------------------------------------
  403. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  404. {
  405. WNDCLASS WindowClass = {0};
  406. WindowClass.lpfnWndProc = DefWindowProc;
  407. WindowClass.hInstance = HInstance;
  408. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  409. FHiddenWindow = NULL;
  410. if (RegisterClass(&WindowClass))
  411. {
  412. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  413. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  414. }
  415. }
  416. //---------------------------------------------------------------------------
  417. bool __fastcall TCustomScpExplorerForm::CanConsole()
  418. {
  419. return
  420. (Terminal != NULL) &&
  421. Terminal->Active &&
  422. (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  423. }
  424. //---------------------------------------------------------------------------
  425. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  426. {
  427. bool Result = Visible && !NonVisualDataModule->Busy;
  428. return Result;
  429. }
  430. //---------------------------------------------------------------------------
  431. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  432. const UnicodeString & CommandLine)
  433. {
  434. TProgramParams Params(CommandLine);
  435. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  436. if (Result)
  437. {
  438. NonVisualDataModule->StartBusy();
  439. try
  440. {
  441. // this action is initiated from another process,
  442. // so it's likely that our window is not visible,
  443. // and user won't see what is going on
  444. Application->BringToFront();
  445. // reload sessions as we may be asked to open a session
  446. // just stored by another instance
  447. StoredSessions->Reload();
  448. UnicodeString SessionName = Params.Param[1];
  449. std::unique_ptr<TObjectList> DataList(new TObjectList());
  450. UnicodeString DownloadFile; // unused
  451. try
  452. {
  453. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this);
  454. }
  455. catch (EAbort &)
  456. {
  457. // Happens when opening session in PuTTY and in other situations,
  458. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  459. // to process the command-line on its own (or to pass it to yet another instance)
  460. }
  461. if (DataList->Count > 0)
  462. {
  463. TTerminalManager * Manager = TTerminalManager::Instance();
  464. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  465. }
  466. }
  467. __finally
  468. {
  469. NonVisualDataModule->EndBusy();
  470. }
  471. }
  472. return Result;
  473. }
  474. //---------------------------------------------------------------------------
  475. void __fastcall TCustomScpExplorerForm::SetTerminal(TManagedTerminal * value)
  476. {
  477. if (FTerminal != value)
  478. {
  479. TerminalChanging();
  480. DoSetTerminal(value, false);
  481. }
  482. }
  483. //---------------------------------------------------------------------------
  484. void __fastcall TCustomScpExplorerForm::DoSetTerminal(TManagedTerminal * value, bool Replace)
  485. {
  486. FTerminal = value;
  487. bool PrevAllowTransferPresetAutoSelect = FAllowTransferPresetAutoSelect;
  488. FAllowTransferPresetAutoSelect = false;
  489. try
  490. {
  491. TerminalChanged(Replace);
  492. }
  493. __finally
  494. {
  495. FAllowTransferPresetAutoSelect = PrevAllowTransferPresetAutoSelect;
  496. }
  497. if (Terminal != NULL)
  498. {
  499. TransferPresetAutoSelect();
  500. }
  501. }
  502. //---------------------------------------------------------------------------
  503. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TManagedTerminal * value)
  504. {
  505. DoSetTerminal(value, true);
  506. }
  507. //---------------------------------------------------------------------------
  508. void __fastcall TCustomScpExplorerForm::TerminalChanging()
  509. {
  510. if (FTerminal != NULL)
  511. {
  512. UpdateTerminal(Terminal);
  513. }
  514. }
  515. //---------------------------------------------------------------------------
  516. void __fastcall TCustomScpExplorerForm::TerminalChanged(bool Replaced)
  517. {
  518. if (Terminal != NULL)
  519. {
  520. UpdateSessionColor((TColor)Terminal->StateData->Color);
  521. }
  522. DoTerminalListChanged();
  523. DebugAssert(!IsLocalBrowserMode()); // TODO
  524. if (Replaced)
  525. {
  526. RemoteDirView->ReplaceTerminal(Terminal);
  527. }
  528. else
  529. {
  530. RemoteDirView->Terminal = Terminal;
  531. }
  532. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  533. if (Terminal != NULL)
  534. {
  535. if (Terminal->Active)
  536. {
  537. Terminal->RefreshDirectory();
  538. }
  539. if (WinConfiguration->PreservePanelState)
  540. {
  541. if (Terminal->RemoteExplorerState != NULL)
  542. {
  543. DirView(osRemote)->RestoreState(Terminal->RemoteExplorerState);
  544. }
  545. else
  546. {
  547. DirView(osRemote)->ClearState();
  548. }
  549. }
  550. if (!Replaced && Terminal->Active)
  551. {
  552. InitStatusBar();
  553. }
  554. }
  555. UpdateTransferList();
  556. // Update panels Enable state before refreshing the labels
  557. UpdateControls();
  558. }
  559. //---------------------------------------------------------------------------
  560. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  561. {
  562. if (Queue != value)
  563. {
  564. if (FQueue != NULL)
  565. {
  566. FQueue->OnListUpdate = NULL;
  567. FQueue->OnQueueItemUpdate = NULL;
  568. }
  569. FQueue = value;
  570. if (FQueue != NULL)
  571. {
  572. DebugAssert(FQueue->OnListUpdate == NULL);
  573. FQueue->OnListUpdate = QueueListUpdate;
  574. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  575. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  576. }
  577. QueueChanged();
  578. }
  579. }
  580. //---------------------------------------------------------------------------
  581. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  582. TListItem * Item)
  583. {
  584. if (FQueueActedItem == Item)
  585. {
  586. FQueueActedItem = NULL;
  587. if ((QueueView3->PopupMenu != NULL) &&
  588. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  589. {
  590. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  591. ReleaseCapture();
  592. }
  593. }
  594. if (Item->Data == FPendingQueueActionItem)
  595. {
  596. FPendingQueueActionItem = NULL;
  597. }
  598. }
  599. //---------------------------------------------------------------------------
  600. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  601. {
  602. {
  603. TGuard Guard(FQueueStatusSection);
  604. FQueueStatusInvalidated = false;
  605. if (FQueue != NULL)
  606. {
  607. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  608. }
  609. }
  610. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  611. {
  612. FMaxQueueLength = FQueueStatus->Count;
  613. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  614. }
  615. FQueueController->UpdateQueueStatus(FQueueStatus);
  616. SetQueueProgress();
  617. UpdateQueueView();
  618. bool IsEmpty = (FQueueStatus == NULL) || (FQueueStatus->Count == 0);
  619. if (IsEmpty && (Terminal != NULL))
  620. {
  621. TOnceDoneOperation OnceDoneOperation =
  622. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  623. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  624. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  625. {
  626. FQueue->Enabled = false;
  627. }
  628. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  629. {
  630. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  631. }
  632. }
  633. }
  634. //---------------------------------------------------------------------------
  635. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  636. {
  637. UnicodeString Result;
  638. if (FQueueStatus != NULL)
  639. {
  640. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  641. {
  642. TFileOperationProgressType * ProgressData =
  643. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  644. if ((ProgressData != NULL) && ProgressData->InProgress)
  645. {
  646. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  647. }
  648. }
  649. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  650. {
  651. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  652. }
  653. }
  654. return Result;
  655. }
  656. //---------------------------------------------------------------------------
  657. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  658. {
  659. ComponentVisible[fcQueueView] =
  660. (WinConfiguration->QueueView.Show == qvShow) ||
  661. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  662. (FQueueStatus != NULL) && (FQueueStatus->Count > 0));
  663. }
  664. //---------------------------------------------------------------------------
  665. void __fastcall TCustomScpExplorerForm::QueueChanged()
  666. {
  667. if (FQueueStatus != NULL)
  668. {
  669. delete FQueueStatus;
  670. FQueueStatus = NULL;
  671. }
  672. UpdateQueueStatus(true);
  673. }
  674. //---------------------------------------------------------------------------
  675. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  676. {
  677. if (FQueue == Queue)
  678. {
  679. FQueueStatusInvalidated = true;
  680. }
  681. }
  682. //---------------------------------------------------------------------------
  683. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  684. TQueueItem * Item)
  685. {
  686. if (FQueue == Queue)
  687. {
  688. TGuard Guard(FQueueStatusSection);
  689. // this may be running in parallel with QueueChanged
  690. if (FQueueStatus != NULL)
  691. {
  692. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  693. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  694. {
  695. FRefreshLocalDirectory = (QueueItem == NULL) ||
  696. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  697. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  698. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  699. }
  700. if (QueueItem != NULL)
  701. {
  702. QueueItem->UserData = (void*)true;
  703. FQueueItemInvalidated = true;
  704. }
  705. }
  706. }
  707. }
  708. //---------------------------------------------------------------------------
  709. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  710. {
  711. // during standalone editing, we have no way to see/control queue,
  712. // so we have to always popup prompts automatically
  713. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  714. }
  715. //---------------------------------------------------------------------------
  716. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  717. {
  718. if (FQueueStatus != NULL)
  719. {
  720. bool QueueAutoPopup = IsQueueAutoPopup();
  721. bool NeedRefresh = FQueueController->NeedRefresh();
  722. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  723. FQueueItemInvalidated = false;
  724. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  725. bool Updated = false;
  726. TQueueItemProxy * QueueItem;
  727. bool WasUserAction;
  728. for (int Index = 0; Index < Limit; Index++)
  729. {
  730. bool Update = false;
  731. QueueItem = FQueueStatus->Items[Index];
  732. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  733. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  734. (FPendingQueueActionItem == NULL))
  735. {
  736. FPendingQueueActionItem = QueueItem;
  737. FUserActionTimer->Enabled = true;
  738. }
  739. if ((bool)QueueItem->UserData)
  740. {
  741. QueueItem->UserData = (void*)false;
  742. QueueItem->Update();
  743. Updated = true;
  744. Update = true;
  745. }
  746. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  747. {
  748. Update = true;
  749. }
  750. if (Update)
  751. {
  752. FQueueController->RefreshQueueItem(QueueItem);
  753. }
  754. }
  755. if (Updated)
  756. {
  757. NonVisualDataModule->UpdateNonVisibleActions();
  758. SetQueueProgress();
  759. }
  760. }
  761. }
  762. //---------------------------------------------------------------------------
  763. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  764. {
  765. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  766. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  767. }
  768. //---------------------------------------------------------------------------
  769. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(int Progress)
  770. {
  771. if (Progress >= 0)
  772. {
  773. FTaskbarList->SetProgressValue(GetMainForm()->Handle, Progress, 100);
  774. }
  775. else
  776. {
  777. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  778. }
  779. }
  780. //---------------------------------------------------------------------------
  781. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  782. {
  783. int OverallProgress;
  784. // FProgressForm is null when this is called from SetQueueProgress
  785. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  786. {
  787. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  788. }
  789. else if (!TFileOperationProgressType::IsIndeterminateOperation(ProgressData->Operation))
  790. {
  791. OverallProgress = ProgressData->OverallProgress();
  792. }
  793. else
  794. {
  795. OverallProgress = -1;
  796. }
  797. SetTaskbarListProgressValue(OverallProgress);
  798. }
  799. //---------------------------------------------------------------------------
  800. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  801. {
  802. TTerminalManager::Instance()->QueueStatusUpdated();
  803. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  804. {
  805. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  806. {
  807. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  808. {
  809. TFileOperationProgressType * ProgressData = NULL;
  810. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  811. {
  812. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  813. }
  814. if ((ProgressData != NULL) &&
  815. ProgressData->InProgress)
  816. {
  817. SetTaskbarListProgressValue(ProgressData);
  818. }
  819. else
  820. {
  821. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  822. }
  823. }
  824. else
  825. {
  826. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  827. }
  828. }
  829. else
  830. {
  831. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  832. }
  833. }
  834. UpdateQueueLabel();
  835. }
  836. //---------------------------------------------------------------------------
  837. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  838. {
  839. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  840. if (FQueueStatus != NULL)
  841. {
  842. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  843. if (ActiveAndPendingPrimaryCount > 0)
  844. {
  845. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  846. }
  847. }
  848. QueueLabel->Caption = Caption;
  849. }
  850. //---------------------------------------------------------------------------
  851. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  852. {
  853. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  854. static_cast<TComponent*>(GetComponent(fcTransferList)));
  855. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  856. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  857. TransferList->Strings->BeginUpdate();
  858. try
  859. {
  860. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  861. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  862. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  863. if (FTransferDropDownHint.IsEmpty())
  864. {
  865. FTransferDropDownHint = TransferDropDown->Hint;
  866. }
  867. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  868. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  869. TransferDropDown->Text = StripHotkey(Name);
  870. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  871. (FTransferDropDownHint, StripHotkey(Name),
  872. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  873. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General))));
  874. // update the label, otherwise when it is updated only on the first draw
  875. // of the list, it is drawn "bold" for some reason
  876. FTransferListHoverIndex = TransferList->ItemIndex;
  877. UpdateTransferLabel();
  878. }
  879. __finally
  880. {
  881. TransferList->Strings->EndUpdate();
  882. }
  883. }
  884. //---------------------------------------------------------------------------
  885. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  886. {
  887. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  888. DebugAssert(Toolbar != NULL);
  889. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  890. }
  891. //---------------------------------------------------------------------------
  892. void __fastcall TCustomScpExplorerForm::UpdateActions()
  893. {
  894. TForm::UpdateActions();
  895. if (ComponentVisible[fcCustomCommandsBand])
  896. {
  897. UpdateCustomCommandsToolbar();
  898. }
  899. }
  900. //---------------------------------------------------------------------------
  901. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  902. {
  903. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  904. }
  905. //---------------------------------------------------------------------------
  906. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  907. {
  908. Color = GetBtnFaceColor();
  909. DebugAssert(Configuration && RemoteDirView);
  910. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  911. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  912. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  913. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  914. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  915. if (RemoteDirView->RowSelect != WinConfiguration->FullRowSelect)
  916. {
  917. RemoteDirView->RowSelect = WinConfiguration->FullRowSelect;
  918. // selection is not redrawn automatically when RowSelect changes
  919. RemoteDirView->Invalidate();
  920. }
  921. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  922. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  923. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  924. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  925. UpdateSessionsPageControlHeight();
  926. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  927. UpdateToolbarDisplayMode();
  928. UpdateTransferList();
  929. if (Terminal != NULL)
  930. {
  931. TransferPresetAutoSelect();
  932. }
  933. UpdateQueueView();
  934. UpdateControls();
  935. // this can be called even before constuctor finishes.
  936. if (FEditorManager != NULL)
  937. {
  938. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  939. }
  940. // this can be called even before constuctor finishes.
  941. if (FLocalEditors != NULL)
  942. {
  943. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  944. {
  945. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  946. }
  947. }
  948. if (ComponentVisible[fcCustomCommandsBand])
  949. {
  950. UpdateCustomCommandsToolbar();
  951. }
  952. // show only when keeping queue items forever,
  953. // otherwise, its enough to have in in the context menu
  954. QueueDeleteAllDoneQueueToolbarItem->Visible =
  955. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  956. if (FFileColorsCurrent != WinConfiguration->FileColors)
  957. {
  958. TFileColorData::LoadList(WinConfiguration->FileColors, FFileColors);
  959. FFileColorsCurrent = WinConfiguration->FileColors;
  960. FileColorsChanged();
  961. }
  962. }
  963. //---------------------------------------------------------------------------
  964. void __fastcall TCustomScpExplorerForm::DoFileColorsChanged(TCustomDirView * DirView)
  965. {
  966. DirView->OnGetItemColor = FFileColors.empty() ? TDirViewGetItemColorEvent(NULL) : TDirViewGetItemColorEvent(&DirViewGetItemColor);
  967. DirView->Invalidate();
  968. }
  969. //---------------------------------------------------------------------------
  970. void __fastcall TCustomScpExplorerForm::FileColorsChanged()
  971. {
  972. DoFileColorsChanged(RemoteDirView);
  973. }
  974. //---------------------------------------------------------------------------
  975. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  976. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  977. void * /*Arg*/)
  978. {
  979. if (Token != NULL)
  980. {
  981. TForm * Editor = dynamic_cast<TForm*>(Token);
  982. DebugAssert(Editor != NULL);
  983. ReconfigureEditorForm(Editor);
  984. }
  985. }
  986. //---------------------------------------------------------------------------
  987. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  988. {
  989. WinConfiguration->DDTransferConfirmation = asOn;
  990. }
  991. //---------------------------------------------------------------------------
  992. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  993. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  994. {
  995. // noop
  996. }
  997. //---------------------------------------------------------------------------
  998. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  999. TTransferDirection Direction, TTransferType Type, bool Temp,
  1000. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  1001. bool Confirm, bool DragDrop, int Options)
  1002. {
  1003. bool Result = true;
  1004. DebugAssert(Terminal && Terminal->Active);
  1005. // these parameters are known in advance
  1006. int Params =
  1007. FLAGMASK(Type == ttMove, cpDelete);
  1008. bool ToTemp = (Temp && (Direction == tdToLocal));
  1009. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  1010. {
  1011. bool DisableNewerOnly =
  1012. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  1013. ToTemp;
  1014. Options |=
  1015. FLAGMASK(ToTemp, coTemp) |
  1016. FLAGMASK(!Terminal->IsCapable[fsBackgroundTransfers], coDisableQueue) |
  1017. coDoNotShowAgain;
  1018. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1019. int CopyParamAttrs = (Direction == tdToRemote ?
  1020. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1021. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1022. int OutputOptions =
  1023. FLAGMASK(DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto),
  1024. cooDoNotShowAgain);
  1025. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1026. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1027. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1028. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions, -1);
  1029. if (Result)
  1030. {
  1031. if (FLAGSET(OutputOptions, cooDoNotShowAgain))
  1032. {
  1033. if (DragDrop)
  1034. {
  1035. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1036. {
  1037. PopupTrayBalloon(NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation,
  1038. NULL, 0, EnableDDTransferConfirmation, NULL);
  1039. }
  1040. WinConfiguration->DDTransferConfirmation = asOff;
  1041. }
  1042. else
  1043. {
  1044. WinConfiguration->ConfirmTransferring = false;
  1045. }
  1046. }
  1047. else
  1048. {
  1049. // User explicitly unchecked "do not show again",
  1050. // so show him the dialog the next time
  1051. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1052. {
  1053. WinConfiguration->DDTransferConfirmation = asOn;
  1054. }
  1055. }
  1056. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1057. }
  1058. }
  1059. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fsBackgroundTransfers])
  1060. {
  1061. Configuration->Usage->Inc(L"TransfersOnBackground");
  1062. // these parameter are known only after transfer dialog
  1063. Params |=
  1064. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1065. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1066. Result = false;
  1067. ClearTransferSourceSelection(Direction);
  1068. }
  1069. return Result;
  1070. }
  1071. //---------------------------------------------------------------------------
  1072. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1073. {
  1074. if (FOnFileOperationFinished != NULL)
  1075. {
  1076. FOnFileOperationFinished(UnicodeString(), true);
  1077. }
  1078. else
  1079. {
  1080. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1081. if (HasDirView[Side])
  1082. {
  1083. DirView(Side)->SelectAll(smNone);
  1084. }
  1085. }
  1086. }
  1087. //---------------------------------------------------------------------------
  1088. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1089. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1090. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1091. int Params)
  1092. {
  1093. DebugAssert(Queue != NULL);
  1094. bool SingleFile = false;
  1095. if (FileList->Count == 1)
  1096. {
  1097. if (Direction == tdToRemote)
  1098. {
  1099. UnicodeString FileName = FileList->Strings[0];
  1100. SingleFile = FileExists(ApiPath(FileName));
  1101. }
  1102. else
  1103. {
  1104. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1105. SingleFile = !File->IsDirectory;
  1106. }
  1107. }
  1108. TQueueItem * QueueItem;
  1109. if (Direction == tdToRemote)
  1110. {
  1111. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1112. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1113. }
  1114. else
  1115. {
  1116. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1117. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1118. }
  1119. AddQueueItem(Queue, QueueItem, Terminal);
  1120. }
  1121. //---------------------------------------------------------------------------
  1122. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TManagedTerminal * Terminal)
  1123. {
  1124. if (Queue->IsEmpty)
  1125. {
  1126. Terminal->QueueOperationStart = Now();
  1127. }
  1128. Queue->AddItem(QueueItem);
  1129. }
  1130. //---------------------------------------------------------------------------
  1131. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1132. {
  1133. }
  1134. //---------------------------------------------------------------------------
  1135. void __fastcall TCustomScpExplorerForm::InitControls()
  1136. {
  1137. DebugAssert(FDocks != NULL);
  1138. for (int Index = 0; Index < ComponentCount; Index++)
  1139. {
  1140. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1141. if ((Dock != NULL) && (Dock->Tag == 0))
  1142. {
  1143. FDocks->Add(Dock);
  1144. }
  1145. }
  1146. CollectItemsWithTextDisplayMode(this);
  1147. }
  1148. //---------------------------------------------------------------------------
  1149. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1150. {
  1151. ConfigurationChanged();
  1152. QueuePanel->Height =
  1153. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1154. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1155. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1156. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1157. UpdateCustomCommandsToolbar();
  1158. }
  1159. //---------------------------------------------------------------------------
  1160. void __fastcall TCustomScpExplorerForm::StoreParams()
  1161. {
  1162. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1163. WinConfiguration->QueueView.HeightPixelsPerInch = GetControlPixelsPerInch(this);
  1164. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1165. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1166. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1167. }
  1168. //---------------------------------------------------------------------------
  1169. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1170. {
  1171. if (!FFormRestored)
  1172. {
  1173. FFormRestored = true;
  1174. RestoreFormParams();
  1175. }
  1176. TForm::CreateParams(Params);
  1177. }
  1178. //---------------------------------------------------------------------------
  1179. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1180. {
  1181. DebugAssert(FDocks != NULL);
  1182. for (int Index = 0; Index < FDocks->Count; Index++)
  1183. {
  1184. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1185. }
  1186. }
  1187. //---------------------------------------------------------------------------
  1188. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1189. {
  1190. SetDockAllowDrag(true);
  1191. while (!ButtonsStr.IsEmpty())
  1192. {
  1193. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1194. UnicodeString Name = CutToChar(S, L'=', true);
  1195. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1196. {
  1197. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1198. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1199. {
  1200. while (!S.IsEmpty())
  1201. {
  1202. UnicodeString S2 = CutToChar(S, L',', true);
  1203. UnicodeString Name = CutToChar(S2, L':', true);
  1204. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1205. {
  1206. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1207. if (SameText(GetToolbarItemName(Item), Name))
  1208. {
  1209. // forward compatibility
  1210. UnicodeString S3 = CutToChar(S2, L':', true);
  1211. bool Visible = StrToIntDef(S3, 0);
  1212. Item->Visible = Visible;
  1213. }
  1214. }
  1215. }
  1216. }
  1217. }
  1218. }
  1219. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1220. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1221. }
  1222. //---------------------------------------------------------------------------
  1223. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1224. {
  1225. UnicodeString Result;
  1226. SetDockAllowDrag(true);
  1227. Result = ::GetToolbarsLayoutStr(this);
  1228. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1229. return Result;
  1230. }
  1231. //---------------------------------------------------------------------------
  1232. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1233. {
  1234. UnicodeString Result;
  1235. if (Item->Action != NULL)
  1236. {
  1237. Result = Item->Action->Name;
  1238. Result = RemoveSuffix(Result, L"Action");
  1239. }
  1240. else
  1241. {
  1242. Result = Item->Name;
  1243. Result = RemoveSuffix(Result, L"SubmenuItem");
  1244. Result = RemoveSuffix(Result, L"Item");
  1245. }
  1246. return Result;
  1247. }
  1248. //---------------------------------------------------------------------------
  1249. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1250. {
  1251. UnicodeString Result;
  1252. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1253. {
  1254. UnicodeString ToolbarStr;
  1255. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1256. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1257. {
  1258. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1259. {
  1260. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1261. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1262. // Once we introduce any buttons that are hidden by default, we would have to remember their initial state
  1263. // and save the changes here only.
  1264. if (NonVisualDataModule->IsCustomizableToolbarItem(Item) && !Item->Visible)
  1265. {
  1266. UnicodeString Name = GetToolbarItemName(Item);
  1267. DebugAssert(Name.Pos(L"TBX") == 0);
  1268. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1269. }
  1270. }
  1271. }
  1272. if (!ToolbarStr.IsEmpty())
  1273. {
  1274. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1275. }
  1276. }
  1277. return Result;
  1278. }
  1279. //---------------------------------------------------------------------------
  1280. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1281. {
  1282. DebugAssert(FProgressForm == NULL);
  1283. bool AllowSkip = (Terminal != NULL) ? Terminal->IsCapable[fsSkipTransfer] : false;
  1284. FProgressForm = new TProgressForm(Application, (FTransferResumeList != NULL), AllowSkip, SynchronizeProgress);
  1285. FProgressForm->DeleteLocalToRecycleBin =
  1286. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1287. FProgressForm->DeleteRemoteToRecycleBin =
  1288. (Terminal != NULL) &&
  1289. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1290. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1291. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1292. LockWindow();
  1293. if (FTaskbarList != NULL)
  1294. {
  1295. // Actually, do not know what hides the progress once the operation finishes
  1296. SetTaskbarListProgressState(TBPF_NORMAL);
  1297. }
  1298. }
  1299. //---------------------------------------------------------------------------
  1300. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1301. {
  1302. UnlockWindow();
  1303. SAFE_DESTROY(FProgressForm);
  1304. SetQueueProgress();
  1305. }
  1306. //---------------------------------------------------------------------------
  1307. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1308. TFileOperationProgressType & ProgressData)
  1309. {
  1310. // Download to temporary local directory
  1311. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1312. {
  1313. bool Continue = true;
  1314. FPendingTempSpaceWarn = false;
  1315. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1316. if (!Continue)
  1317. {
  1318. Abort();
  1319. }
  1320. }
  1321. // operation is being executed and we still didn't show up progress form
  1322. if (ProgressData.InProgress && !FProgressForm)
  1323. {
  1324. CreateProgressForm(NULL);
  1325. DebugAssert(FProgressForm->OnceDoneOperation == odoIdle);
  1326. FProgressForm->OnceDoneOperation = ProgressData.InitialOnceDoneOperation;
  1327. }
  1328. // operation is finished (or terminated), so we hide progress form
  1329. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1330. {
  1331. DestroyProgressForm();
  1332. if (ProgressData.Operation == foCalculateSize)
  1333. {
  1334. // When calculating size before transfer, the abort caused by
  1335. // cancel flag set due to "MoveToQueue" below,
  1336. // is not propagated back to us in ExecuteFileOperation,
  1337. // as it is silently swallowed.
  1338. // So we explicitly (re)throw it here.
  1339. if (FMoveToQueue)
  1340. {
  1341. Abort();
  1342. }
  1343. }
  1344. else
  1345. {
  1346. if ((ProgressData.Cancel == csContinue) &&
  1347. !FAutoOperation)
  1348. {
  1349. OperationComplete(ProgressData.StartTime);
  1350. }
  1351. }
  1352. }
  1353. if (FProgressForm &&
  1354. // Can happen only during synchronization, when ending one of the operations
  1355. ProgressData.InProgress)
  1356. {
  1357. FProgressForm->SetProgressData(ProgressData);
  1358. if (FTaskbarList != NULL)
  1359. {
  1360. DebugAssert(ProgressData.InProgress);
  1361. SetTaskbarListProgressValue(&ProgressData);
  1362. }
  1363. if (FProgressForm->Cancel > csContinue)
  1364. {
  1365. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1366. if (FProgressForm->Cancel == csCancelFile)
  1367. {
  1368. FProgressForm->ClearCancel();
  1369. }
  1370. // cancel cancels even the move
  1371. FMoveToQueue = false;
  1372. }
  1373. else if (FProgressForm->MoveToQueue)
  1374. {
  1375. FMoveToQueue = true;
  1376. ProgressData.SetCancelAtLeast(csCancel);
  1377. }
  1378. if ((FTransferResumeList != NULL) &&
  1379. ProgressData.InProgress &&
  1380. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1381. !ProgressData.FullFileName.IsEmpty())
  1382. {
  1383. if ((FTransferResumeList->Count == 0) ||
  1384. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1385. {
  1386. // note that we do not recognize directories from files here
  1387. FTransferResumeList->Add(ProgressData.FullFileName);
  1388. }
  1389. }
  1390. }
  1391. }
  1392. //---------------------------------------------------------------------------
  1393. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1394. const TDateTime & StartTime)
  1395. {
  1396. if (GUIConfiguration->BeepOnFinish &&
  1397. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1398. {
  1399. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1400. DWORD Sound;
  1401. if (!ExtractFileExt(BeepSound).IsEmpty())
  1402. {
  1403. Sound = SND_FILENAME;
  1404. }
  1405. else
  1406. {
  1407. Sound = SND_ALIAS;
  1408. }
  1409. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1410. }
  1411. }
  1412. //---------------------------------------------------------------------------
  1413. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1414. TFileOperationProgressType & ProgressData)
  1415. {
  1416. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1417. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1418. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1419. FileOperationProgress(ProgressData);
  1420. }
  1421. //---------------------------------------------------------------------------
  1422. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1423. {
  1424. UnicodeString Result;
  1425. if (FProgressForm != NULL)
  1426. {
  1427. Result = FProgressForm->ProgressStr();
  1428. }
  1429. return Result;
  1430. }
  1431. //---------------------------------------------------------------------------
  1432. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1433. bool DragDrop)
  1434. {
  1435. return (!DragDrop && (DropSourceControl == NULL)) ||
  1436. (DropSourceControl == DirView(osOther));
  1437. }
  1438. //---------------------------------------------------------------------------
  1439. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1440. TFileOperation Operation, TOperationSide Side,
  1441. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1442. TOnceDoneOperation & OnceDoneOperation)
  1443. {
  1444. if (!FAutoOperation)
  1445. {
  1446. // no selection on "/upload", form servers only as event handler
  1447. // (it is not displayed)
  1448. if (PanelOperation(Side, FDragDropOperation) &&
  1449. Visible && (Operation != foCalculateSize) &&
  1450. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1451. {
  1452. if (FOnFileOperationFinished != NULL)
  1453. {
  1454. FOnFileOperationFinished(FileName, Success);
  1455. }
  1456. else
  1457. {
  1458. TCustomDirView * DView = DirView(Side);
  1459. UnicodeString FileNameOnly = ExtractFileName(FileName, !IsSideLocalBrowser(Side));
  1460. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1461. // this can happen when local drive is unplugged in the middle of the operation
  1462. if (Item != NULL)
  1463. {
  1464. if (Success) Item->Selected = false;
  1465. DView->MakeProgressVisible(Item);
  1466. }
  1467. }
  1468. }
  1469. if ((Operation == foCopy) || (Operation == foMove))
  1470. {
  1471. DebugAssert(!IsLocalBrowserMode()); // TODO
  1472. if (Side == osLocal)
  1473. {
  1474. Configuration->Usage->Inc(L"UploadedFiles");
  1475. }
  1476. else
  1477. {
  1478. Configuration->Usage->Inc(L"DownloadedFiles");
  1479. }
  1480. }
  1481. }
  1482. if (Success && (FSynchronizeController != NULL))
  1483. {
  1484. DebugAssert(!IsSideLocalBrowser(Side));
  1485. if (Operation == foCopy)
  1486. {
  1487. DebugAssert(Side == osLocal);
  1488. FSynchronizeController->LogOperation(soUpload, FileName);
  1489. }
  1490. else if (Operation == foDelete)
  1491. {
  1492. DebugAssert(Side == osRemote);
  1493. FSynchronizeController->LogOperation(soDelete, FileName);
  1494. }
  1495. }
  1496. if (FProgressForm)
  1497. {
  1498. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1499. }
  1500. }
  1501. //---------------------------------------------------------------------------
  1502. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1503. TFileOperation Operation, TOperationSide Side,
  1504. bool Temp, const UnicodeString & FileName, Boolean Success,
  1505. TOnceDoneOperation & OnceDoneOperation)
  1506. {
  1507. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1508. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1509. OnceDoneOperation);
  1510. }
  1511. //---------------------------------------------------------------------------
  1512. bool TCustomScpExplorerForm::IsLocalBrowserMode()
  1513. {
  1514. return false;
  1515. }
  1516. //---------------------------------------------------------------------------
  1517. bool TCustomScpExplorerForm::IsSideLocalBrowser(TOperationSide)
  1518. {
  1519. return false;
  1520. }
  1521. //---------------------------------------------------------------------------
  1522. TCustomDirView * TCustomScpExplorerForm::GetCurrentLocalBrowser()
  1523. {
  1524. return NULL;
  1525. }
  1526. //---------------------------------------------------------------------------
  1527. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1528. {
  1529. DebugAssert(GetSide(Side) == osRemote);
  1530. DebugUsedParam(Side);
  1531. return RemoteDirView;
  1532. }
  1533. //---------------------------------------------------------------------------
  1534. TCustomDriveView * __fastcall TCustomScpExplorerForm::DriveView(TOperationSide Side)
  1535. {
  1536. DebugAssert(GetSide(Side) == osRemote);
  1537. DebugUsedParam(Side);
  1538. return RemoteDriveView;
  1539. }
  1540. //---------------------------------------------------------------------------
  1541. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1542. {
  1543. DebugAssert(GetSide(Side) == osRemote);
  1544. DebugUsedParam(Side);
  1545. return (Terminal != NULL) && Terminal->Active;
  1546. }
  1547. //---------------------------------------------------------------------------
  1548. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1549. TOperationSide Side, int FilesOnly)
  1550. {
  1551. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1552. }
  1553. //---------------------------------------------------------------------------
  1554. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1555. TOperationSide Side, int FilesOnly)
  1556. {
  1557. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1558. }
  1559. //---------------------------------------------------------------------------
  1560. struct THistoryItemData
  1561. {
  1562. short int Side;
  1563. short int Index;
  1564. };
  1565. //---------------------------------------------------------------------------
  1566. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1567. {
  1568. DirView(Side)->HistoryGo(Index);
  1569. }
  1570. //---------------------------------------------------------------------------
  1571. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1572. {
  1573. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1574. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1575. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1576. }
  1577. //---------------------------------------------------------------------------
  1578. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1579. bool Back)
  1580. {
  1581. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1582. {
  1583. TCustomDirView * DView = DirView(Side);
  1584. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1585. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1586. if (ICount > 10)
  1587. {
  1588. ICount = 10;
  1589. }
  1590. Menu->Items->Clear();
  1591. THistoryItemData Data;
  1592. Data.Side = Side;
  1593. for (short int i = 1; i <= ICount; i++)
  1594. {
  1595. TTBCustomItem * Item = new TTBXItem(Menu);
  1596. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1597. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, !IsSideLocalBrowser(Side));
  1598. Item->Hint = DView->HistoryPath[Data.Index];
  1599. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1600. Item->Tag = *reinterpret_cast<int*>(&Data);
  1601. Item->OnClick = HistoryItemClick;
  1602. Menu->Items->Add(Item);
  1603. }
  1604. }
  1605. }
  1606. //---------------------------------------------------------------------------
  1607. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1608. TOperationSide Side, bool Back)
  1609. {
  1610. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1611. {
  1612. // workaround
  1613. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1614. // operator new allocates memory in Pascal code, but calls inline
  1615. // contructor in C++, leading in problems, the function does
  1616. // both in Pascal code
  1617. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1618. UpdateHistoryMenu(Side, Back);
  1619. }
  1620. return FHistoryMenu[Side == osLocal][Back];
  1621. }
  1622. //---------------------------------------------------------------------------
  1623. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1624. TCustomDirView *Sender)
  1625. {
  1626. TOperationSide Side = (Sender == DirView(osOther) ? osOther : osLocal);
  1627. UpdateHistoryMenu(Side, true);
  1628. UpdateHistoryMenu(Side, false);
  1629. }
  1630. //---------------------------------------------------------------------------
  1631. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1632. {
  1633. // remote custom commands can be executed only if the server supports shell commands
  1634. // or have secondary shell
  1635. return (FTerminal != NULL) && FTerminal->Active && (FTerminal->IsCapable[fcSecondaryShell] || FTerminal->IsCapable[fcShellAnyCommand]);
  1636. }
  1637. //---------------------------------------------------------------------------
  1638. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1639. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1640. {
  1641. int Result;
  1642. TFileCustomCommand RemoteCustomCommand;
  1643. TLocalCustomCommand LocalCustomCommand;
  1644. TFileCustomCommand * NonInteractiveCustomCommand =
  1645. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1646. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1647. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1648. if (FLAGCLEAR(Command.Params, ccLocal))
  1649. {
  1650. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1651. // custom command that does not operate with files can be executed anytime ...
  1652. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1653. {
  1654. // ... but do not show such command in remote file menu
  1655. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1656. {
  1657. Result = AllowedState;
  1658. }
  1659. else
  1660. {
  1661. Result = -1;
  1662. }
  1663. }
  1664. else
  1665. {
  1666. if ((ListType == ccltAll) || ((ListType == ccltFile) && !IsSideLocalBrowser(FCurrentSide)))
  1667. {
  1668. Result = (DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1669. }
  1670. else
  1671. {
  1672. Result = -1;
  1673. }
  1674. }
  1675. }
  1676. else
  1677. {
  1678. // custom command that does not operate with files can be executed anytime
  1679. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1680. {
  1681. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1682. }
  1683. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1684. {
  1685. if ((ListType == ccltAll) || (ListType == ccltFile))
  1686. {
  1687. // special case is "diff"-style command that can be executed over any side,
  1688. // if we have both sides
  1689. Result =
  1690. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1691. // directly (instead of EnableSelectedOperation) to pass
  1692. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1693. ((HasDirView[osLocal] && !IsSideLocalBrowser(osRemote) &&
  1694. DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1695. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1696. }
  1697. else if (ListType == ccltBoth)
  1698. {
  1699. DebugAssert(!IsLocalBrowserMode());
  1700. Result = 1;
  1701. }
  1702. else
  1703. {
  1704. Result = -1;
  1705. }
  1706. }
  1707. else
  1708. {
  1709. if ((ListType == ccltAll) ||
  1710. ((ListType == ccltFile) && (!IsSideLocalBrowser(FCurrentSide) || FLAGCLEAR(Command.Params, ccRemoteFiles))))
  1711. {
  1712. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1713. }
  1714. else
  1715. {
  1716. Result = -1;
  1717. }
  1718. }
  1719. }
  1720. return Result;
  1721. }
  1722. //---------------------------------------------------------------------------
  1723. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1724. TStrings * FileList, const TCustomCommandType & ACommand,
  1725. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1726. {
  1727. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1728. {
  1729. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1730. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1731. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1732. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1733. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1734. bool Capture =
  1735. FLAGSET(ACommand.Params, ccShowResults) ||
  1736. FLAGSET(ACommand.Params, ccCopyResults) ||
  1737. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1738. TCaptureOutputEvent OutputEvent = NULL;
  1739. DebugAssert(FCapturedLog == NULL);
  1740. if (Capture)
  1741. {
  1742. FCapturedLog = new TStringList();
  1743. OutputEvent = TerminalCaptureLog;
  1744. }
  1745. try
  1746. {
  1747. if (!RemoteCustomCommand.IsFileCommand(Command))
  1748. {
  1749. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1750. OutputEvent);
  1751. }
  1752. else
  1753. {
  1754. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1755. }
  1756. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1757. {
  1758. if (FLAGSET(ACommand.Params, ccCopyResults))
  1759. {
  1760. CopyToClipboard(FCapturedLog);
  1761. }
  1762. if (FLAGSET(ACommand.Params, ccShowResults))
  1763. {
  1764. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1765. }
  1766. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1767. {
  1768. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1769. }
  1770. }
  1771. }
  1772. __finally
  1773. {
  1774. SAFE_DESTROY(FCapturedLog);
  1775. }
  1776. }
  1777. }
  1778. //---------------------------------------------------------------------------
  1779. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1780. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1781. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1782. {
  1783. DebugAssert(!IsLocalBrowserMode());
  1784. TStrings * LocalFileList = NULL;
  1785. TStrings * RemoteFileList = NULL;
  1786. TStrings * RemoteFileListFull = NULL;
  1787. try
  1788. {
  1789. if (LocalFileCommand)
  1790. {
  1791. if (ALocalFileList == NULL)
  1792. {
  1793. DebugAssert(HasDirView[osLocal]);
  1794. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1795. // directly (instead of EnableSelectedOperation) to pass
  1796. // false to FocusedFileOnlyWhenFocused
  1797. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1798. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1799. }
  1800. else
  1801. {
  1802. LocalFileList = ALocalFileList;
  1803. }
  1804. if (FileListCommand)
  1805. {
  1806. if (LocalFileList->Count != 1)
  1807. {
  1808. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1809. }
  1810. }
  1811. else
  1812. {
  1813. if ((LocalFileList->Count != 1) &&
  1814. (FileList->Count != 1) &&
  1815. (LocalFileList->Count != FileList->Count))
  1816. {
  1817. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1818. }
  1819. }
  1820. }
  1821. UnicodeString RootTempDir;
  1822. UnicodeString TempDir;
  1823. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1824. if (!RemoteFiles)
  1825. {
  1826. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1827. }
  1828. try
  1829. {
  1830. TDateTimes RemoteFileTimes;
  1831. if (RemoteFiles)
  1832. {
  1833. RemoteFileList = FileList;
  1834. }
  1835. else
  1836. {
  1837. RemoteFileList = new TStringList();
  1838. TMakeLocalFileListParams MakeFileListParam;
  1839. MakeFileListParam.FileList = RemoteFileList;
  1840. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1841. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1842. MakeFileListParam.Recursive =
  1843. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1844. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1845. if (!MakeFileListParam.Recursive)
  1846. {
  1847. RemoteFileTimes.clear();
  1848. RemoteFileListFull = new TStringList();
  1849. MakeFileListParam.FileList = RemoteFileListFull;
  1850. MakeFileListParam.Recursive = true;
  1851. MakeFileListParam.IncludeDirs = false;
  1852. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1853. }
  1854. else
  1855. {
  1856. RemoteFileListFull = RemoteFileList;
  1857. }
  1858. }
  1859. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1860. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1861. if (!NonBlocking)
  1862. {
  1863. int Count;
  1864. if (FileListCommand)
  1865. {
  1866. Count = 1;
  1867. }
  1868. else if (LocalFileCommand)
  1869. {
  1870. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  1871. }
  1872. else
  1873. {
  1874. Count = RemoteFileList->Count;
  1875. }
  1876. Progress.Start(foCustomCommand, osRemote, Count);
  1877. DebugAssert(FProgressForm != NULL);
  1878. FProgressForm->ReadOnly = true;
  1879. }
  1880. try
  1881. {
  1882. if (FileListCommand)
  1883. {
  1884. UnicodeString LocalFile;
  1885. // MakeFileList does not delimit filenames
  1886. UnicodeString FileList = MakeFileList(RemoteFileList);
  1887. if (LocalFileCommand)
  1888. {
  1889. DebugAssert(LocalFileList->Count == 1);
  1890. LocalFile = LocalFileList->Strings[0];
  1891. }
  1892. TLocalCustomCommand CustomCommand(Data,
  1893. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1894. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1895. if (NonBlocking)
  1896. {
  1897. DebugAssert(!POutput);
  1898. ExecuteShellChecked(ShellCommand);
  1899. }
  1900. else
  1901. {
  1902. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1903. }
  1904. }
  1905. else if (LocalFileCommand)
  1906. {
  1907. if (LocalFileList->Count == 1)
  1908. {
  1909. UnicodeString LocalFile = LocalFileList->Strings[0];
  1910. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1911. {
  1912. UnicodeString FileName = RemoteFileList->Strings[Index];
  1913. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1914. Progress.SetFile(FileNameForProgress);
  1915. TLocalCustomCommand CustomCommand(Data,
  1916. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  1917. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1918. TOnceDoneOperation OnceDoneOperation;
  1919. // Do not unselect anything, as it won't work with two panels anyway
  1920. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1921. }
  1922. }
  1923. else if (RemoteFileList->Count == 1)
  1924. {
  1925. UnicodeString FileName = RemoteFileList->Strings[0];
  1926. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1927. {
  1928. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1929. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1930. Progress.SetFile(LocalFileNameOnly);
  1931. TLocalCustomCommand CustomCommand(
  1932. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1933. FileName, LocalFileName, L"");
  1934. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1935. TOnceDoneOperation OnceDoneOperation;
  1936. // Do not unselect anything, as it won't work with two panels anyway
  1937. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1938. }
  1939. }
  1940. else
  1941. {
  1942. if (LocalFileList->Count != RemoteFileList->Count)
  1943. {
  1944. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  1945. }
  1946. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1947. {
  1948. UnicodeString FileName = RemoteFileList->Strings[Index];
  1949. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1950. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1951. Progress.SetFile(LocalFileNameOnly); // sic
  1952. TLocalCustomCommand CustomCommand(
  1953. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1954. FileName, LocalFileName, L"");
  1955. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1956. TOnceDoneOperation OnceDoneOperation;
  1957. // Do not unselect anything, as it won't work with two panels anyway
  1958. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1959. }
  1960. }
  1961. }
  1962. else
  1963. {
  1964. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1965. {
  1966. UnicodeString FileName = RemoteFileList->Strings[Index];
  1967. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1968. Progress.SetFile(FileNameForProgress);
  1969. TLocalCustomCommand CustomCommand(Data,
  1970. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1971. FileName, L"", L"");
  1972. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1973. TOnceDoneOperation OnceDoneOperation;
  1974. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  1975. }
  1976. }
  1977. }
  1978. __finally
  1979. {
  1980. if (!NonBlocking)
  1981. {
  1982. Progress.Stop();
  1983. }
  1984. }
  1985. DebugAssert(!FAutoOperation);
  1986. if (!RemoteFiles)
  1987. {
  1988. TempDir = IncludeTrailingBackslash(TempDir);
  1989. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  1990. {
  1991. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  1992. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  1993. // Skip directories, process only nested files.
  1994. // The check is redundant as FileAge fails for directories anyway.
  1995. !DirectoryExists(FileName))
  1996. {
  1997. UnicodeString RemoteDir =
  1998. UnixExtractFileDir(
  1999. UnixIncludeTrailingBackslash(FTerminal->CurrentDirectory) +
  2000. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  2001. TDateTime NewTime;
  2002. if (FileAge(FileName, NewTime) &&
  2003. (NewTime != RemoteFileTimes[Index]))
  2004. {
  2005. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2006. TemporaryFileCopyParam(CopyParam);
  2007. CopyParam.FileMask = L"";
  2008. FAutoOperation = true;
  2009. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  2010. TemporaryFilesList->Add(FileName);
  2011. FTerminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  2012. }
  2013. }
  2014. }
  2015. }
  2016. }
  2017. __finally
  2018. {
  2019. FAutoOperation = false;
  2020. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  2021. {
  2022. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2023. }
  2024. }
  2025. }
  2026. __finally
  2027. {
  2028. if (RemoteFileList != FileList)
  2029. {
  2030. delete RemoteFileList;
  2031. }
  2032. if (LocalFileList != ALocalFileList)
  2033. {
  2034. delete LocalFileList;
  2035. }
  2036. if (RemoteFileListFull != RemoteFileList)
  2037. {
  2038. delete RemoteFileListFull;
  2039. }
  2040. }
  2041. }
  2042. //---------------------------------------------------------------------------
  2043. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2044. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2045. bool FileListCommand, UnicodeString * POutput)
  2046. {
  2047. std::unique_ptr<TStrings> SelectedFileList(DirView(osLocal)->CreateFileList(false, true, NULL));
  2048. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2049. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2050. {
  2051. UnicodeString FileName = SelectedFileList->Strings[Index];
  2052. if (DirectoryExists(FileName))
  2053. {
  2054. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2055. {
  2056. LocalFileList->Add(FileName);
  2057. }
  2058. if (FLAGSET(ACommand.Params, ccRecursive))
  2059. {
  2060. TMakeLocalFileListParams MakeFileListParam;
  2061. MakeFileListParam.FileList = LocalFileList.get();
  2062. MakeFileListParam.FileTimes = NULL;
  2063. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2064. MakeFileListParam.Recursive = true;
  2065. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2066. }
  2067. }
  2068. else
  2069. {
  2070. LocalFileList->Add(FileName);
  2071. }
  2072. }
  2073. UnicodeString RemotePath;
  2074. if (Terminal != NULL)
  2075. {
  2076. RemotePath = Terminal->CurrentDirectory;
  2077. }
  2078. if (FileListCommand)
  2079. {
  2080. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2081. TLocalCustomCommand CustomCommand(
  2082. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2083. L"", L"", FileList);
  2084. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2085. }
  2086. else
  2087. {
  2088. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2089. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2090. DebugAssert(FProgressForm != NULL);
  2091. FProgressForm->ReadOnly = true;
  2092. try
  2093. {
  2094. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2095. {
  2096. UnicodeString FileName = LocalFileList->Strings[Index];
  2097. Progress.SetFile(FileName);
  2098. TLocalCustomCommand CustomCommand(
  2099. Data, RemotePath, DefaultDownloadTargetDirectory(),
  2100. FileName, L"", L"");
  2101. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2102. TOnceDoneOperation OnceDoneOperation;
  2103. Progress.Finish(FileName, true, OnceDoneOperation);
  2104. }
  2105. }
  2106. __finally
  2107. {
  2108. Progress.Stop();
  2109. }
  2110. }
  2111. }
  2112. //---------------------------------------------------------------------------
  2113. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2114. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2115. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2116. {
  2117. UnicodeString RemotePath = (Terminal != NULL) ? Terminal->CurrentDirectory : UnicodeString();
  2118. TLocalCustomCommand LocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory());
  2119. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2120. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2121. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2122. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2123. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2124. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2125. std::unique_ptr<UnicodeString> POutput;
  2126. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2127. FLAGSET(ACommand.Params, ccCopyResults))
  2128. {
  2129. POutput.reset(new UnicodeString());
  2130. }
  2131. if (!LocalCustomCommand.IsFileCommand(Command))
  2132. {
  2133. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2134. }
  2135. // remote files?
  2136. else if ((FCurrentSide == osRemote) || LocalFileCommand)
  2137. {
  2138. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2139. }
  2140. // local files
  2141. else
  2142. {
  2143. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2144. }
  2145. if (POutput.get() != NULL)
  2146. {
  2147. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2148. int P = POutput->Pos(sLineBreak);
  2149. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2150. {
  2151. POutput->SetLength(P - 1);
  2152. }
  2153. // Copy even empty output to clipboard
  2154. if (FLAGSET(ACommand.Params, ccCopyResults))
  2155. {
  2156. CopyToClipboard(*POutput);
  2157. }
  2158. // But do not show an empty message box.
  2159. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2160. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2161. !POutput->IsEmpty())
  2162. {
  2163. TClipboardHandler ClipboardHandler;
  2164. ClipboardHandler.Text = *POutput;
  2165. TMessageParams Params;
  2166. TQueryButtonAlias Aliases[1];
  2167. Aliases[0].Button = qaRetry;
  2168. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2169. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2170. Params.Aliases = Aliases;
  2171. Params.AliasesCount = LENOF(Aliases);
  2172. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2173. }
  2174. }
  2175. }
  2176. //---------------------------------------------------------------------------
  2177. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2178. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2179. {
  2180. TCustomCommandData Data;
  2181. UnicodeString Site;
  2182. UnicodeString RemotePath;
  2183. if (Terminal != NULL)
  2184. {
  2185. Data = TCustomCommandData(Terminal);
  2186. Site = Terminal->SessionData->SessionKey;
  2187. RemotePath = Terminal->CurrentDirectory;
  2188. }
  2189. UnicodeString HelpKeyword = ACommand.HomePage;
  2190. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2191. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2192. {
  2193. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2194. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2195. {
  2196. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, RemotePath));
  2197. }
  2198. else
  2199. {
  2200. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, RemotePath, DefaultDownloadTargetDirectory()));
  2201. }
  2202. if (!DoCustomCommandOptionsDialog(
  2203. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2204. {
  2205. Abort();
  2206. }
  2207. }
  2208. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2209. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2210. {
  2211. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2212. }
  2213. else
  2214. {
  2215. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2216. }
  2217. }
  2218. //---------------------------------------------------------------------------
  2219. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2220. const TCustomCommandType & Command)
  2221. {
  2222. DebugAssert(FCustomCommandLocalFileList != NULL);
  2223. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2224. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2225. TStrings * LocalFileList = new TStringList();
  2226. TStrings * RemoteFileList = new TStringList();
  2227. try
  2228. {
  2229. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2230. {
  2231. LocalFileList->Clear();
  2232. LocalFileList->AddObject(
  2233. FCustomCommandLocalFileList->Strings[Index],
  2234. FCustomCommandLocalFileList->Objects[Index]);
  2235. RemoteFileList->Clear();
  2236. RemoteFileList->AddObject(
  2237. FCustomCommandRemoteFileList->Strings[Index],
  2238. FCustomCommandRemoteFileList->Objects[Index]);
  2239. CustomCommand(RemoteFileList, Command, LocalFileList);
  2240. }
  2241. }
  2242. __finally
  2243. {
  2244. delete LocalFileList;
  2245. delete RemoteFileList;
  2246. }
  2247. }
  2248. //---------------------------------------------------------------------------
  2249. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2250. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2251. {
  2252. delete FCustomCommandLocalFileList;
  2253. delete FCustomCommandRemoteFileList;
  2254. // takeover ownership,
  2255. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2256. FCustomCommandLocalFileList = LocalFileList;
  2257. FCustomCommandRemoteFileList = RemoteFileList;
  2258. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2259. if (Button != NULL)
  2260. {
  2261. FCustomCommandMenu->Items->Clear();
  2262. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth, NULL);
  2263. MenuPopup(FCustomCommandMenu, Button);
  2264. }
  2265. else
  2266. {
  2267. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2268. }
  2269. }
  2270. //---------------------------------------------------------------------------
  2271. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2272. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2273. {
  2274. DebugAssert(FCapturedLog != NULL);
  2275. if ((OutputType == cotOutput) || (OutputType == cotError))
  2276. {
  2277. FCapturedLog->Add(AddedLine);
  2278. }
  2279. }
  2280. //---------------------------------------------------------------------------
  2281. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2282. TOperationSide Side)
  2283. {
  2284. return
  2285. (Side == osOther) &&
  2286. ((Control == DirView(osOther)) || (Control == DriveView(osOther)));
  2287. }
  2288. //---------------------------------------------------------------------------
  2289. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2290. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2291. {
  2292. TTBItemOptions O;
  2293. O = Item->Options;
  2294. TCustomDirView * DView = DirView(Side);
  2295. if ((DView->ItemFocused != NULL) &&
  2296. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2297. // when resolving links is disabled, default action is to enter the directory,
  2298. // no matter what DoubleClickAction is configured to
  2299. (IsSideLocalBrowser(Side) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2300. // Can only Edit files, but can Open/Copy even directories
  2301. ((DoubleClickAction != dcaEdit) ||
  2302. !DView->ItemIsDirectory(DView->ItemFocused)))
  2303. {
  2304. Item->Options = O << tboDefault;
  2305. }
  2306. else
  2307. {
  2308. Item->Options = O >> tboDefault;
  2309. }
  2310. }
  2311. //---------------------------------------------------------------------------
  2312. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2313. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2314. {
  2315. TCustomDirView * DView = DirView(Side);
  2316. TListItem * Item = DView->ItemFocused;
  2317. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2318. EnableFocusedOperation[Side])
  2319. {
  2320. TPoint ClientPoint;
  2321. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2322. TPoint(0, 0) : MousePos;
  2323. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2324. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2325. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2326. }
  2327. }
  2328. //---------------------------------------------------------------------------
  2329. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2330. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2331. {
  2332. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2333. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2334. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2335. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2336. Handled = true;
  2337. }
  2338. //---------------------------------------------------------------------------
  2339. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2340. {
  2341. }
  2342. //---------------------------------------------------------------------------
  2343. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2344. {
  2345. DebugAssert(FErrorList == NULL);
  2346. if (WinConfiguration->ContinueOnError)
  2347. {
  2348. FErrorList = new TStringList();
  2349. Configuration->Usage->Inc(L"ContinuationsOnError");
  2350. }
  2351. NonVisualDataModule->StartBusy();
  2352. }
  2353. //---------------------------------------------------------------------------
  2354. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2355. {
  2356. NonVisualDataModule->EndBusy();
  2357. if (FErrorList)
  2358. {
  2359. HandleErrorList(FErrorList);
  2360. }
  2361. }
  2362. //---------------------------------------------------------------------------
  2363. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2364. const TCopyParamType & CopyParam)
  2365. {
  2366. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2367. {
  2368. Configuration->Usage->Inc(L"FileMaskUses");
  2369. }
  2370. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2371. {
  2372. Configuration->Usage->Inc(L"OperationMaskUses");
  2373. }
  2374. }
  2375. //---------------------------------------------------------------------------
  2376. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2377. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2378. {
  2379. DebugAssert(!IsLocalBrowserMode());
  2380. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2381. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2382. TTransferOperationParam DefaultParam;
  2383. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2384. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2385. switch (Param.Queue)
  2386. {
  2387. case asOn:
  2388. CopyParam.Queue = true;
  2389. break;
  2390. case asOff:
  2391. CopyParam.Queue = false;
  2392. break;
  2393. case asAuto:
  2394. default:
  2395. // keep default
  2396. break;
  2397. }
  2398. bool Result =
  2399. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2400. if (Result)
  2401. {
  2402. DebugAssert(Terminal);
  2403. bool SelectionRestored = false;
  2404. TCustomDirView * DView = NULL;
  2405. if (HasDirView[Side])
  2406. {
  2407. DView = DirView(Side);
  2408. DView->SaveSelection();
  2409. DView->SaveSelectedNames();
  2410. }
  2411. UpdateCopyParamCounters(CopyParam);
  2412. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2413. DebugAssert(FTransferResumeList == NULL);
  2414. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2415. FMoveToQueue = false;
  2416. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2417. try
  2418. {
  2419. TStrings * PermanentFileList;
  2420. std::unique_ptr<TStrings> PermanentFileListOwner;
  2421. try
  2422. {
  2423. if (Side == osLocal)
  2424. {
  2425. PermanentFileList = FileList;
  2426. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2427. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2428. if (Operation == foMove)
  2429. {
  2430. ReloadLocalDirectory();
  2431. if (DView != NULL)
  2432. {
  2433. DView->RestoreSelection();
  2434. }
  2435. SelectionRestored = true;
  2436. }
  2437. }
  2438. else
  2439. {
  2440. // Clone the file list as it may refer to current directory files,
  2441. // which get destroyed, when the source directory is reloaded after foMove operation.
  2442. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2443. // But for a hotfix, we are not going to do such a big change.
  2444. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2445. PermanentFileList = PermanentFileListOwner.get();
  2446. try
  2447. {
  2448. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2449. }
  2450. __finally
  2451. {
  2452. if (Operation == foMove)
  2453. {
  2454. if (DView != NULL)
  2455. {
  2456. DView->RestoreSelection();
  2457. }
  2458. SelectionRestored = true;
  2459. }
  2460. ReloadLocalDirectory(Param.TargetDirectory);
  2461. }
  2462. }
  2463. }
  2464. catch (EAbort &)
  2465. {
  2466. if (FMoveToQueue)
  2467. {
  2468. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2469. DebugAssert(CopyParam.TransferSkipList == NULL);
  2470. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2471. if (TransferResumeList->Count > 0)
  2472. {
  2473. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2474. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2475. }
  2476. CopyParam.TransferSkipList = TransferResumeList.release();
  2477. // not really needed, just to keep it consistent with TransferResumeList
  2478. FTransferResumeList = NULL;
  2479. FMoveToQueue = false;
  2480. Configuration->Usage->Inc("MovesToBackground");
  2481. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2482. ClearTransferSourceSelection(Direction);
  2483. }
  2484. throw;
  2485. }
  2486. }
  2487. __finally
  2488. {
  2489. if (!SelectionRestored && (DView != NULL))
  2490. {
  2491. DView->DiscardSavedSelection();
  2492. }
  2493. FTransferResumeList = NULL;
  2494. }
  2495. }
  2496. return Result;
  2497. }
  2498. //---------------------------------------------------------------------------
  2499. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2500. TOperationSide Side, TStrings * FileList, void * Param)
  2501. {
  2502. DebugAssert(FileList->Count);
  2503. // We deliberately do not toggle alternative flag (Param), but use OR,
  2504. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2505. // shortcut of CurrentDeleteAlternativeAction
  2506. bool Alternative =
  2507. bool(Param) || UseAlternativeFunction();
  2508. bool Recycle;
  2509. if (IsSideLocalBrowser(Side))
  2510. {
  2511. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2512. }
  2513. else
  2514. {
  2515. Recycle =
  2516. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2517. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2518. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2519. }
  2520. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2521. if (!Result)
  2522. {
  2523. UnicodeString Query;
  2524. if (FileList->Count == 1)
  2525. {
  2526. if (IsSideLocalBrowser(Side))
  2527. {
  2528. Query = ExtractFileName(FileList->Strings[0]);
  2529. }
  2530. else
  2531. {
  2532. Query = UnixExtractFileName(FileList->Strings[0]);
  2533. }
  2534. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2535. }
  2536. else
  2537. {
  2538. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2539. }
  2540. TMessageParams Params(mpNeverAskAgainCheck);
  2541. Params.ImageName = L"Delete file";
  2542. unsigned int Answer =
  2543. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2544. if (Answer == qaNeverAskAgain)
  2545. {
  2546. Result = true;
  2547. if (Recycle)
  2548. {
  2549. WinConfiguration->ConfirmRecycling = false;
  2550. }
  2551. else
  2552. {
  2553. WinConfiguration->ConfirmDeleting = false;
  2554. }
  2555. }
  2556. else
  2557. {
  2558. Result = (Answer == qaOK);
  2559. }
  2560. }
  2561. if (Result)
  2562. {
  2563. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2564. }
  2565. return Result;
  2566. }
  2567. //---------------------------------------------------------------------------
  2568. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2569. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2570. {
  2571. TAutoBatch AutoBatch(this);
  2572. bool Result;
  2573. if ((Operation == foCopy) || (Operation == foMove))
  2574. {
  2575. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2576. }
  2577. else if (Operation == foRename)
  2578. {
  2579. DebugAssert(DirView(Side)->ItemFocused);
  2580. DirView(Side)->ItemFocused->EditCaption();
  2581. Result = true;
  2582. }
  2583. else if (Operation == foDelete)
  2584. {
  2585. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2586. }
  2587. else if (Operation == foSetProperties)
  2588. {
  2589. DirView(osRemote)->SaveSelectedNames();
  2590. Result = SetProperties(Side, FileList);
  2591. }
  2592. else if (Operation == foCustomCommand)
  2593. {
  2594. DebugAssert(Param);
  2595. DebugAssert(Side == osRemote); // Side carries no meaning for foCustomCommand
  2596. DirView(osRemote)->SaveSelectedNames();
  2597. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2598. CustomCommand(FileList, *Command, NULL);
  2599. Result = true;
  2600. }
  2601. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2602. {
  2603. DebugAssert(!IsSideLocalBrowser(Side));
  2604. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2605. (Operation == foRemoteMove), reinterpret_cast<TManagedTerminal *>(Param));
  2606. }
  2607. else if (Operation == foLock)
  2608. {
  2609. DebugAssert(!IsSideLocalBrowser(Side));
  2610. LockFiles(FileList, true);
  2611. Result = true;
  2612. }
  2613. else if (Operation == foUnlock)
  2614. {
  2615. DebugAssert(!IsSideLocalBrowser(Side));
  2616. LockFiles(FileList, false);
  2617. Result = true;
  2618. }
  2619. else
  2620. {
  2621. DebugFail();
  2622. }
  2623. return Result;
  2624. }
  2625. //---------------------------------------------------------------------------
  2626. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2627. {
  2628. if (Side == osCurrent)
  2629. {
  2630. Side = FCurrentSide;
  2631. }
  2632. return Side;
  2633. }
  2634. //---------------------------------------------------------------------------
  2635. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2636. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2637. {
  2638. Side = GetSide(Side);
  2639. bool Result;
  2640. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, IsSideLocalBrowser(Side), NULL);
  2641. try
  2642. {
  2643. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2644. }
  2645. __finally
  2646. {
  2647. delete FileList;
  2648. }
  2649. return Result;
  2650. }
  2651. //---------------------------------------------------------------------------
  2652. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2653. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2654. bool NoConfirmation, void * Param)
  2655. {
  2656. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2657. {
  2658. if ((Operation == foCopy) || (Operation == foMove))
  2659. {
  2660. DebugAssert(!IsLocalBrowserMode());
  2661. if (GetSide(Side) == osLocal)
  2662. {
  2663. Configuration->Usage->Inc(L"UploadsCommand");
  2664. }
  2665. else
  2666. {
  2667. Configuration->Usage->Inc(L"DownloadsCommand");
  2668. }
  2669. }
  2670. }
  2671. }
  2672. //---------------------------------------------------------------------------
  2673. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2674. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2675. {
  2676. TTransferOperationParam Param;
  2677. if ((WinConfiguration->Interface != ifCommander) ||
  2678. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2679. {
  2680. Flags &= ~cocShortCutHint;
  2681. }
  2682. TCustomDirView * DView = DirView(Side);
  2683. Param.Options =
  2684. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2685. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2686. if (FLAGSET(Flags, cocQueue))
  2687. {
  2688. Param.Queue = asOn;
  2689. }
  2690. else if (FLAGSET(Flags, cocNonQueue))
  2691. {
  2692. Param.Queue = asOff;
  2693. }
  2694. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2695. }
  2696. //---------------------------------------------------------------------------
  2697. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2698. {
  2699. try
  2700. {
  2701. if (ErrorList->Count)
  2702. {
  2703. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2704. if (MessageDialog(Message, qtError,
  2705. qaOK | qaCancel, HELP_NONE) == qaOK)
  2706. {
  2707. unsigned int Answer;
  2708. int Index = 0;
  2709. do
  2710. {
  2711. DebugAssert(Index >= 0 && Index < ErrorList->Count);
  2712. TQueryButtonAlias Aliases[2];
  2713. Aliases[0].Button = qaYes;
  2714. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2715. Aliases[1].Button = qaNo;
  2716. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2717. TMessageParams Params;
  2718. Params.Aliases = Aliases;
  2719. Params.AliasesCount = LENOF(Aliases);
  2720. Answer = MoreMessageDialog(
  2721. FMTLOAD(ERROR_LIST_NUMBER, (Index+1, ErrorList->Count, ErrorList->Strings[Index])),
  2722. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2723. (Index ? qaYes : 0) | (Index < ErrorList->Count - 1 ? qaNo : 0) |
  2724. qaOK, HELP_NONE, &Params);
  2725. if (Answer == qaNo)
  2726. {
  2727. Index++;
  2728. }
  2729. if (Answer == qaYes)
  2730. {
  2731. Index--;
  2732. }
  2733. }
  2734. while (Answer != qaOK);
  2735. }
  2736. }
  2737. }
  2738. __finally
  2739. {
  2740. TStrings * List = ErrorList;
  2741. ErrorList = NULL;
  2742. for (int i = 0; i < List->Count; i++)
  2743. {
  2744. delete List->Objects[i];
  2745. }
  2746. delete List;
  2747. }
  2748. }
  2749. //---------------------------------------------------------------------------
  2750. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2751. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2752. {
  2753. // needed for checking filemasks, as there's no directory object
  2754. // associated with the file object
  2755. File->FullFileName = FullFileName;
  2756. TFileMasks::TParams MaskParams;
  2757. MaskParams.Size = File->Size;
  2758. MaskParams.Modification = File->Modification;
  2759. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2760. }
  2761. //---------------------------------------------------------------------------
  2762. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2763. {
  2764. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2765. Side = GetSide(Side);
  2766. TCustomDirView * DView = DirView(Side);
  2767. TListItem * FocusedItem = DView->ItemFocused;
  2768. UnicodeString Name;
  2769. if ((FocusedItem != NULL) && !DView->ItemIsDirectory(FocusedItem))
  2770. {
  2771. Name = DView->ItemFileName(FocusedItem);
  2772. }
  2773. else
  2774. {
  2775. Name = LoadStr(NEW_FILE);
  2776. }
  2777. UnicodeString Names = Name;
  2778. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2779. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2780. HELP_EDIT_NEW, History.get(), true))
  2781. {
  2782. while (!Names.IsEmpty())
  2783. {
  2784. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2785. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2786. UnicodeString TargetFileName;
  2787. UnicodeString LocalFileName;
  2788. UnicodeString RootTempDir;
  2789. UnicodeString TempDir;
  2790. UnicodeString RemoteDirectory;
  2791. bool ExistingFile = false;
  2792. if (!IsSideLocalBrowser(Side))
  2793. {
  2794. Name = AbsolutePath(FTerminal->CurrentDirectory, Name);
  2795. TRemoteFile * File = NULL;
  2796. if (FTerminal->FileExists(Name, &File) &&
  2797. !File->IsDirectory)
  2798. {
  2799. try
  2800. {
  2801. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2802. ExistingFile = true;
  2803. }
  2804. __finally
  2805. {
  2806. delete File;
  2807. }
  2808. }
  2809. if (!ExistingFile)
  2810. {
  2811. RemoteDirectory = UnixExtractFilePath(Name);
  2812. TemporaryDirectoryForRemoteFiles(
  2813. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2814. TargetFileName = UnixExtractFileName(Name);
  2815. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2816. LocalFileName = TempDir +
  2817. // We probably do not want to trim the VMS version here
  2818. FTerminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2819. }
  2820. }
  2821. else
  2822. {
  2823. if (ExtractFilePath(Name).IsEmpty())
  2824. {
  2825. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2826. }
  2827. else
  2828. {
  2829. LocalFileName = ExpandFileName(Name);
  2830. }
  2831. TargetFileName = ExtractFileName(Name);
  2832. }
  2833. if (!ExistingFile)
  2834. {
  2835. bool NewFile = !FileExists(ApiPath(LocalFileName));
  2836. if (NewFile)
  2837. {
  2838. int File = FileCreate(ApiPath(LocalFileName));
  2839. if (File < 0)
  2840. {
  2841. if (!RootTempDir.IsEmpty())
  2842. {
  2843. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2844. }
  2845. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2846. }
  2847. else
  2848. {
  2849. FileClose(File);
  2850. }
  2851. }
  2852. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2853. const TEditorData * ExternalEditor = NULL;
  2854. TFileMasks::TParams MaskParams; // size not known
  2855. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2856. false, MaskParams);
  2857. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2858. ExternalEditor, RootTempDir, RemoteDirectory, NewFile);
  2859. }
  2860. }
  2861. }
  2862. }
  2863. //---------------------------------------------------------------------------
  2864. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2865. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2866. {
  2867. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2868. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2869. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2870. return
  2871. ((ExecuteFileBy == efInternalEditor)) ||
  2872. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2873. }
  2874. //---------------------------------------------------------------------------
  2875. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2876. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2877. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2878. UnicodeString RemoteDirectory, bool NewFile)
  2879. {
  2880. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2881. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2882. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2883. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2884. Side = GetSide(Side);
  2885. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2886. if (!IsSideLocalBrowser(Side))
  2887. {
  2888. Data->Terminal = Terminal;
  2889. Data->Queue = Queue;
  2890. Data->SessionData = CloneCurrentSessionData();
  2891. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2892. Data->RemoteDirectory = RemoteDirectory;
  2893. Data->SessionName = Terminal->SessionData->SessionName;
  2894. Data->LocalRootDirectory = LocalRootDirectory;
  2895. Data->OriginalFileName = OriginalFileName;
  2896. Data->Command = L""; // will be changed later for external editor
  2897. }
  2898. if (ExecuteFileBy == efInternalEditor)
  2899. {
  2900. if (!IsSideLocalBrowser(Side))
  2901. {
  2902. UnicodeString Caption = UnixIncludeTrailingBackslash(RemoteDirectory) + OriginalFileName +
  2903. L" - " + Terminal->SessionData->SessionName;
  2904. TForm * Editor;
  2905. try
  2906. {
  2907. TNotifyEvent OnFileChanged = NULL;
  2908. TNotifyEvent OnSaveAll = NULL;
  2909. TAnyModifiedEvent OnAnyModified = NULL;
  2910. // Edited files are uploaded in background queue, what we do not support with encrypted files
  2911. if (Terminal->IsCapable[fsBackgroundTransfers])
  2912. {
  2913. OnFileChanged = FEditorManager->FileChanged;
  2914. OnSaveAll = SaveAllInternalEditors;
  2915. OnAnyModified = AnyInternalEditorModified;
  2916. }
  2917. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  2918. FEditorManager->FileReload, FEditorManager->FileClosed,
  2919. OnSaveAll, OnAnyModified,
  2920. Caption, FStandaloneEditing, SessionColor, Terminal->SessionData->InternalEditorEncoding, NewFile);
  2921. }
  2922. catch(...)
  2923. {
  2924. if (!LocalRootDirectory.IsEmpty())
  2925. {
  2926. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  2927. }
  2928. throw;
  2929. }
  2930. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  2931. }
  2932. else
  2933. {
  2934. DebugAssert(!FStandaloneEditing);
  2935. TForm * Editor =
  2936. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  2937. SaveAllInternalEditors, AnyInternalEditorModified,
  2938. L"", false, SessionColor, -1, NewFile);
  2939. FLocalEditors->Add(Editor);
  2940. }
  2941. }
  2942. else
  2943. {
  2944. HANDLE Process;
  2945. if (ExecuteFileBy == efExternalEditor)
  2946. {
  2947. UnicodeString Program, Params, Dir;
  2948. Data->Command = ExternalEditor->ExternalEditor;
  2949. ReformatFileNameCommand(Data->Command);
  2950. SplitCommand(Data->Command, Program, Params, Dir);
  2951. Params = ExpandFileNameCommand(Params, FileName);
  2952. Program = ExpandEnvironmentVariables(Program);
  2953. if (!ExecuteShell(Program, Params, Process))
  2954. {
  2955. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  2956. }
  2957. }
  2958. else
  2959. {
  2960. DebugAssert(IsSideLocalBrowser(Side));
  2961. if (!ExecuteShell(FileName, L"", Process))
  2962. {
  2963. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  2964. }
  2965. }
  2966. if (IsSideLocalBrowser(Side) ||
  2967. ((ExecuteFileBy == efShell) &&
  2968. !WinConfiguration->Editor.SDIShellEditor) ||
  2969. ((ExecuteFileBy == efExternalEditor) &&
  2970. !ExternalEditor->SDIExternalEditor))
  2971. {
  2972. // no need for handle
  2973. if (Process != NULL)
  2974. {
  2975. DebugCheck(CloseHandle(Process));
  2976. }
  2977. Process = INVALID_HANDLE_VALUE;
  2978. }
  2979. else
  2980. {
  2981. if (Process == NULL)
  2982. {
  2983. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  2984. }
  2985. }
  2986. if (!IsSideLocalBrowser(Side))
  2987. {
  2988. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  2989. }
  2990. }
  2991. }
  2992. //---------------------------------------------------------------------------
  2993. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  2994. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2995. void * /*Arg*/)
  2996. {
  2997. if (Token != NULL)
  2998. {
  2999. EditorFormFileSave(static_cast<TForm *>(Token));
  3000. }
  3001. }
  3002. //---------------------------------------------------------------------------
  3003. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  3004. {
  3005. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  3006. {
  3007. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  3008. }
  3009. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  3010. }
  3011. //---------------------------------------------------------------------------
  3012. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  3013. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  3014. void * Arg)
  3015. {
  3016. if ((Token != NULL) &&
  3017. IsEditorFormModified(static_cast<TForm *>(Token)))
  3018. {
  3019. *static_cast<bool *>(Arg) = true;
  3020. }
  3021. }
  3022. //---------------------------------------------------------------------------
  3023. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  3024. TObject * /*Sender*/, bool & Modified)
  3025. {
  3026. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  3027. {
  3028. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  3029. {
  3030. Modified = true;
  3031. }
  3032. }
  3033. if (!Modified)
  3034. {
  3035. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  3036. }
  3037. }
  3038. //---------------------------------------------------------------------------
  3039. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  3040. {
  3041. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  3042. }
  3043. //---------------------------------------------------------------------------
  3044. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  3045. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  3046. UnicodeString & Result, UnicodeString & RootDirectory)
  3047. {
  3048. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  3049. {
  3050. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  3051. Result = RootDirectory;
  3052. }
  3053. else
  3054. {
  3055. RootDirectory = L"";
  3056. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3057. Result = IncludeTrailingBackslash(Result);
  3058. }
  3059. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3060. {
  3061. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3062. }
  3063. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3064. {
  3065. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3066. {
  3067. RemoteDirectory.Delete(1, 1);
  3068. }
  3069. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3070. }
  3071. if (!ForceDirectories(ApiPath(Result)))
  3072. {
  3073. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3074. }
  3075. }
  3076. //---------------------------------------------------------------------------
  3077. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3078. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3079. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  3080. {
  3081. DebugAssert(!IsLocalBrowserMode());
  3082. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3083. if (ForceText)
  3084. {
  3085. CopyParam.TransferMode = tmAscii;
  3086. }
  3087. TemporaryFileCopyParam(CopyParam);
  3088. if (AllFiles)
  3089. {
  3090. CopyParam.IncludeFileMask = TFileMasks();
  3091. }
  3092. if (TempDir.IsEmpty())
  3093. {
  3094. TemporaryDirectoryForRemoteFiles(FTerminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3095. }
  3096. DebugAssert(!FAutoOperation);
  3097. FAutoOperation = AutoOperation;
  3098. Terminal->ExceptionOnFail = true;
  3099. try
  3100. {
  3101. try
  3102. {
  3103. // turn off confirmations, as for MDI editors we may possibly download
  3104. // the same file over
  3105. Terminal->CopyToLocal(
  3106. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3107. if (GetTargetNames)
  3108. {
  3109. for (int i = 0; i < FileList->Count; i++)
  3110. {
  3111. FileList->Strings[i] =
  3112. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3113. }
  3114. }
  3115. }
  3116. catch(...)
  3117. {
  3118. if (!RootTempDir.IsEmpty())
  3119. {
  3120. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3121. }
  3122. throw;
  3123. }
  3124. }
  3125. __finally
  3126. {
  3127. FAutoOperation = false;
  3128. Terminal->ExceptionOnFail = false;
  3129. }
  3130. }
  3131. //---------------------------------------------------------------------------
  3132. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3133. {
  3134. // Do not waste time checking for default editor list the next time,
  3135. // or testing if default editor is not notepad.
  3136. bool TryNextTime = false;
  3137. UnicodeString UsageState;
  3138. if (!WinConfiguration->EditorList->IsDefaultList())
  3139. {
  3140. UsageState = "H";
  3141. }
  3142. else
  3143. {
  3144. UnicodeString Executable;
  3145. UnicodeString ExecutableDescription;
  3146. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3147. {
  3148. UnicodeString Message =
  3149. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3150. unsigned int Answer =
  3151. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3152. if (Answer != qaOK)
  3153. {
  3154. UsageState = "R";
  3155. }
  3156. else
  3157. {
  3158. UsageState = "A";
  3159. TEditorData EditorData;
  3160. EditorData.Editor = edExternal;
  3161. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3162. EditorData.DecideExternalEditorText();
  3163. TEditorList EditorList;
  3164. EditorList = *WinConfiguration->EditorList;
  3165. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3166. WinConfiguration->EditorList = &EditorList;
  3167. }
  3168. }
  3169. }
  3170. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3171. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3172. }
  3173. //---------------------------------------------------------------------------
  3174. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3175. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3176. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3177. {
  3178. if (ExecuteFileBy == efDefaultEditor)
  3179. {
  3180. if (!WinConfiguration->OfferedEditorAutoConfig)
  3181. {
  3182. EditorAutoConfig();
  3183. }
  3184. const TEditorPreferences * Editor =
  3185. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3186. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3187. {
  3188. ExecuteFileBy = efInternalEditor;
  3189. ExternalEditor = NULL;
  3190. }
  3191. else if (Editor->Data->Editor == edOpen)
  3192. {
  3193. ExecuteFileBy = efShell;
  3194. ExternalEditor = NULL;
  3195. }
  3196. else
  3197. {
  3198. ExecuteFileBy = efExternalEditor;
  3199. ExternalEditor = Editor->Data;
  3200. }
  3201. }
  3202. }
  3203. //---------------------------------------------------------------------------
  3204. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3205. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3206. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3207. {
  3208. UnicodeString OriginalFileName;
  3209. UnicodeString LocalRootDirectory;
  3210. UnicodeString RemoteDirectory;
  3211. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3212. IsSideLocalBrowser(Side), MaskParams);
  3213. UnicodeString Counter;
  3214. UnicodeString LocalFileName;
  3215. bool Handled = false;
  3216. if (!IsSideLocalBrowser(Side))
  3217. {
  3218. // We need to trim VMS version here, so that we use name without version
  3219. // when uploading back to create a new version of the file
  3220. OriginalFileName = FTerminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3221. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3222. TObject * Token = NULL;
  3223. UnicodeString LocalDirectory;
  3224. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3225. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3226. LocalDirectory))
  3227. {
  3228. if (Token != NULL)
  3229. {
  3230. TForm * Form = dynamic_cast<TForm *>(Token);
  3231. if (Form->WindowState == wsMinimized)
  3232. {
  3233. ShowWindow(Form->Handle, SW_RESTORE);
  3234. }
  3235. else
  3236. {
  3237. Form->SetFocus();
  3238. }
  3239. Handled = true;
  3240. }
  3241. else
  3242. {
  3243. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3244. }
  3245. }
  3246. if (!Handled)
  3247. {
  3248. TStringList * FileList1 = new TStringList();
  3249. try
  3250. {
  3251. FileList1->AddObject(FullFileName, Object);
  3252. TemporarilyDownloadFiles(FileList1,
  3253. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3254. LocalRootDirectory, LocalDirectory, true, true, true);
  3255. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3256. }
  3257. __finally
  3258. {
  3259. delete FileList1;
  3260. }
  3261. switch (ExecuteFileBy)
  3262. {
  3263. case efShell:
  3264. Counter = "RemoteFilesExecuted";
  3265. break;
  3266. case efInternalEditor:
  3267. Counter = "RemoteFilesOpenedInInternalEditor";
  3268. break;
  3269. case efExternalEditor:
  3270. Counter = "RemoteFilesOpenedInExternalEditor";
  3271. break;
  3272. default:
  3273. DebugFail();
  3274. }
  3275. }
  3276. }
  3277. else
  3278. {
  3279. LocalFileName = FullFileName;
  3280. OriginalFileName = ExtractFileName(FullFileName);
  3281. switch (ExecuteFileBy)
  3282. {
  3283. case efShell:
  3284. Counter = "LocalFilesExecuted";
  3285. break;
  3286. case efInternalEditor:
  3287. Counter = "LocalFilesOpenedInInternalEditor";
  3288. break;
  3289. case efExternalEditor:
  3290. Counter = "LocalFilesOpenedInExternalEditor";
  3291. break;
  3292. default:
  3293. DebugFail();
  3294. }
  3295. }
  3296. if (!Handled)
  3297. {
  3298. Configuration->Usage->Inc(Counter);
  3299. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3300. ExternalEditor, LocalRootDirectory, RemoteDirectory, false);
  3301. }
  3302. }
  3303. //---------------------------------------------------------------------------
  3304. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3305. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3306. bool AllSelected, bool OnFocused)
  3307. {
  3308. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3309. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3310. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3311. Side = GetSide(Side);
  3312. TCustomDirView * DView = DirView(Side);
  3313. TStrings * FileList =
  3314. AllSelected ?
  3315. DView->CreateFileList(OnFocused, IsSideLocalBrowser(Side)) :
  3316. DView->CreateFocusedFileList(IsSideLocalBrowser(Side));
  3317. try
  3318. {
  3319. DebugAssert(AllSelected || (FileList->Count == 1));
  3320. for (int i = 0; i < FileList->Count; i++)
  3321. {
  3322. UnicodeString ListFileName = FileList->Strings[i];
  3323. UnicodeString FileNameOnly =
  3324. !IsSideLocalBrowser(Side) ? UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3325. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3326. if (!DView->ItemIsDirectory(Item))
  3327. {
  3328. UnicodeString FullFileName;
  3329. if (!IsSideLocalBrowser(Side))
  3330. {
  3331. FullFileName = DirView(Side)->Path + ListFileName;
  3332. }
  3333. else
  3334. {
  3335. FullFileName = ListFileName;
  3336. }
  3337. TFileMasks::TParams MaskParams;
  3338. MaskParams.Size = DView->ItemFileSize(Item);
  3339. TDateTimePrecision Precision;
  3340. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3341. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3342. FileList->Objects[i], MaskParams);
  3343. }
  3344. }
  3345. }
  3346. __finally
  3347. {
  3348. delete FileList;
  3349. }
  3350. }
  3351. //---------------------------------------------------------------------------
  3352. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3353. {
  3354. // do not forget to add additional options to TemporarilyDownloadFiles, and AS
  3355. CopyParam.FileNameCase = ncNoChange;
  3356. CopyParam.PreserveRights = false;
  3357. CopyParam.PreserveReadOnly = false;
  3358. CopyParam.ReplaceInvalidChars = true;
  3359. CopyParam.IncludeFileMask = TFileMasks();
  3360. CopyParam.NewerOnly = false;
  3361. CopyParam.FileMask = L"";
  3362. }
  3363. //---------------------------------------------------------------------------
  3364. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3365. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3366. {
  3367. TTerminalManager * Manager = TTerminalManager::Instance();
  3368. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3369. {
  3370. if (!NonVisualDataModule->Busy)
  3371. {
  3372. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3373. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3374. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3375. if (Data->Terminal == NULL)
  3376. {
  3377. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3378. if (SameSiteTerminal != NULL)
  3379. {
  3380. UnicodeString Message =
  3381. FMTLOAD(EDIT_SESSION_REATTACH,
  3382. (FileNameOnly, Data->SessionName, FileNameOnly));
  3383. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3384. {
  3385. Data->Terminal = Terminal;
  3386. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3387. Data->SessionName = Terminal->SessionData->SessionName;
  3388. // We might also overwrite session data
  3389. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3390. }
  3391. else
  3392. {
  3393. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3394. Abort();
  3395. }
  3396. }
  3397. }
  3398. // foreground session should reconnect itself
  3399. else if (Terminal != Data->Terminal)
  3400. {
  3401. UnicodeString Message =
  3402. MainInstructions(
  3403. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3404. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3405. {
  3406. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3407. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3408. }
  3409. else
  3410. {
  3411. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3412. Abort();
  3413. }
  3414. }
  3415. }
  3416. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3417. {
  3418. Configuration->Usage->Inc(L"EditInactiveSession");
  3419. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3420. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3421. (ExtractFileName(FileName), Data->SessionName)));
  3422. }
  3423. }
  3424. TStrings * FileList = new TStringList();
  3425. try
  3426. {
  3427. FileList->Add(FileName);
  3428. // consider using the same settings (preset) as when the file was downloaded
  3429. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3430. TemporaryFileCopyParam(CopyParam);
  3431. if (Data->ForceText)
  3432. {
  3433. CopyParam.TransferMode = tmAscii;
  3434. }
  3435. // so i do not need to worry if masking algorithm works in all cases
  3436. // ("" means "copy file name", no masking is actually done)
  3437. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3438. {
  3439. CopyParam.FileMask = L"";
  3440. }
  3441. else
  3442. {
  3443. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3444. }
  3445. DebugAssert(Data->Queue != NULL);
  3446. int Params = cpNoConfirmation | cpTemporary;
  3447. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3448. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3449. QueueItem->CompleteEvent = UploadCompleteEvent;
  3450. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3451. }
  3452. __finally
  3453. {
  3454. delete FileList;
  3455. }
  3456. }
  3457. //---------------------------------------------------------------------------
  3458. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3459. const UnicodeString FileName, const TEditedFileData * Data)
  3460. {
  3461. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3462. DebugAssert(!NonVisualDataModule->Busy);
  3463. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3464. {
  3465. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3466. (ExtractFileName(FileName), Data->SessionName)));
  3467. }
  3468. TManagedTerminal * PrevTerminal = TTerminalManager::Instance()->ActiveTerminal;
  3469. TTerminalManager::Instance()->ActiveTerminal = Data->Terminal;
  3470. NonVisualDataModule->StartBusy();
  3471. try
  3472. {
  3473. std::unique_ptr<TRemoteFile> File;
  3474. UnicodeString RemoteFileName =
  3475. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3476. FTerminal->ExceptionOnFail = true;
  3477. try
  3478. {
  3479. TRemoteFile * AFile = NULL;
  3480. FTerminal->ReadFile(RemoteFileName, AFile);
  3481. File.reset(AFile);
  3482. if (!File->HaveFullFileName)
  3483. {
  3484. File->FullFileName = RemoteFileName;
  3485. }
  3486. }
  3487. __finally
  3488. {
  3489. FTerminal->ExceptionOnFail = false;
  3490. }
  3491. std::unique_ptr<TStrings> FileList(new TStringList());
  3492. FileList->AddObject(RemoteFileName, File.get());
  3493. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3494. UnicodeString TempDir = ExtractFilePath(FileName);
  3495. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3496. TempDir, true, true, true);
  3497. // sanity check, the target file name should be still the same
  3498. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3499. }
  3500. __finally
  3501. {
  3502. NonVisualDataModule->EndBusy();
  3503. // it actually may not exist anymore...
  3504. TTerminalManager::Instance()->ActiveTerminal = PrevTerminal;
  3505. }
  3506. }
  3507. //---------------------------------------------------------------------------
  3508. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3509. const TEditedFileData * Data, bool & KeepOpen)
  3510. {
  3511. // Command is set for external editors only (not for "shell" open).
  3512. if (!Data->Command.IsEmpty())
  3513. {
  3514. bool AnyFound = false;
  3515. bool AnyMDI = false;
  3516. bool AnyNonMDI = false;
  3517. bool AnyDetect = false;
  3518. TEditorList * EditorList = new TEditorList();
  3519. try
  3520. {
  3521. *EditorList = *WinConfiguration->EditorList;
  3522. for (int i = 0; i < EditorList->Count; i++)
  3523. {
  3524. const TEditorPreferences * Editor = EditorList->Editors[i];
  3525. if ((Editor->Data->Editor == edExternal) &&
  3526. (Editor->Data->ExternalEditor == Data->Command))
  3527. {
  3528. AnyFound = true;
  3529. if (Editor->Data->SDIExternalEditor)
  3530. {
  3531. AnyNonMDI = true;
  3532. if (Editor->Data->DetectMDIExternalEditor)
  3533. {
  3534. AnyDetect = true;
  3535. }
  3536. }
  3537. else
  3538. {
  3539. AnyMDI = true;
  3540. }
  3541. }
  3542. }
  3543. bool EnableMDI = false;
  3544. bool DisableDetect = false;
  3545. if (AnyMDI)
  3546. {
  3547. KeepOpen = true;
  3548. if (AnyNonMDI)
  3549. {
  3550. // there is at least one instance of the editor with MDI support enabled,
  3551. // and one with disabled, enable it for all instances
  3552. EnableMDI = true;
  3553. }
  3554. }
  3555. else if (AnyFound && !AnyDetect)
  3556. {
  3557. // at least once instance found but all have MDI autodetection disabled
  3558. // => close the file (default action)
  3559. }
  3560. else
  3561. {
  3562. // no instance of the editor has MDI support enabled
  3563. TMessageParams Params;
  3564. if (AnyFound)
  3565. {
  3566. // there is at least one instance of the editor
  3567. Params.Params |= mpNeverAskAgainCheck;
  3568. }
  3569. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3570. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3571. switch (Answer)
  3572. {
  3573. case qaNeverAskAgain:
  3574. DisableDetect = true;
  3575. break;
  3576. case qaNo:
  3577. EnableMDI = true;
  3578. KeepOpen = true;
  3579. break;
  3580. }
  3581. }
  3582. if (AnyFound && (EnableMDI || DisableDetect))
  3583. {
  3584. bool Changed = false;
  3585. for (int i = 0; i < EditorList->Count; i++)
  3586. {
  3587. const TEditorPreferences * Editor = EditorList->Editors[i];
  3588. if ((Editor->Data->Editor == edExternal) &&
  3589. (Editor->Data->ExternalEditor == Data->Command) &&
  3590. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3591. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3592. {
  3593. Changed = true;
  3594. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3595. if (EnableMDI)
  3596. {
  3597. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3598. }
  3599. if (DisableDetect)
  3600. {
  3601. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3602. }
  3603. EditorList->Change(i, UpdatedEditor);
  3604. }
  3605. }
  3606. if (Changed)
  3607. {
  3608. WinConfiguration->EditorList = EditorList;
  3609. }
  3610. }
  3611. }
  3612. __finally
  3613. {
  3614. delete EditorList;
  3615. }
  3616. }
  3617. else
  3618. {
  3619. // "open" case
  3620. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3621. qaOK, HELP_APP_EARLY_CLOSED);
  3622. }
  3623. }
  3624. //---------------------------------------------------------------------------
  3625. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3626. {
  3627. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3628. }
  3629. //---------------------------------------------------------------------------
  3630. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3631. {
  3632. SideEnter(osRemote);
  3633. }
  3634. //---------------------------------------------------------------------------
  3635. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3636. {
  3637. MakeNextInTabOrder(RemoteDirPanel, RemoteDrivePanel);
  3638. SideEnter(osRemote);
  3639. }
  3640. //---------------------------------------------------------------------------
  3641. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3642. {
  3643. FCurrentSide = Side;
  3644. if (Visible)
  3645. {
  3646. UpdateControls();
  3647. }
  3648. }
  3649. //---------------------------------------------------------------------------
  3650. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3651. TStrings * FileList, bool Alternative)
  3652. {
  3653. TCustomDirView * DView = DirView(Side);
  3654. DView->SaveSelection();
  3655. DView->SaveSelectedNames();
  3656. DebugAssert(!FAlternativeDelete);
  3657. FAlternativeDelete = Alternative;
  3658. try
  3659. {
  3660. if (!IsSideLocalBrowser(Side))
  3661. {
  3662. DebugAssert(Terminal != NULL);
  3663. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3664. }
  3665. else
  3666. {
  3667. try
  3668. {
  3669. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3670. }
  3671. __finally
  3672. {
  3673. ReloadLocalDirectory();
  3674. }
  3675. }
  3676. FAlternativeDelete = false;
  3677. }
  3678. catch(...)
  3679. {
  3680. FAlternativeDelete = false;
  3681. DView->DiscardSavedSelection();
  3682. throw;
  3683. }
  3684. DView->RestoreSelection();
  3685. }
  3686. //---------------------------------------------------------------------------
  3687. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3688. {
  3689. DebugAssert(Terminal);
  3690. DebugAssert(!IsLocalBrowserMode());
  3691. RemoteDirView->SaveSelection();
  3692. RemoteDirView->SaveSelectedNames();
  3693. try
  3694. {
  3695. if (Lock)
  3696. {
  3697. Terminal->LockFiles(FileList);
  3698. }
  3699. else
  3700. {
  3701. Terminal->UnlockFiles(FileList);
  3702. }
  3703. }
  3704. catch(...)
  3705. {
  3706. RemoteDirView->DiscardSavedSelection();
  3707. throw;
  3708. }
  3709. RemoteDirView->RestoreSelection();
  3710. }
  3711. //---------------------------------------------------------------------------
  3712. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TManagedTerminal *& Session,
  3713. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3714. bool NoConfirmation, bool Move)
  3715. {
  3716. DebugAssert(Terminal != NULL);
  3717. DebugAssert(!IsLocalBrowserMode());
  3718. // update Terminal->StateData->RemoteDirectory
  3719. UpdateTerminal(Terminal);
  3720. if (Session == NULL)
  3721. {
  3722. Session = TTerminalManager::Instance()->ActiveTerminal;
  3723. }
  3724. if (Session == TTerminalManager::Instance()->ActiveTerminal)
  3725. {
  3726. if (RemoteDriveView->DropTarget != NULL)
  3727. {
  3728. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3729. }
  3730. else if (RemoteDirView->DropTarget != NULL)
  3731. {
  3732. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3733. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3734. }
  3735. else
  3736. {
  3737. Target = RemoteDirView->Path;
  3738. }
  3739. }
  3740. else
  3741. {
  3742. Target = Session->StateData->RemoteDirectory;
  3743. }
  3744. Target = UnixIncludeTrailingBackslash(Target);
  3745. if (FileList->Count == 1)
  3746. {
  3747. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3748. }
  3749. else
  3750. {
  3751. FileMask = AnyMask;
  3752. }
  3753. DirectCopy = FTerminal->IsCapable[fcRemoteCopy] || FTerminal->IsCapable[fcSecondaryShell];
  3754. bool Result = true;
  3755. if (!NoConfirmation)
  3756. {
  3757. bool Multi = (FileList->Count > 1);
  3758. if (Move)
  3759. {
  3760. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3761. }
  3762. else
  3763. {
  3764. TStrings * Sessions = TTerminalManager::Instance()->TerminalList;
  3765. TStrings * Directories = new TStringList;
  3766. try
  3767. {
  3768. for (int Index = 0; Index < Sessions->Count; Index++)
  3769. {
  3770. TManagedTerminal * Terminal =
  3771. dynamic_cast<TManagedTerminal *>(Sessions->Objects[Index]);
  3772. Directories->Add(Terminal->StateData->RemoteDirectory);
  3773. }
  3774. TDirectRemoteCopy AllowDirectCopy;
  3775. if (FTerminal->IsCapable[fcRemoteCopy] || FTerminal->CommandSessionOpened)
  3776. {
  3777. DebugAssert(DirectCopy);
  3778. AllowDirectCopy = drcAllow;
  3779. }
  3780. else if (FTerminal->IsCapable[fcSecondaryShell])
  3781. {
  3782. DebugAssert(DirectCopy);
  3783. AllowDirectCopy = drcConfirmCommandSession;
  3784. }
  3785. else
  3786. {
  3787. DebugAssert(!DirectCopy);
  3788. AllowDirectCopy = drcDisallow;
  3789. }
  3790. void * ASession = Session;
  3791. Result = DoRemoteCopyDialog(Sessions, Directories, AllowDirectCopy,
  3792. Multi, ASession, Target, FileMask, DirectCopy, TTerminalManager::Instance()->ActiveTerminal);
  3793. Session = static_cast<TManagedTerminal *>(ASession);
  3794. }
  3795. __finally
  3796. {
  3797. delete Directories;
  3798. }
  3799. }
  3800. }
  3801. return Result;
  3802. }
  3803. //---------------------------------------------------------------------------
  3804. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3805. TStrings * FileList, bool NoConfirmation, bool Move, TManagedTerminal * Session)
  3806. {
  3807. DebugAssert(!IsLocalBrowserMode());
  3808. bool DirectCopy;
  3809. UnicodeString Target, FileMask;
  3810. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3811. if (Result)
  3812. {
  3813. if (!Move && !DirectCopy)
  3814. {
  3815. Configuration->Usage->Inc("RemoteCopyTemp");
  3816. UnicodeString RootTempDir;
  3817. UnicodeString TempDir;
  3818. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3819. TStrings * TemporaryFilesList = new TStringList();
  3820. try
  3821. {
  3822. TMakeLocalFileListParams MakeFileListParam;
  3823. MakeFileListParam.FileList = TemporaryFilesList;
  3824. MakeFileListParam.FileTimes = NULL;
  3825. MakeFileListParam.IncludeDirs = true;
  3826. MakeFileListParam.Recursive = false;
  3827. ProcessLocalDirectory(TempDir, FTerminal->MakeLocalFileList, &MakeFileListParam);
  3828. TTerminalManager::Instance()->ActiveTerminal = Session;
  3829. if (TemporaryFilesList->Count > 0)
  3830. {
  3831. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3832. CopyParam.FileMask = FileMask;
  3833. DebugAssert(!FAutoOperation);
  3834. FAutoOperation = true;
  3835. FTerminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3836. }
  3837. }
  3838. __finally
  3839. {
  3840. delete TemporaryFilesList;
  3841. FAutoOperation = false;
  3842. if (!RootTempDir.IsEmpty())
  3843. {
  3844. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3845. }
  3846. }
  3847. }
  3848. else
  3849. {
  3850. RemoteDirView->SaveSelection();
  3851. RemoteDirView->SaveSelectedNames();
  3852. try
  3853. {
  3854. if (Move)
  3855. {
  3856. Configuration->Usage->Inc("RemoteMove");
  3857. Terminal->MoveFiles(FileList, Target, FileMask);
  3858. }
  3859. else
  3860. {
  3861. Configuration->Usage->Inc("RemoteCopyDirect");
  3862. DebugAssert(DirectCopy);
  3863. DebugAssert(Session == FTerminal);
  3864. if (FTerminal->IsCapable[fcRemoteCopy] ||
  3865. FTerminal->CommandSessionOpened ||
  3866. CommandSessionFallback())
  3867. {
  3868. Terminal->CopyFiles(FileList, Target, FileMask);
  3869. }
  3870. }
  3871. }
  3872. catch(...)
  3873. {
  3874. RemoteDirView->DiscardSavedSelection();
  3875. throw;
  3876. }
  3877. RemoteDirView->RestoreSelection();
  3878. }
  3879. }
  3880. return Result;
  3881. }
  3882. //---------------------------------------------------------------------------
  3883. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3884. const UnicodeString & Path, TRemoteProperties & Properties)
  3885. {
  3886. DebugAssert(!IsLocalBrowserMode());
  3887. Properties.Valid = Properties.Valid << vpEncrypt;
  3888. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  3889. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  3890. }
  3891. //---------------------------------------------------------------------------
  3892. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3893. {
  3894. Side = GetSide(Side);
  3895. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3896. TRemoteProperties * AProperties = (!IsSideLocalBrowser(Side) ? &Properties : NULL);
  3897. UnicodeString Name = LoadStr(NEW_FOLDER);
  3898. int AllowedChanges =
  3899. FLAGMASK(!IsSideLocalBrowser(Side) && Terminal->IsCapable[fcModeChanging], cpMode);
  3900. bool SaveSettings = false;
  3901. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  3902. {
  3903. TWindowLock Lock(this);
  3904. if (!IsSideLocalBrowser(Side))
  3905. {
  3906. if (SaveSettings)
  3907. {
  3908. GUIConfiguration->NewDirectoryProperties = Properties;
  3909. }
  3910. CreateRemoteDirectory(Name, Properties);
  3911. }
  3912. else
  3913. {
  3914. DirView(Side)->CreateDirectory(Name);
  3915. }
  3916. }
  3917. }
  3918. //---------------------------------------------------------------------------
  3919. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  3920. {
  3921. TWindowLock Lock(this);
  3922. DirView(Side)->ExecuteHomeDirectory();
  3923. }
  3924. //---------------------------------------------------------------------------
  3925. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  3926. {
  3927. DoOpenDirectoryDialog(odBrowse, Side);
  3928. }
  3929. //---------------------------------------------------------------------------
  3930. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  3931. {
  3932. UnicodeString Path = Bookmark->GetSideDirectory(Side);
  3933. bool Result = !Path.IsEmpty();
  3934. if (Result)
  3935. {
  3936. // While we might get here when the session is closed (from location profiles),
  3937. // it's not a problem as the Path setter is noop then
  3938. DirView(Side)->Path = Path;
  3939. }
  3940. return Result;
  3941. }
  3942. //---------------------------------------------------------------------------
  3943. void __fastcall TCustomScpExplorerForm::CalculateSize(
  3944. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  3945. bool & Close)
  3946. {
  3947. // terminal can be already closed (e.g. dropped connection)
  3948. if (Terminal != NULL)
  3949. {
  3950. try
  3951. {
  3952. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  3953. }
  3954. catch(...)
  3955. {
  3956. if (!Terminal->Active)
  3957. {
  3958. Close = true;
  3959. }
  3960. throw;
  3961. }
  3962. }
  3963. }
  3964. //---------------------------------------------------------------------------
  3965. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  3966. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  3967. bool & Close)
  3968. {
  3969. // terminal can be already closed (e.g. dropped connection)
  3970. if (Terminal != NULL)
  3971. {
  3972. Configuration->Usage->Inc(L"ChecksumCalculated");
  3973. try
  3974. {
  3975. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  3976. }
  3977. catch(...)
  3978. {
  3979. if (!Terminal->Active)
  3980. {
  3981. Close = true;
  3982. }
  3983. throw;
  3984. }
  3985. }
  3986. }
  3987. //---------------------------------------------------------------------------
  3988. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  3989. {
  3990. bool Result;
  3991. if (!IsSideLocalBrowser(Side))
  3992. {
  3993. TRemoteTokenList * GroupList = NULL;
  3994. TRemoteTokenList * UserList = NULL;
  3995. try
  3996. {
  3997. TRemoteProperties CurrentProperties;
  3998. if (Terminal->LoadFilesProperties(FileList))
  3999. {
  4000. RemoteDirView->Invalidate();
  4001. }
  4002. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  4003. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  4004. if (CapableGroupChanging || CapableOwnerChanging)
  4005. {
  4006. if (CapableGroupChanging)
  4007. {
  4008. GroupList = Terminal->Groups->Duplicate();
  4009. }
  4010. if (CapableOwnerChanging)
  4011. {
  4012. UserList = Terminal->Users->Duplicate();
  4013. }
  4014. TRemoteDirectory * Files = Terminal->Files;
  4015. int Count = Files->Count;
  4016. if (Count > 100)
  4017. {
  4018. Count = 100;
  4019. }
  4020. for (int Index = 0; Index < Count; Index++)
  4021. {
  4022. TRemoteFile * File = Files->Files[Index];
  4023. if (CapableGroupChanging)
  4024. {
  4025. GroupList->AddUnique(File->Group);
  4026. }
  4027. if (CapableOwnerChanging)
  4028. {
  4029. UserList->AddUnique(File->Owner);
  4030. }
  4031. }
  4032. // if we haven't collected tokens for all files in current directory,
  4033. // make sure we collect them at least for all selected files.
  4034. // (note that so far the files in FileList has to be from current direcotry)
  4035. if (Count < Files->Count)
  4036. {
  4037. for (int Index = 0; Index < FileList->Count; Index++)
  4038. {
  4039. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  4040. if (CapableGroupChanging)
  4041. {
  4042. GroupList->AddUnique(File->Group);
  4043. }
  4044. if (CapableOwnerChanging)
  4045. {
  4046. UserList->AddUnique(File->Owner);
  4047. }
  4048. }
  4049. }
  4050. }
  4051. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  4052. int Flags = 0;
  4053. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  4054. if (CapableOwnerChanging) Flags |= cpOwner;
  4055. if (CapableGroupChanging) Flags |= cpGroup;
  4056. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  4057. if (Terminal->IsCapable[fcCalculatingChecksum])
  4058. {
  4059. CalculateChecksumEvent = CalculateChecksum;
  4060. }
  4061. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4062. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4063. TRemoteProperties NewProperties = CurrentProperties;
  4064. Result =
  4065. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4066. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4067. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4068. CalculateSize, CalculateChecksumEvent);
  4069. if (Result)
  4070. {
  4071. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4072. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4073. }
  4074. }
  4075. __finally
  4076. {
  4077. delete GroupList;
  4078. delete UserList;
  4079. }
  4080. }
  4081. else
  4082. {
  4083. DirView(Side)->DisplayPropertiesMenu();
  4084. Result = true;
  4085. }
  4086. return Result;
  4087. }
  4088. //---------------------------------------------------------------------------
  4089. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4090. {
  4091. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4092. {
  4093. FIgnoreNextDialogChar = Key;
  4094. }
  4095. Key = 0;
  4096. }
  4097. //---------------------------------------------------------------------------
  4098. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4099. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4100. {
  4101. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4102. if (Command != NULL)
  4103. {
  4104. KeyProcessed(Key, Shift);
  4105. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4106. {
  4107. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4108. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4109. false, false, const_cast<TCustomCommandType *>(Command));
  4110. }
  4111. }
  4112. }
  4113. //---------------------------------------------------------------------------
  4114. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4115. {
  4116. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4117. {
  4118. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4119. }
  4120. if (!DirView(osCurrent)->IsEditing())
  4121. {
  4122. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4123. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4124. {
  4125. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4126. if (((Action->ShortCut == KeyShortCut) ||
  4127. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4128. AllowedAction(Action, aaShortCut))
  4129. {
  4130. // Has to be called before the action as the dialog char is already in queue.
  4131. // So when the action consumes message queue, we already need to have the
  4132. // FIgnoreNextDialogChar set
  4133. KeyProcessed(Key, Shift);
  4134. // Reset reference to previous component (when menu/toolbar was clicked).
  4135. // Needed to detect that action was invoked by keyboard shortcut
  4136. // in TNonVisualDataModule::ExplorerActionsExecute
  4137. Action->ActionComponent = NULL;
  4138. Action->Execute();
  4139. return;
  4140. }
  4141. }
  4142. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4143. {
  4144. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4145. {
  4146. KeyProcessed(Key, Shift);
  4147. TTerminalManager::Instance()->ActiveTerminalIndex = i;
  4148. return;
  4149. }
  4150. }
  4151. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4152. {
  4153. KeyProcessed(Key, Shift);
  4154. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4155. }
  4156. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4157. if (IsCustomShortCut(CustomShortCut))
  4158. {
  4159. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4160. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4161. if (WinConfiguration->SharedBookmarks != NULL)
  4162. {
  4163. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4164. if ((Bookmark != NULL) &&
  4165. OpenBookmark(FCurrentSide, Bookmark))
  4166. {
  4167. KeyProcessed(Key, Shift);
  4168. }
  4169. }
  4170. }
  4171. }
  4172. TForm::KeyDown(Key, Shift);
  4173. }
  4174. //---------------------------------------------------------------------------
  4175. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4176. {
  4177. DebugAssert(Terminal != NULL);
  4178. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4179. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  4180. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4181. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4182. bool SecurityEnabled = !SessionInfo.SecurityProtocolName.IsEmpty();
  4183. SessionStatusBar->Panels->Items[Offset + 0]->Enabled = SecurityEnabled;
  4184. // expanded from ?: to avoid memory leaks
  4185. if (SecurityEnabled)
  4186. {
  4187. SessionStatusBar->Panels->Items[Offset + 0]->Hint =
  4188. FMTLOAD(STATUS_SECURE, (SessionInfo.SecurityProtocolName));
  4189. }
  4190. else
  4191. {
  4192. SessionStatusBar->Panels->Items[Offset + 0]->Hint = LoadStr(STATUS_INSECURE);
  4193. }
  4194. if (FileSystemInfo.ProtocolName.IsEmpty())
  4195. {
  4196. SessionStatusBar->Panels->Items[Offset + 1]->Caption = SessionInfo.ProtocolName;
  4197. }
  4198. else
  4199. {
  4200. SessionStatusBar->Panels->Items[Offset + 1]->Caption = FileSystemInfo.ProtocolName;
  4201. }
  4202. SessionStatusBar->Panels->Items[Offset + 1]->Hint = LoadStr(STATUS_PROTOCOL_HINT);
  4203. SessionStatusBar->Panels->Items[Offset + 2]->Enabled =
  4204. (!SessionInfo.CSCompression.IsEmpty() || !SessionInfo.SCCompression.IsEmpty());
  4205. UnicodeString CSCompressionStr = DefaultStr(SessionInfo.CSCompression, LoadStr(NO_STR));
  4206. UnicodeString SCCompressionStr = DefaultStr(SessionInfo.SCCompression, LoadStr(NO_STR));
  4207. UnicodeString CompressionStr = CSCompressionStr;
  4208. if (CSCompressionStr != SCCompressionStr)
  4209. {
  4210. CompressionStr += UnicodeString(L"/") + SCCompressionStr;
  4211. }
  4212. SessionStatusBar->Panels->Items[Offset + 2]->Hint = FMTLOAD(STATUS_COMPRESSION_HINT, (CompressionStr));
  4213. SessionStatusBar->Panels->Items[Offset + 3]->Hint = LoadStr(STATUS_DURATION_HINT);
  4214. UpdateStatusBar();
  4215. }
  4216. //---------------------------------------------------------------------------
  4217. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4218. {
  4219. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4220. DebugAssert(SessionStatusBar != NULL);
  4221. if (!Terminal || !Terminal->Active || Terminal->Status < ssOpened)
  4222. {
  4223. // note: (Terminal->Status < sshReady) currently never happens here,
  4224. // so STATUS_CONNECTING is never used
  4225. SessionStatusBar->SimplePanel = true;
  4226. SessionStatusBar->SimpleText = LoadStr(
  4227. !Terminal || !Terminal->Active ? STATUS_NOT_CONNECTED : STATUS_CONNECTING);
  4228. }
  4229. else
  4230. {
  4231. DebugAssert(Terminal);
  4232. SessionStatusBar->SimplePanel = false;
  4233. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4234. if (!FNote.IsEmpty())
  4235. {
  4236. SessionStatusBar->Panels->Items[0]->Caption = FNote;
  4237. }
  4238. else
  4239. {
  4240. UpdateStatusPanelText(SessionStatusBar->Panels->Items[0]);
  4241. }
  4242. SessionStatusBar->Panels->Items[0]->Hint = FNoteHints;
  4243. SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1]->Caption =
  4244. FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4245. }
  4246. }
  4247. //---------------------------------------------------------------------------
  4248. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4249. {
  4250. Panel->Caption = L"";
  4251. }
  4252. //---------------------------------------------------------------------------
  4253. void __fastcall TCustomScpExplorerForm::Idle()
  4254. {
  4255. if (FShowing || FStandaloneEditing)
  4256. {
  4257. FEditorManager->Check();
  4258. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4259. // that needs to know if queue view is visible (and it may be closed after queue update)
  4260. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4261. }
  4262. if (FShowing)
  4263. {
  4264. if (!NonVisualDataModule->Busy &&
  4265. // Menu is opened or drag&drop is going on
  4266. (Mouse->Capture == NULL))
  4267. {
  4268. if (FRefreshRemoteDirectory)
  4269. {
  4270. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4271. {
  4272. Terminal->RefreshDirectory();
  4273. }
  4274. FRefreshRemoteDirectory = false;
  4275. }
  4276. if (FRefreshLocalDirectory)
  4277. {
  4278. ReloadLocalDirectory();
  4279. FRefreshLocalDirectory = false;
  4280. }
  4281. if (WinConfiguration->RefreshRemotePanel)
  4282. {
  4283. if ((Terminal != NULL) && (Terminal->Status == ssOpened) &&
  4284. (Now() - Terminal->DirectoryLoaded > WinConfiguration->RefreshRemotePanelInterval))
  4285. {
  4286. RemoteDirView->ReloadDirectory();
  4287. }
  4288. }
  4289. }
  4290. }
  4291. if (FShowing || FStandaloneEditing)
  4292. {
  4293. if (FQueueStatusInvalidated)
  4294. {
  4295. UpdateQueueStatus(false);
  4296. }
  4297. RefreshQueueItems();
  4298. }
  4299. if (FShowing)
  4300. {
  4301. UpdateStatusBar();
  4302. }
  4303. FIgnoreNextDialogChar = 0;
  4304. }
  4305. //---------------------------------------------------------------------------
  4306. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4307. {
  4308. try
  4309. {
  4310. FUserActionTimer->Enabled = false;
  4311. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4312. {
  4313. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4314. {
  4315. FPendingQueueActionItem->ProcessUserAction();
  4316. }
  4317. }
  4318. }
  4319. __finally
  4320. {
  4321. FPendingQueueActionItem = NULL;
  4322. }
  4323. }
  4324. //---------------------------------------------------------------------------
  4325. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4326. {
  4327. if (WinConfiguration->MinimizeToTray)
  4328. {
  4329. UpdateTrayIcon();
  4330. FTrayIcon->Visible = true;
  4331. if (Visible)
  4332. {
  4333. ShowWindow(Handle, SW_HIDE);
  4334. }
  4335. }
  4336. }
  4337. //---------------------------------------------------------------------------
  4338. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4339. {
  4340. // WORKAROUND
  4341. // When restoring maximized window from minimization,
  4342. // rarely some controls do not align properly.
  4343. // Two instances seen (both for Commander):
  4344. // - When restoring, window is temporarily narrower (not maximized),
  4345. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4346. // Once maximized already, top dock shinks back, but the session PageControl,
  4347. // do not align up, leaving space between TopDock and PageControl.
  4348. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4349. for (int Index = 0; Index < ControlCount; Index++)
  4350. {
  4351. RealignControl(Controls[Index]);
  4352. }
  4353. if (FTrayIcon->Visible)
  4354. {
  4355. FTrayIcon->Visible = false;
  4356. if (Visible)
  4357. {
  4358. ShowWindow(Handle, SW_SHOW);
  4359. }
  4360. }
  4361. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4362. {
  4363. FNeedSession = false;
  4364. NonVisualDataModule->StartBusy();
  4365. try
  4366. {
  4367. NeedSession(true);
  4368. }
  4369. __finally
  4370. {
  4371. NonVisualDataModule->EndBusy();
  4372. }
  4373. }
  4374. }
  4375. //---------------------------------------------------------------------------
  4376. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4377. {
  4378. FTrayIcon->Hint = GetMainForm()->Caption;
  4379. }
  4380. //---------------------------------------------------------------------------
  4381. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4382. {
  4383. UpdateTrayIcon();
  4384. }
  4385. //---------------------------------------------------------------------------
  4386. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4387. {
  4388. // workaround
  4389. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4390. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4391. // But WM_ACTIVATEAPP message can be received even when
  4392. // the main window is minimized to the tray and internal editor window is focused
  4393. // (after another application was previously active)
  4394. if (::IsIconic(Handle))
  4395. {
  4396. if (!IsAppIconic())
  4397. {
  4398. SetAppIconic(true);
  4399. }
  4400. }
  4401. ::ApplicationRestore();
  4402. Application->BringToFront();
  4403. }
  4404. //---------------------------------------------------------------------------
  4405. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4406. {
  4407. RestoreApp();
  4408. }
  4409. //---------------------------------------------------------------------------
  4410. void __fastcall TCustomScpExplorerForm::NewSession(bool FromSite, const UnicodeString & SessionUrl)
  4411. {
  4412. if (OpenInNewWindow())
  4413. {
  4414. // todo: Pass FromSite
  4415. ExecuteNewInstance(SessionUrl);
  4416. }
  4417. else
  4418. {
  4419. TTerminalManager::Instance()->NewSession(FromSite, SessionUrl);
  4420. }
  4421. }
  4422. //---------------------------------------------------------------------------
  4423. UnicodeString __fastcall TCustomScpExplorerForm::SaveHiddenDuplicateSession(TSessionData * SessionData)
  4424. {
  4425. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->SessionName;
  4426. StoredSessions->NewSession(SessionName, SessionData);
  4427. // modified only, explicit
  4428. StoredSessions->Save(false, true);
  4429. // encode session name because of slashes in hierarchical sessions
  4430. return EncodeUrlString(SessionName);
  4431. }
  4432. //---------------------------------------------------------------------------
  4433. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4434. {
  4435. // current working directories become defaults here, what is not right
  4436. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4437. return SaveHiddenDuplicateSession(SessionData.get());
  4438. }
  4439. //---------------------------------------------------------------------------
  4440. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4441. {
  4442. if (OpenInNewWindow())
  4443. {
  4444. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4445. }
  4446. else
  4447. {
  4448. // current working directories become defaults here, what is not right
  4449. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4450. TTerminalManager * Manager = TTerminalManager::Instance();
  4451. TManagedTerminal * Terminal = Manager->NewManagedTerminal(SessionData.get());
  4452. Manager->ActiveTerminal = Terminal;
  4453. }
  4454. }
  4455. //---------------------------------------------------------------------------
  4456. void __fastcall TCustomScpExplorerForm::RenameSession()
  4457. {
  4458. UnicodeString Name = Terminal->SessionData->SessionName;
  4459. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME))
  4460. {
  4461. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4462. TSessionData::ValidateName(Name);
  4463. // This is inconsistent with how color (for example) is handled.
  4464. Terminal->SessionData->Name = Name;
  4465. UpdateControls();
  4466. // Add/Remove distinguishing paths from sessions of the same name.
  4467. DoTerminalListChanged();
  4468. }
  4469. }
  4470. //---------------------------------------------------------------------------
  4471. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4472. {
  4473. DebugAssert(Queue != NULL);
  4474. bool Result = Queue->IsEmpty;
  4475. if (!Result)
  4476. {
  4477. SetFocus();
  4478. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4479. }
  4480. return Result;
  4481. }
  4482. //---------------------------------------------------------------------------
  4483. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4484. {
  4485. return CanCloseQueue(FQueue);
  4486. }
  4487. //---------------------------------------------------------------------------
  4488. void __fastcall TCustomScpExplorerForm::CloseSession()
  4489. {
  4490. if (CanCloseQueue())
  4491. {
  4492. TTerminalManager::Instance()->FreeActiveTerminal();
  4493. }
  4494. }
  4495. //---------------------------------------------------------------------------
  4496. void __fastcall TCustomScpExplorerForm::DisconnectSession()
  4497. {
  4498. if (CanCloseQueue())
  4499. {
  4500. TTerminalManager::Instance()->DisconnectActiveTerminal();
  4501. }
  4502. }
  4503. //---------------------------------------------------------------------------
  4504. void __fastcall TCustomScpExplorerForm::TerminalDisconnected()
  4505. {
  4506. RemoteDirView->Terminal = Terminal;
  4507. UpdateRemotePathComboBox(false);
  4508. UpdateControls();
  4509. }
  4510. //---------------------------------------------------------------------------
  4511. void __fastcall TCustomScpExplorerForm::ReconnectSession()
  4512. {
  4513. if (DebugAlwaysTrue(!Terminal->Active))
  4514. {
  4515. TTerminalManager::Instance()->ReconnectActiveTerminal();
  4516. }
  4517. }
  4518. //---------------------------------------------------------------------------
  4519. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4520. {
  4521. if (OpenInNewWindow())
  4522. {
  4523. // encode session name because of slashes in hierarchical sessions
  4524. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4525. }
  4526. else
  4527. {
  4528. TTerminalManager * Manager = TTerminalManager::Instance();
  4529. TManagedTerminal * Terminal = Manager->NewManagedTerminal(Data);
  4530. Manager->ActiveTerminal = Terminal;
  4531. }
  4532. }
  4533. //---------------------------------------------------------------------------
  4534. void __fastcall TCustomScpExplorerForm::DoOpenFolderOrWorkspace(const UnicodeString & Name, bool ConnectFirstTerminal)
  4535. {
  4536. TTerminalManager * Manager = TTerminalManager::Instance();
  4537. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4538. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4539. TManagedTerminal * FirstTerminal = Manager->NewTerminals(DataList.get());
  4540. if (!ConnectFirstTerminal)
  4541. {
  4542. FirstTerminal->Disconnected = true;
  4543. FirstTerminal->DisconnectedTemporarily = true;
  4544. }
  4545. Manager->ActiveTerminal = FirstTerminal;
  4546. }
  4547. //---------------------------------------------------------------------------
  4548. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4549. {
  4550. if (OpenInNewWindow())
  4551. {
  4552. ExecuteNewInstance(Name);
  4553. }
  4554. else
  4555. {
  4556. DoOpenFolderOrWorkspace(Name, true);
  4557. }
  4558. }
  4559. //---------------------------------------------------------------------------
  4560. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4561. bool &CanClose)
  4562. {
  4563. if (Terminal != NULL)
  4564. {
  4565. if (Terminal->Active && WinConfiguration->ConfirmClosingSession)
  4566. {
  4567. unsigned int Result;
  4568. TMessageParams Params(mpNeverAskAgainCheck);
  4569. UnicodeString Message;
  4570. int Answers = qaOK | qaCancel;
  4571. if (TTerminalManager::Instance()->Count > 1)
  4572. {
  4573. if (!WinConfiguration->AutoSaveWorkspace)
  4574. {
  4575. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE3);
  4576. }
  4577. else
  4578. {
  4579. Message = LoadStr(CLOSE_SESSIONS);
  4580. }
  4581. }
  4582. else
  4583. {
  4584. if (!WinConfiguration->AutoSaveWorkspace)
  4585. {
  4586. Message = LoadStr(CLOSE_SESSION_WORKSPACE);
  4587. }
  4588. else
  4589. {
  4590. Message = LoadStr(CLOSE_SESSION);
  4591. }
  4592. Message = FORMAT(Message, (Terminal->SessionData->SessionName));
  4593. }
  4594. UnicodeString Note;
  4595. if (WinConfiguration->AutoSaveWorkspace)
  4596. {
  4597. Note = FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()));
  4598. }
  4599. else
  4600. {
  4601. Note = LoadStr(AUTO_WORKSPACE_ENABLE);
  4602. Answers = qaYes | qaNo | qaCancel;
  4603. }
  4604. Message = FORMAT("%s\n\n%s", (MainInstructions(Message), Note));
  4605. SetFocus();
  4606. Result = MessageDialog(Message, qtConfirmation,
  4607. Answers, HELP_NONE, &Params);
  4608. if (Result == qaNeverAskAgain)
  4609. {
  4610. WinConfiguration->ConfirmClosingSession = false;
  4611. }
  4612. if (Result == qaNo)
  4613. {
  4614. CanClose = SaveWorkspace(true);
  4615. // note that the workspace will be saved redundatly again from FormClose
  4616. }
  4617. else
  4618. {
  4619. CanClose =
  4620. (Result == qaOK) ||
  4621. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4622. (Result == qaNeverAskAgain);
  4623. }
  4624. }
  4625. if (CanClose)
  4626. {
  4627. CanClose = CanCloseQueue();
  4628. }
  4629. }
  4630. if (CanClose)
  4631. {
  4632. CanClose =
  4633. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4634. FEditorManager->CloseExternalFilesWithoutProcess();
  4635. if (CanClose)
  4636. {
  4637. while (CanClose && (FLocalEditors->Count > 0))
  4638. {
  4639. int PrevCount = FLocalEditors->Count;
  4640. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4641. CanClose = (FLocalEditors->Count < PrevCount);
  4642. }
  4643. if (CanClose)
  4644. {
  4645. CanClose = FEditorManager->Empty(true);
  4646. if (!CanClose)
  4647. {
  4648. SetFocus();
  4649. CanClose =
  4650. (MessageDialog(
  4651. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4652. }
  4653. }
  4654. }
  4655. }
  4656. }
  4657. //---------------------------------------------------------------------------
  4658. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4659. {
  4660. TForm * Form = dynamic_cast<TForm *>(Sender);
  4661. DebugAssert(Form != NULL);
  4662. Form->Close();
  4663. }
  4664. //---------------------------------------------------------------------------
  4665. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4666. {
  4667. TForm * Form = dynamic_cast<TForm *>(Sender);
  4668. delete Form;
  4669. }
  4670. //---------------------------------------------------------------------------
  4671. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4672. {
  4673. while (FLocalEditors->Count > 0)
  4674. {
  4675. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4676. }
  4677. }
  4678. //---------------------------------------------------------------------------
  4679. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4680. TObject *Sender)
  4681. {
  4682. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4683. try
  4684. {
  4685. SetProperties(osRemote, FileList);
  4686. }
  4687. __finally
  4688. {
  4689. delete FileList;
  4690. }
  4691. }
  4692. //---------------------------------------------------------------------------
  4693. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4694. {
  4695. if (value)
  4696. {
  4697. if (Component == fcCustomCommandsBand)
  4698. {
  4699. UpdateCustomCommandsToolbar();
  4700. }
  4701. }
  4702. }
  4703. //---------------------------------------------------------------------------
  4704. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4705. {
  4706. TControl * Control = GetComponent(Component);
  4707. DebugAssert(Control);
  4708. bool Changed = (Control->Visible != value);
  4709. if (Changed)
  4710. {
  4711. ComponentShowing(Component, value);
  4712. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4713. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4714. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4715. if (value)
  4716. {
  4717. int RemainingHeight = Control->Parent->ClientHeight;
  4718. int RemainingWidth = Control->Parent->ClientWidth;
  4719. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4720. {
  4721. TControl * ChildControl = Control->Parent->Controls[i];
  4722. if (ChildControl->Visible)
  4723. {
  4724. switch (ChildControl->Align)
  4725. {
  4726. case alTop:
  4727. case alBottom:
  4728. RemainingHeight -= ChildControl->Height;
  4729. break;
  4730. case alLeft:
  4731. case alRight:
  4732. RemainingWidth -= ChildControl->Width;
  4733. break;
  4734. }
  4735. }
  4736. }
  4737. int Reserve = ScaleByTextHeight(this, 32);
  4738. // queue in explorer, trees in commander
  4739. if (Control->Height > RemainingHeight - Reserve)
  4740. {
  4741. Control->Height = RemainingHeight / 2;
  4742. }
  4743. if (Control->Width > RemainingWidth - Reserve)
  4744. {
  4745. Control->Width = RemainingWidth / 2;
  4746. }
  4747. }
  4748. Control->Visible = value;
  4749. if (WasFocused && Visible)
  4750. {
  4751. DirView(osCurrent)->SetFocus();
  4752. }
  4753. FixControlsPlacement();
  4754. }
  4755. }
  4756. //---------------------------------------------------------------------------
  4757. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4758. {
  4759. TControl * Control = GetComponent(Component);
  4760. if (Control == NULL)
  4761. {
  4762. return false;
  4763. }
  4764. else
  4765. {
  4766. return Control->Visible;
  4767. }
  4768. }
  4769. //---------------------------------------------------------------------------
  4770. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4771. {
  4772. bool Result;
  4773. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4774. {
  4775. Result = !IsUWP();
  4776. }
  4777. else
  4778. {
  4779. Result = true;
  4780. }
  4781. return Result;
  4782. }
  4783. //---------------------------------------------------------------------------
  4784. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4785. {
  4786. if (DirView(osOther)->ItemFocused != NULL)
  4787. {
  4788. DirView(osOther)->ItemFocused->MakeVisible(false);
  4789. }
  4790. QueueSplitter->Visible = QueuePanel->Visible;
  4791. RemotePanelSplitter->Visible = RemoteDrivePanel->Visible;
  4792. }
  4793. //---------------------------------------------------------------------------
  4794. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4795. {
  4796. switch (Component) {
  4797. case fcStatusBar: return RemoteStatusBar;
  4798. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4799. case fcQueueView: return QueuePanel;
  4800. case fcQueueToolbar: return QueueDock;
  4801. case fcRemoteTree: return RemoteDrivePanel;
  4802. case fcSessionsTabs: return SessionsPageControl;
  4803. default: return NULL;
  4804. }
  4805. }
  4806. //---------------------------------------------------------------------------
  4807. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4808. TObject *Sender, TListColumn *Column, TPoint &Point)
  4809. {
  4810. DebugAssert(NonVisualDataModule && Column && Sender);
  4811. NonVisualDataModule->ListColumn = Column;
  4812. TPopupMenu * DirViewColumnMenu;
  4813. if (dynamic_cast<TUnixDirView *>(Sender) != NULL)
  4814. {
  4815. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4816. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4817. (Column->Index == uvName);
  4818. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4819. (Column->Index == uvSize);
  4820. }
  4821. else
  4822. {
  4823. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4824. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4825. (Column->Index == dvName);
  4826. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4827. (Column->Index == dvSize);
  4828. }
  4829. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  4830. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  4831. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  4832. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4833. }
  4834. //---------------------------------------------------------------------------
  4835. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4836. TObject *Sender, TListItem *Item, bool &AllowExec)
  4837. {
  4838. DoDirViewExecFile(Sender, Item, AllowExec);
  4839. }
  4840. //---------------------------------------------------------------------------
  4841. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4842. TListItem * Item, bool & AllowExec)
  4843. {
  4844. DebugAssert(Sender && Item && Configuration);
  4845. DebugAssert(AllowExec);
  4846. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  4847. bool Remote = (ADirView == DirView(osRemote)) && !IsSideLocalBrowser(osRemote);
  4848. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks;
  4849. TOperationSide Side = (Remote ? osRemote : osLocal); // TODO
  4850. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  4851. // on files only (not directories)
  4852. // and only when symlinks are resolved (apply to remote panel only)
  4853. if (!ADirView->ItemIsDirectory(Item) &&
  4854. (ResolvedSymlinks || FForceExecution || Terminal->IsEncryptingFiles()))
  4855. {
  4856. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  4857. !FForceExecution &&
  4858. (ResolvedSymlinks || Terminal->IsEncryptingFiles()))
  4859. {
  4860. if (WinConfiguration->DoubleClickAction == dcaCopy)
  4861. {
  4862. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  4863. UnlockWindow();
  4864. try
  4865. {
  4866. ExecuteFileOperation(
  4867. foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  4868. }
  4869. __finally
  4870. {
  4871. LockWindow();
  4872. }
  4873. AllowExec = false;
  4874. }
  4875. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  4876. {
  4877. if (!Remote || !WinConfiguration->DisableOpenEdit)
  4878. {
  4879. ExecuteFile(Side, efDefaultEditor);
  4880. AllowExec = false;
  4881. }
  4882. }
  4883. else
  4884. {
  4885. DebugFail();
  4886. }
  4887. }
  4888. // if we have not done anything special, fall back to default behavior
  4889. if (AllowExec)
  4890. {
  4891. if (Remote && !WinConfiguration->DisableOpenEdit)
  4892. {
  4893. ExecuteFile(osRemote, efShell);
  4894. AllowExec = false;
  4895. }
  4896. }
  4897. }
  4898. }
  4899. //---------------------------------------------------------------------------
  4900. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  4901. {
  4902. return ((Side == osRemote) || (Side == osCurrent));
  4903. }
  4904. //---------------------------------------------------------------------------
  4905. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  4906. {
  4907. DebugFail();
  4908. }
  4909. //---------------------------------------------------------------------------
  4910. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  4911. {
  4912. DebugFail();
  4913. }
  4914. //---------------------------------------------------------------------------
  4915. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  4916. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int UseDefaults)
  4917. {
  4918. TSynchronizeParamType Params;
  4919. Params.LocalDirectory = LocalDirectory;
  4920. Params.RemoteDirectory = RemoteDirectory;
  4921. int UnusedParams =
  4922. (GUIConfiguration->SynchronizeParams &
  4923. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize));
  4924. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  4925. Params.Options = GUIConfiguration->SynchronizeOptions;
  4926. bool SaveSettings = false;
  4927. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  4928. &DoSynchronizeTooManyDirectories);
  4929. DebugAssert(FSynchronizeController == NULL);
  4930. FSynchronizeController = &Controller;
  4931. bool Result;
  4932. try
  4933. {
  4934. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4935. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  4936. int Options =
  4937. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  4938. DebugAssert(FOnFeedSynchronizeError == NULL);
  4939. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  4940. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  4941. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  4942. if (Result)
  4943. {
  4944. if (SaveSettings)
  4945. {
  4946. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  4947. GUIConfiguration->SynchronizeOptions = Params.Options;
  4948. }
  4949. else
  4950. {
  4951. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  4952. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  4953. FLAGSET(Params.Options, soSynchronize))
  4954. {
  4955. GUIConfiguration->SynchronizeOptions =
  4956. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  4957. soSynchronize;
  4958. }
  4959. }
  4960. LocalDirectory = Params.LocalDirectory;
  4961. RemoteDirectory = Params.RemoteDirectory;
  4962. }
  4963. }
  4964. __finally
  4965. {
  4966. FSynchronizeController = NULL;
  4967. DebugAssert(FOnFeedSynchronizeError == NULL);
  4968. FOnFeedSynchronizeError = NULL;
  4969. }
  4970. return Result;
  4971. }
  4972. //---------------------------------------------------------------------------
  4973. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  4974. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  4975. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  4976. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  4977. TSynchronizeOptions * Options, bool Full)
  4978. {
  4979. // Keep-up-to-date mode
  4980. if (Terminal->Status == ssOpened)
  4981. {
  4982. try
  4983. {
  4984. int PParams = Params.Params;
  4985. if (!Full)
  4986. {
  4987. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  4988. if (FLAGSET(Params.Options, soRecurse))
  4989. {
  4990. PParams |= TTerminal::spSubDirs;
  4991. }
  4992. }
  4993. else
  4994. {
  4995. // if keepuptodate is non-recursive,
  4996. // full sync before has to be non-recursive as well
  4997. if (FLAGCLEAR(Params.Options, soRecurse))
  4998. {
  4999. PParams |= TTerminal::spNoRecurse;
  5000. }
  5001. }
  5002. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  5003. PParams, Checklist, Options);
  5004. }
  5005. catch (Exception & E)
  5006. {
  5007. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  5008. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  5009. {
  5010. // noop, already logged in MoreMessageDialog
  5011. }
  5012. else
  5013. {
  5014. ShowExtendedExceptionEx(Terminal, &E);
  5015. throw;
  5016. }
  5017. }
  5018. }
  5019. }
  5020. //---------------------------------------------------------------------------
  5021. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  5022. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  5023. const UnicodeString ErrorStr)
  5024. {
  5025. if (!Directory.IsEmpty())
  5026. {
  5027. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  5028. }
  5029. else
  5030. {
  5031. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  5032. }
  5033. }
  5034. //---------------------------------------------------------------------------
  5035. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  5036. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  5037. {
  5038. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  5039. {
  5040. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  5041. }
  5042. else
  5043. {
  5044. TMessageParams Params(mpNeverAskAgainCheck);
  5045. unsigned int Result = MessageDialog(
  5046. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  5047. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  5048. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  5049. {
  5050. MaxDirectories *= 2;
  5051. if (Result == qaNeverAskAgain)
  5052. {
  5053. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  5054. }
  5055. }
  5056. else
  5057. {
  5058. Abort();
  5059. }
  5060. }
  5061. }
  5062. //---------------------------------------------------------------------------
  5063. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  5064. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  5065. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  5066. TSynchronizeOptions * Options)
  5067. {
  5068. // Keep-up-to-date mode
  5069. DebugAssert(!FAutoOperation);
  5070. void * BatchStorage;
  5071. BatchStart(BatchStorage);
  5072. FAutoOperation = true;
  5073. bool AnyOperation = false;
  5074. TDateTime StartTime = Now();
  5075. TSynchronizeChecklist * AChecklist = NULL;
  5076. try
  5077. {
  5078. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, -1);
  5079. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  5080. {
  5081. FSynchronizeProgressForm->Start();
  5082. }
  5083. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5084. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5085. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5086. Options);
  5087. SAFE_DESTROY(FSynchronizeProgressForm);
  5088. AnyOperation = (AChecklist->CheckedCount > 0);
  5089. if (AnyOperation)
  5090. {
  5091. TSynchronizeProgress SynchronizeProgress(AChecklist);
  5092. CreateProgressForm(&SynchronizeProgress);
  5093. try
  5094. {
  5095. Terminal->SynchronizeApply(
  5096. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5097. SynchronizeProcessedItem, NULL, NULL, NULL);
  5098. }
  5099. __finally
  5100. {
  5101. DestroyProgressForm();
  5102. }
  5103. }
  5104. }
  5105. __finally
  5106. {
  5107. if (Checklist == NULL)
  5108. {
  5109. delete AChecklist;
  5110. }
  5111. else
  5112. {
  5113. *Checklist = AChecklist;
  5114. }
  5115. FAutoOperation = false;
  5116. SAFE_DESTROY(FSynchronizeProgressForm);
  5117. BatchEnd(BatchStorage);
  5118. ReloadLocalDirectory();
  5119. if (AnyOperation)
  5120. {
  5121. OperationComplete(StartTime);
  5122. }
  5123. }
  5124. }
  5125. //---------------------------------------------------------------------------
  5126. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5127. {
  5128. // can be called from command line
  5129. return Visible &&
  5130. ((DirView(osOther)->SelCount > 0) ||
  5131. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5132. }
  5133. //---------------------------------------------------------------------------
  5134. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5135. {
  5136. LogSynchronizeEvent(Terminal, Message);
  5137. }
  5138. //---------------------------------------------------------------------------
  5139. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5140. int Params, TSynchronizeOptions & Options)
  5141. {
  5142. DebugAssert(!IsLocalBrowserMode());
  5143. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5144. {
  5145. Options.Filter = new TStringList();
  5146. Options.Filter->CaseSensitive = false;
  5147. Options.Filter->Duplicates = Types::dupAccept;
  5148. if (DirView(osRemote)->SelCount > 0)
  5149. {
  5150. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5151. }
  5152. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5153. {
  5154. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5155. }
  5156. Options.Filter->Sort();
  5157. }
  5158. }
  5159. //---------------------------------------------------------------------------
  5160. UnicodeString __fastcall TCustomScpExplorerForm::SerializeCopyParamForCommandLine(const TCopyParamType * CopyParams)
  5161. {
  5162. TCopyParamType Defaults;
  5163. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5164. ConfigStorage->AccessMode = smRead;
  5165. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false))
  5166. {
  5167. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5168. }
  5169. ConfigStorage.reset(NULL);
  5170. std::unique_ptr<TStringList> Options(new TStringList());
  5171. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5172. CopyParams->Save(OptionsStorage.get(), &Defaults);
  5173. UnicodeString Result;
  5174. if (Options->Count > 0)
  5175. {
  5176. Result =
  5177. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), StringsToParams(Options.get())));
  5178. }
  5179. return Result;
  5180. }
  5181. //---------------------------------------------------------------------------
  5182. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5183. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5184. {
  5185. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5186. UnicodeString AdditionalParams =
  5187. FORMAT(L"%s \"%s\" \"%s\" %d %d %s%s", (
  5188. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5189. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options,
  5190. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5191. SerializeCopyParamForCommandLine(CopyParams)));
  5192. ExecuteNewInstance(SessionName, AdditionalParams);
  5193. }
  5194. //---------------------------------------------------------------------------
  5195. struct TSynchronizeParams
  5196. {
  5197. UnicodeString LocalDirectory;
  5198. UnicodeString RemoteDirectory;
  5199. TSynchronizeMode Mode;
  5200. TSynchronizeChecklist * Checklist;
  5201. int Params;
  5202. TCopyParamType * CopyParam;
  5203. TDateTime CollectElapsed;
  5204. TDateTime * StartTime;
  5205. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5206. };
  5207. //---------------------------------------------------------------------------
  5208. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5209. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5210. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5211. {
  5212. DebugAssert(!FAutoOperation);
  5213. void * BatchStorage;
  5214. BatchStart(BatchStorage);
  5215. FAutoOperation = true;
  5216. TFileOperationStatistics Statistics;
  5217. TDateTime Start = Now();
  5218. try
  5219. {
  5220. Params.OnProcessedItem = OnProcessedItem;
  5221. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5222. CreateProgressForm(&SynchronizeProgress);
  5223. Terminal->SynchronizeApply(
  5224. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5225. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5226. &Statistics);
  5227. }
  5228. __finally
  5229. {
  5230. Params.OnProcessedItem = NULL;
  5231. FAutoOperation = false;
  5232. DestroyProgressForm();
  5233. BatchEnd(BatchStorage);
  5234. ReloadLocalDirectory();
  5235. }
  5236. if (WinConfiguration->SynchronizeSummary)
  5237. {
  5238. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5239. // The statistics should be 0 anyway in this case
  5240. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5241. {
  5242. Message += L"\n";
  5243. if (Statistics.FilesUploaded > 0)
  5244. {
  5245. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5246. }
  5247. if (Statistics.FilesDownloaded > 0)
  5248. {
  5249. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5250. }
  5251. if (Statistics.FilesDeletedLocal > 0)
  5252. {
  5253. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5254. }
  5255. if (Statistics.FilesDeletedRemote > 0)
  5256. {
  5257. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5258. }
  5259. }
  5260. TDateTime Elapsed = (Now() - Start);
  5261. Message +=
  5262. L"\n" +
  5263. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5264. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5265. TMessageParams Params(mpNeverAskAgainCheck);
  5266. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5267. if (Result == qaNeverAskAgain)
  5268. {
  5269. WinConfiguration->SynchronizeSummary = false;
  5270. }
  5271. }
  5272. }
  5273. //---------------------------------------------------------------------------
  5274. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5275. {
  5276. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5277. {
  5278. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5279. }
  5280. // Not set in keep-up-to-date mode - Synchronize() method
  5281. if (Token != NULL)
  5282. {
  5283. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5284. if (Params.OnProcessedItem != NULL)
  5285. {
  5286. Params.OnProcessedItem(NULL, ChecklistItem);
  5287. }
  5288. }
  5289. }
  5290. //---------------------------------------------------------------------------
  5291. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5292. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5293. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5294. {
  5295. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5296. *Params.StartTime = Now();
  5297. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5298. }
  5299. //---------------------------------------------------------------------------
  5300. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5301. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5302. {
  5303. // terminal can be already closed (e.g. dropped connection)
  5304. if (Terminal != NULL)
  5305. {
  5306. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5307. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5308. }
  5309. }
  5310. //---------------------------------------------------------------------------
  5311. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5312. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5313. {
  5314. DebugAssert(!IsLocalBrowserMode());
  5315. TAutoBatch AutoBatch(this);
  5316. TAutoFlag AutoOperationFlag(FAutoOperation);
  5317. if (Side == osRemote)
  5318. {
  5319. std::unique_ptr<TStrings> FileList(new TStringList());
  5320. FileList->AddObject(FileName, RemoteFile);
  5321. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5322. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5323. RemoteDirView->SaveSelection();
  5324. RemoteDirView->SaveSelectedNames();
  5325. try
  5326. {
  5327. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5328. }
  5329. catch(...)
  5330. {
  5331. RemoteDirView->DiscardSavedSelection();
  5332. throw;
  5333. }
  5334. RemoteDirView->RestoreSelection();
  5335. }
  5336. else if (DebugAlwaysTrue(Side == osLocal))
  5337. {
  5338. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5339. {
  5340. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5341. }
  5342. UnicodeString Directory = ExtractFileDir(FileName);
  5343. ReloadLocalDirectory(Directory);
  5344. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5345. if (!SamePaths(Directory, NewDirectory))
  5346. {
  5347. ReloadLocalDirectory(NewDirectory);
  5348. }
  5349. }
  5350. }
  5351. //---------------------------------------------------------------------------
  5352. void __fastcall TCustomScpExplorerForm::DoSynchronizeBrowse(TOperationSide Side, TSynchronizeChecklist::TAction Action, const TSynchronizeChecklist::TItem * Item)
  5353. {
  5354. UnicodeString LocalPath = ExcludeTrailingBackslash(Item->Local.Directory);
  5355. if (Side == osLocal)
  5356. {
  5357. if (Action == TSynchronizeChecklist::saDownloadNew)
  5358. {
  5359. OpenFolderInExplorer(LocalPath);
  5360. }
  5361. else
  5362. {
  5363. OpenFileInExplorer(TPath::Combine(LocalPath, Item->GetFileName()));
  5364. }
  5365. }
  5366. else if (DebugAlwaysTrue(Side == osRemote))
  5367. {
  5368. // Similar to CreateHiddenDuplicateSession, except that it modifies the initial directories
  5369. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  5370. SessionData->RemoteDirectory = UnixExcludeTrailingBackslash(Item->Remote.Directory);
  5371. if (!LocalPath.IsEmpty())
  5372. {
  5373. SessionData->LocalDirectory = LocalPath;
  5374. }
  5375. UnicodeString SessionName = SaveHiddenDuplicateSession(SessionData.get());
  5376. ExecuteNewInstance(SessionName, FORMAT(L"%s=%s", (TProgramParams::FormatSwitch(BROWSE_SWITCH), Item->GetFileName())));
  5377. }
  5378. }
  5379. //---------------------------------------------------------------------------
  5380. void __fastcall TCustomScpExplorerForm::FullSynchronizeInNewWindow(
  5381. TSynchronizeMode Mode, int Params, const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5382. const TCopyParamType * CopyParams)
  5383. {
  5384. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5385. UnicodeString AdditionalParams =
  5386. FORMAT(L"%s \"%s\" \"%s\" %d %d %s%s", (
  5387. TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH),
  5388. LocalDirectory, RemoteDirectory, Mode, Params,
  5389. TProgramParams::FormatSwitch(DEFAULTS_SWITCH),
  5390. SerializeCopyParamForCommandLine(CopyParams)));
  5391. ExecuteNewInstance(SessionName, AdditionalParams);
  5392. }
  5393. //---------------------------------------------------------------------------
  5394. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5395. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5396. TSynchronizeMode & Mode, int Params, bool & SaveMode, int UseDefaults)
  5397. {
  5398. int Result;
  5399. bool SaveSettings = false;
  5400. int Options =
  5401. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5402. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5403. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5404. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5405. bool Continue =
  5406. (UseDefaults == 0) ||
  5407. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5408. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs, FullSynchronizeInNewWindow, UseDefaults);
  5409. if (Continue)
  5410. {
  5411. Configuration->Usage->Inc(L"Synchronizations");
  5412. UpdateCopyParamCounters(CopyParam);
  5413. TSynchronizeOptions SynchronizeOptions;
  5414. GetSynchronizeOptions(Params, SynchronizeOptions);
  5415. if (SaveSettings)
  5416. {
  5417. GUIConfiguration->SynchronizeParams = Params;
  5418. }
  5419. else
  5420. {
  5421. SaveMode = false;
  5422. }
  5423. TDateTime StartTime = Now();
  5424. TSynchronizeChecklist * Checklist = NULL;
  5425. try
  5426. {
  5427. DebugAssert(!FAutoOperation);
  5428. FAutoOperation = true;
  5429. try
  5430. {
  5431. UnicodeString SessionKey = Terminal->SessionData->SessionKey;
  5432. std::unique_ptr<TStrings> DataList(Configuration->LoadDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam));
  5433. int Files = -1;
  5434. if (DataList->Count >= 1)
  5435. {
  5436. Files = StrToIntDef(DataList->Strings[0], Files);
  5437. }
  5438. else
  5439. {
  5440. DataList->Add(UnicodeString());
  5441. }
  5442. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, Files);
  5443. FSynchronizeProgressForm->Start();
  5444. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5445. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5446. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5447. &SynchronizeOptions);
  5448. if (Terminal->SessionData->CacheDirectories)
  5449. {
  5450. DataList->Strings[0] = IntToStr(SynchronizeOptions.Files);
  5451. Configuration->SaveDirectoryStatisticsCache(SessionKey, RemoteDirectory, CopyParam, DataList.get());
  5452. }
  5453. }
  5454. __finally
  5455. {
  5456. FAutoOperation = false;
  5457. SAFE_DESTROY(FSynchronizeProgressForm);
  5458. }
  5459. TSynchronizeParams SynchronizeParams;
  5460. SynchronizeParams.LocalDirectory = LocalDirectory;
  5461. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5462. SynchronizeParams.Mode = Mode;
  5463. SynchronizeParams.CopyParam = &CopyParam;
  5464. SynchronizeParams.Params = Params;
  5465. SynchronizeParams.Checklist = Checklist;
  5466. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5467. SynchronizeParams.StartTime = &StartTime;
  5468. SynchronizeParams.OnProcessedItem = NULL;
  5469. Result = Checklist->Count;
  5470. if (Checklist->Count > 0)
  5471. {
  5472. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5473. {
  5474. if (!DoSynchronizeChecklistDialog(
  5475. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5476. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, DoSynchronizeBrowse, &SynchronizeParams))
  5477. {
  5478. Result = -1;
  5479. }
  5480. }
  5481. else
  5482. {
  5483. FullSynchronize(SynchronizeParams, NULL, NULL);
  5484. }
  5485. }
  5486. else
  5487. {
  5488. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5489. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5490. }
  5491. }
  5492. __finally
  5493. {
  5494. delete Checklist;
  5495. }
  5496. OperationComplete(StartTime);
  5497. }
  5498. else
  5499. {
  5500. Result = -1;
  5501. }
  5502. return Result;
  5503. }
  5504. //---------------------------------------------------------------------------
  5505. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5506. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  5507. bool & Continue, bool Collect, const TSynchronizeOptions * Options)
  5508. {
  5509. if (Collect)
  5510. {
  5511. DebugAssert(FSynchronizeProgressForm != NULL);
  5512. if (!FSynchronizeProgressForm->Started)
  5513. {
  5514. FSynchronizeProgressForm->Start();
  5515. }
  5516. int CompareProgress = FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Options->Files, Continue);
  5517. SetTaskbarListProgressValue(CompareProgress);
  5518. }
  5519. else
  5520. {
  5521. DebugAssert(FSynchronizeProgressForm == NULL);
  5522. DebugAssert(FProgressForm != NULL);
  5523. }
  5524. }
  5525. //---------------------------------------------------------------------------
  5526. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5527. {
  5528. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  5529. TRemoteFile * File = NULL;
  5530. Terminal->ReadFile(FullFileName, File);
  5531. if (File != NULL)
  5532. {
  5533. std::unique_ptr<TRemoteFile> FileOwner(File);
  5534. TAutoFlag Flag(FStandaloneEditing);
  5535. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5536. Application->ShowMainForm = false;
  5537. Application->Run();
  5538. }
  5539. }
  5540. //---------------------------------------------------------------------------
  5541. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  5542. {
  5543. DebugFail();
  5544. }
  5545. //---------------------------------------------------------------------------
  5546. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5547. {
  5548. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5549. SessionData->Assign(Terminal->SessionData);
  5550. UpdateSessionData(SessionData.get());
  5551. Terminal->UpdateSessionCredentials(SessionData.get());
  5552. if (!Terminal->SessionData->HasSessionName())
  5553. {
  5554. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5555. // once IsWorkspace is cleared
  5556. SessionData->Name = UnicodeString();
  5557. SessionData->IsWorkspace = false;
  5558. }
  5559. return SessionData.release();
  5560. }
  5561. //---------------------------------------------------------------------------
  5562. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5563. {
  5564. TSessionData * SessionData = CloneCurrentSessionData();
  5565. try
  5566. {
  5567. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5568. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5569. }
  5570. __finally
  5571. {
  5572. delete SessionData;
  5573. }
  5574. }
  5575. //---------------------------------------------------------------------------
  5576. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5577. {
  5578. TTerminalManager * Manager = TTerminalManager::Instance();
  5579. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5580. if (DebugAlwaysTrue(Terminal != NULL))
  5581. {
  5582. // Update (Managed)Terminal->StateData
  5583. UpdateTerminal(Terminal);
  5584. }
  5585. Manager->SaveWorkspace(DataList.get());
  5586. return DataList.release();
  5587. }
  5588. //---------------------------------------------------------------------------
  5589. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5590. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5591. {
  5592. WinConfiguration->LastWorkspace = Name;
  5593. if (!SavePasswords)
  5594. {
  5595. for (int Index = 0; Index < DataList->Count; Index++)
  5596. {
  5597. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5598. if (SessionData->Link.IsEmpty())
  5599. {
  5600. SessionData->ClearSessionPasswords();
  5601. }
  5602. }
  5603. }
  5604. StoredSessions->NewWorkspace(Name, DataList);
  5605. // modified only
  5606. StoredSessions->Save(false, Explicit);
  5607. }
  5608. //---------------------------------------------------------------------------
  5609. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5610. {
  5611. return
  5612. DefaultStr(WinConfiguration->LastWorkspace,
  5613. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5614. }
  5615. //---------------------------------------------------------------------------
  5616. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5617. {
  5618. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5619. bool AnyNonStoredSessionWithPassword = false;
  5620. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5621. bool AllNonStoredSessionsAnonymous = true;
  5622. TTerminalManager * Manager = TTerminalManager::Instance();
  5623. for (int Index = 0; Index < DataList->Count; Index++)
  5624. {
  5625. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5626. if (SessionData->Link.IsEmpty())
  5627. {
  5628. if (SessionData->HasAnySessionPassword())
  5629. {
  5630. AnyNonStoredSessionWithPassword = true;
  5631. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  5632. {
  5633. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5634. }
  5635. }
  5636. if (!SameText(SessionData->UserName, AnonymousUserName))
  5637. {
  5638. AllNonStoredSessionsAnonymous = false;
  5639. }
  5640. }
  5641. }
  5642. bool SavePasswords;
  5643. bool * PSavePasswords;
  5644. bool NotRecommendedSavingPasswords =
  5645. !CustomWinConfiguration->UseMasterPassword &&
  5646. !AllNonStoredSessionsAnonymous;
  5647. if (Configuration->DisablePasswordStoring ||
  5648. !AnyNonStoredSessionWithPassword)
  5649. {
  5650. PSavePasswords = NULL;
  5651. SavePasswords = false;
  5652. }
  5653. else
  5654. {
  5655. PSavePasswords = &SavePasswords;
  5656. SavePasswords =
  5657. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5658. !NotRecommendedSavingPasswords;
  5659. }
  5660. UnicodeString Name = WorkspaceName();
  5661. bool CreateShortcut = false;
  5662. bool Result =
  5663. DoSaveWorkspaceDialog(
  5664. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5665. EnableAutoSave);
  5666. if (Result)
  5667. {
  5668. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5669. if (CreateShortcut)
  5670. {
  5671. TOperationVisualizer Visualizer;
  5672. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5673. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5674. }
  5675. if (EnableAutoSave)
  5676. {
  5677. WinConfiguration->AutoSaveWorkspace = true;
  5678. WinConfiguration->AutoWorkspace = Name;
  5679. if (PSavePasswords != NULL)
  5680. {
  5681. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5682. }
  5683. }
  5684. }
  5685. return Result;
  5686. }
  5687. //---------------------------------------------------------------------------
  5688. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TManagedTerminal * Terminal)
  5689. {
  5690. DebugAssert(!IsLocalBrowserMode());
  5691. SAFE_DESTROY(Terminal->RemoteExplorerState);
  5692. if (WinConfiguration->PreservePanelState)
  5693. {
  5694. Terminal->RemoteExplorerState = RemoteDirView->SaveState();
  5695. }
  5696. UpdateSessionData(Terminal->StateData);
  5697. }
  5698. //---------------------------------------------------------------------------
  5699. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5700. {
  5701. // Keep in sync with TSessionData::CopyStateData
  5702. DebugAssert(!IsLocalBrowserMode());
  5703. DebugAssert(Data != NULL);
  5704. // This is inconsistent with how color (for example) is handled.
  5705. Data->Name = Terminal->SessionData->Name;
  5706. // cannot use RemoteDirView->Path, because it is empty if connection
  5707. // was already closed
  5708. // also only peek, we may not be connected at all atm
  5709. // so make sure we do not try retrieving current directory from the server
  5710. // (particularly with FTP)
  5711. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5712. if (!ACurrentDirectory.IsEmpty())
  5713. {
  5714. Data->RemoteDirectory = ACurrentDirectory;
  5715. }
  5716. Data->Color = SessionColor;
  5717. }
  5718. //---------------------------------------------------------------------------
  5719. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5720. {
  5721. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5722. DebugAssert(Toolbar != NULL);
  5723. for (int i = 0; i < Toolbar->Items->Count; i++)
  5724. {
  5725. TTBXCustomDropDownItem * DropDownItem;
  5726. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5727. if (DropDownItem != NULL)
  5728. {
  5729. ToolbarItemResize(DropDownItem,
  5730. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5731. Toolbar->NonClientWidth);
  5732. break;
  5733. }
  5734. }
  5735. }
  5736. //---------------------------------------------------------------------------
  5737. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5738. {
  5739. Item->EditWidth = Width;
  5740. }
  5741. //---------------------------------------------------------------------------
  5742. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5743. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5744. {
  5745. for (int i = 0; i < Toolbar->Items->Count; i++)
  5746. {
  5747. TTBXCustomDropDownItem * DropDownItem;
  5748. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5749. if (DropDownItem != NULL)
  5750. {
  5751. ASize.x -= DropDownItem->EditWidth;
  5752. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5753. }
  5754. }
  5755. }
  5756. //---------------------------------------------------------------------------
  5757. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5758. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5759. {
  5760. if (WinConfiguration->DDWarnLackOfTempSpace)
  5761. {
  5762. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5763. if (!ADrive.IsEmpty())
  5764. {
  5765. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5766. DebugAssert(RequiredSpace >= 0);
  5767. __int64 RequiredWithReserve;
  5768. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5769. if (FreeSpace < RequiredWithReserve)
  5770. {
  5771. unsigned int Result;
  5772. TMessageParams Params(mpNeverAskAgainCheck);
  5773. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5774. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5775. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5776. if (Result == qaNeverAskAgain)
  5777. {
  5778. WinConfiguration->DDWarnLackOfTempSpace = false;
  5779. }
  5780. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5781. }
  5782. }
  5783. }
  5784. }
  5785. //---------------------------------------------------------------------------
  5786. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5787. {
  5788. DoOpenDirectoryDialog(odAddBookmark, Side);
  5789. }
  5790. //---------------------------------------------------------------------------
  5791. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5792. {
  5793. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5794. TStringList * VisitedDirectories = CreateSortedStringList(!IsSideLocalBrowser(Side));
  5795. try
  5796. {
  5797. TCustomDirView * DView = DirView(Side);
  5798. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5799. {
  5800. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5801. }
  5802. }
  5803. catch (...)
  5804. {
  5805. delete VisitedDirectories;
  5806. throw;
  5807. }
  5808. return VisitedDirectories;
  5809. }
  5810. //---------------------------------------------------------------------------
  5811. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5812. TOpenDirectoryMode Mode, TOperationSide Side)
  5813. {
  5814. bool Continue = true;
  5815. if (Mode == odAddBookmark)
  5816. {
  5817. TMessageParams Params(mpNeverAskAgainCheck);
  5818. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5819. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5820. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  5821. unsigned int Answer =
  5822. MessageDialog(Message,
  5823. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  5824. if (Answer == qaNeverAskAgain)
  5825. {
  5826. Continue = true;
  5827. WinConfiguration->UseSharedBookmarks = true;
  5828. }
  5829. else if (Answer == qaOK)
  5830. {
  5831. Continue = true;
  5832. WinConfiguration->UseSharedBookmarks = false;
  5833. }
  5834. else
  5835. {
  5836. Continue = false;
  5837. }
  5838. }
  5839. if (Continue)
  5840. {
  5841. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  5842. try
  5843. {
  5844. UnicodeString Name = DirView(Side)->PathName;
  5845. bool Repeat;
  5846. do
  5847. {
  5848. Repeat = false;
  5849. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  5850. // do not allow switching to location profiles,
  5851. // if we are not connected
  5852. HasDirView[osLocal] && (Terminal != NULL)))
  5853. {
  5854. TWindowLock Lock(this);
  5855. if (!TryOpenDirectory(Side, Name))
  5856. {
  5857. Repeat = true;
  5858. Mode = odBrowse;
  5859. }
  5860. }
  5861. }
  5862. while (Repeat);
  5863. }
  5864. __finally
  5865. {
  5866. delete VisitedDirectories;
  5867. }
  5868. }
  5869. }
  5870. //---------------------------------------------------------------------------
  5871. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  5872. {
  5873. DebugAssert(!FTerminal->CommandSessionOpened);
  5874. return TTerminalManager::Instance()->ConnectTerminal(FTerminal->CommandSession);
  5875. }
  5876. //---------------------------------------------------------------------------
  5877. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  5878. {
  5879. bool Result = FTerminal->IsCapable[Capability] ||
  5880. FTerminal->CommandSessionOpened;
  5881. if (!Result)
  5882. {
  5883. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  5884. if (!GUIConfiguration->ConfirmCommandSession)
  5885. {
  5886. Result = true;
  5887. }
  5888. else
  5889. {
  5890. TMessageParams Params(mpNeverAskAgainCheck);
  5891. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  5892. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  5893. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  5894. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  5895. if (Answer == qaNeverAskAgain)
  5896. {
  5897. GUIConfiguration->ConfirmCommandSession = false;
  5898. Result = true;
  5899. }
  5900. else if (Answer == qaOK)
  5901. {
  5902. Result = true;
  5903. }
  5904. }
  5905. if (Result)
  5906. {
  5907. Result = CommandSessionFallback();
  5908. }
  5909. }
  5910. return Result;
  5911. }
  5912. //---------------------------------------------------------------------------
  5913. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  5914. {
  5915. if (EnsureCommandSessionFallback(fcAnyCommand))
  5916. {
  5917. DoConsoleDialog(Terminal, Command);
  5918. }
  5919. }
  5920. //---------------------------------------------------------------------------
  5921. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  5922. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  5923. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  5924. {
  5925. if (!IsSideLocalBrowser(osRemote) &&
  5926. IsFileControl(DropSourceControl, osRemote) &&
  5927. (FDDExtMapFile != NULL))
  5928. {
  5929. Accept = true;
  5930. }
  5931. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  5932. DebugAssert(FDDTargetControl != NULL);
  5933. }
  5934. //---------------------------------------------------------------------------
  5935. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  5936. _di_IDataObject DataObj, int KeyState,
  5937. const TPoint & Point, int & Effect, bool & Accept)
  5938. {
  5939. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  5940. }
  5941. //---------------------------------------------------------------------------
  5942. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  5943. _di_IDataObject DataObj, int KeyState,
  5944. const TPoint & Point, int & Effect, bool & Accept)
  5945. {
  5946. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  5947. }
  5948. //---------------------------------------------------------------------------
  5949. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  5950. TObject * Sender)
  5951. {
  5952. DebugUsedParam(Sender);
  5953. DebugAssert(FDDTargetControl == Sender);
  5954. FDDTargetControl = NULL;
  5955. }
  5956. //---------------------------------------------------------------------------
  5957. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  5958. {
  5959. FileControlDDDragLeave(SessionsPageControl);
  5960. }
  5961. //---------------------------------------------------------------------------
  5962. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  5963. {
  5964. FileControlDDDragLeave(QueueView3);
  5965. }
  5966. //---------------------------------------------------------------------------
  5967. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  5968. {
  5969. DebugAssert(!IsSideLocalBrowser(Side));
  5970. DebugUsedParam(Side);
  5971. bool Edit = false;
  5972. TRemoteFile * File = NULL;
  5973. UnicodeString FileName;
  5974. UnicodeString PointTo;
  5975. bool SymbolicLink = true;
  5976. if (RemoteDirView->ItemFocused)
  5977. {
  5978. DebugAssert(RemoteDirView->ItemFocused->Data);
  5979. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  5980. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  5981. if (Edit)
  5982. {
  5983. FileName = File->FileName;
  5984. PointTo = File->LinkTo;
  5985. }
  5986. else
  5987. {
  5988. PointTo = File->FileName;
  5989. }
  5990. }
  5991. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  5992. Terminal->IsCapable[fcHardLink]))
  5993. {
  5994. Configuration->Usage->Inc(L"RemoteLinksCreated");
  5995. if (Edit)
  5996. {
  5997. DebugAssert(File->FileName == FileName);
  5998. int Params = dfNoRecursive;
  5999. Terminal->ExceptionOnFail = true;
  6000. try
  6001. {
  6002. Terminal->DeleteFile(L"", File, &Params);
  6003. }
  6004. __finally
  6005. {
  6006. Terminal->ExceptionOnFail = false;
  6007. }
  6008. }
  6009. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  6010. }
  6011. }
  6012. //---------------------------------------------------------------------------
  6013. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  6014. {
  6015. return
  6016. (IsSideLocalBrowser(Side) ||
  6017. ((Terminal != NULL) &&
  6018. Terminal->ResolvingSymlinks &&
  6019. Terminal->IsCapable[fcSymbolicLink]));
  6020. }
  6021. //---------------------------------------------------------------------------
  6022. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  6023. {
  6024. return
  6025. !IsSideLocalBrowser(FCurrentSide) &&
  6026. (RemoteDirView->ItemFocused != NULL) &&
  6027. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  6028. Terminal->SessionData->ResolveSymlinks;
  6029. }
  6030. //---------------------------------------------------------------------------
  6031. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  6032. {
  6033. DebugAssert(!WinConfiguration->DisableOpenEdit);
  6034. FForceExecution = true;
  6035. try
  6036. {
  6037. DirView(osCurrent)->ExecuteCurrentFile();
  6038. }
  6039. __finally
  6040. {
  6041. FForceExecution = false;
  6042. }
  6043. }
  6044. //---------------------------------------------------------------------------
  6045. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  6046. {
  6047. TEditorData ExternalEditor;
  6048. ExternalEditor.Editor = edExternal;
  6049. bool Remember = false;
  6050. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc, !IsSideLocalBrowser(osCurrent)))
  6051. {
  6052. if (Remember)
  6053. {
  6054. TEditorList * EditorList = new TEditorList;
  6055. try
  6056. {
  6057. *EditorList = *WinConfiguration->EditorList;
  6058. bool Found = false;
  6059. int i = 0;
  6060. while (!Found && (i < EditorList->Count))
  6061. {
  6062. const TEditorPreferences * Editor = EditorList->Editors[i];
  6063. if ((Editor->Data->Editor == edExternal) &&
  6064. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  6065. {
  6066. Found = true;
  6067. }
  6068. i++;
  6069. }
  6070. if (!Found)
  6071. {
  6072. EditorList->Add(new TEditorPreferences(ExternalEditor));
  6073. WinConfiguration->EditorList = EditorList;
  6074. }
  6075. }
  6076. __finally
  6077. {
  6078. delete EditorList;
  6079. }
  6080. }
  6081. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  6082. }
  6083. }
  6084. //---------------------------------------------------------------------------
  6085. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  6086. {
  6087. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  6088. if (FFileFindTerminal == Sender)
  6089. {
  6090. FFileFindTerminal = NULL;
  6091. HideFileFindDialog();
  6092. }
  6093. if (FClipboardTerminal == Sender)
  6094. {
  6095. ClipboardClear(); // implies ClipboardStop
  6096. }
  6097. }
  6098. //---------------------------------------------------------------------------
  6099. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  6100. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  6101. {
  6102. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  6103. DebugAssert(Terminal != NULL);
  6104. if (Data->Terminal == Terminal)
  6105. {
  6106. Data->Terminal = NULL;
  6107. }
  6108. }
  6109. //---------------------------------------------------------------------------
  6110. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  6111. {
  6112. UpdateControls();
  6113. SessionColor = TColor(0);
  6114. UpdateRemotePathComboBox(false);
  6115. NeedSession(false);
  6116. }
  6117. //---------------------------------------------------------------------------
  6118. void __fastcall TCustomScpExplorerForm::NeedSession(bool Startup)
  6119. {
  6120. try
  6121. {
  6122. // Cache, as the login dialog can change its value
  6123. bool ShowLogin = WinConfiguration->ShowLoginWhenNoSession;
  6124. try
  6125. {
  6126. if (ShowLogin)
  6127. {
  6128. bool ReloadSessions = !Startup;
  6129. TTerminalManager::Instance()->NewSession(false, L"", ReloadSessions, this);
  6130. }
  6131. else if (Startup && WinConfiguration->AutoSaveWorkspace && !WinConfiguration->AutoWorkspace.IsEmpty() &&
  6132. // This detects if workspace was saved the last time the main widow was closed
  6133. SameText(WinConfiguration->LastStoredSession, WinConfiguration->AutoWorkspace))
  6134. {
  6135. DoOpenFolderOrWorkspace(WinConfiguration->AutoWorkspace, false);
  6136. Configuration->Usage->Inc(L"OpenedWorkspacesAuto");
  6137. }
  6138. }
  6139. __finally
  6140. {
  6141. // Do not terminate, if we are only starting up and we are not showing Login dialog
  6142. // (so there was no chance for the user to open any session yet)
  6143. if ((ShowLogin || !Startup) &&
  6144. !WinConfiguration->KeepOpenWhenNoSession &&
  6145. ((Terminal == NULL) || (!Terminal->Active && !Terminal->Permanent)))
  6146. {
  6147. TerminateApplication();
  6148. }
  6149. }
  6150. }
  6151. catch (EAbort &)
  6152. {
  6153. // swallow
  6154. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  6155. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  6156. }
  6157. }
  6158. //---------------------------------------------------------------------------
  6159. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged()
  6160. {
  6161. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  6162. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6163. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  6164. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  6165. try
  6166. {
  6167. while ((SessionsPageControl->PageCount > TerminalList->Count + 1) ||
  6168. // Clear the design time unthemed tab
  6169. ((SessionsPageControl->PageCount > 0) &&
  6170. (dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[SessionsPageControl->PageCount - 1]) == NULL)))
  6171. {
  6172. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6173. }
  6174. for (int Index = 0; Index <= TerminalList->Count; Index++)
  6175. {
  6176. TThemeTabSheet * TabSheet;
  6177. if (Index >= SessionsPageControl->PageCount)
  6178. {
  6179. TabSheet = new TThemeTabSheet(SessionsPageControl);
  6180. TabSheet->PageControl = SessionsPageControl;
  6181. }
  6182. else
  6183. {
  6184. TabSheet = DebugNotNull(dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[Index]));
  6185. }
  6186. bool IsSessionTab = (Index < TerminalList->Count);
  6187. if (IsSessionTab)
  6188. {
  6189. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  6190. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  6191. UpdateSessionTab(TabSheet);
  6192. }
  6193. else
  6194. {
  6195. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  6196. TabSheet->Tag = 0; // not really needed
  6197. TabSheet->Shadowed = false;
  6198. // We know that we are at the last page, otherwise we could not call this (it assumes that new session tab is the last one)
  6199. UpdateNewSessionTab();
  6200. }
  6201. TabSheet->ShowCloseButton = IsSessionTab;
  6202. }
  6203. }
  6204. __finally
  6205. {
  6206. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6207. }
  6208. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  6209. }
  6210. //---------------------------------------------------------------------------
  6211. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  6212. {
  6213. DoTerminalListChanged();
  6214. }
  6215. //---------------------------------------------------------------------------
  6216. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  6217. {
  6218. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6219. DebugAssert(TabSheet->ImageIndex == 0);
  6220. TabSheet->Caption =
  6221. WinConfiguration->SelectiveToolbarText ?
  6222. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  6223. UnicodeString();
  6224. }
  6225. //---------------------------------------------------------------------------
  6226. TManagedTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  6227. {
  6228. return reinterpret_cast<TManagedTerminal *>(TabSheet->Tag);
  6229. }
  6230. //---------------------------------------------------------------------------
  6231. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  6232. {
  6233. if (DebugAlwaysTrue(TabSheet != NULL))
  6234. {
  6235. TManagedTerminal * ATerminal = GetSessionTabTerminal(TabSheet);
  6236. if (DebugAlwaysTrue(ATerminal != NULL))
  6237. {
  6238. TColor Color = (ATerminal == FTerminal) ? FSessionColor : ATerminal->StateData->Color;
  6239. TabSheet->ImageIndex = AddSessionColor(Color);
  6240. UnicodeString TabCaption = TTerminalManager::Instance()->GetTerminalTitle(ATerminal, true);
  6241. TabSheet->Caption = SessionsPageControl->FormatCaptionWithCloseButton(TabCaption);
  6242. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  6243. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  6244. {
  6245. ThemeTabSheet->Shadowed = !ATerminal->Active;
  6246. }
  6247. }
  6248. }
  6249. }
  6250. //---------------------------------------------------------------------------
  6251. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6252. {
  6253. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6254. TManagedTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  6255. bool Result = (Terminal != NULL);
  6256. if (Result)
  6257. {
  6258. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6259. }
  6260. else
  6261. {
  6262. try
  6263. {
  6264. NewSession(false);
  6265. }
  6266. __finally
  6267. {
  6268. DoTerminalListChanged();
  6269. }
  6270. FSessionsPageControlNewSessionTime = Now();
  6271. }
  6272. return Result;
  6273. }
  6274. //---------------------------------------------------------------------------
  6275. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6276. {
  6277. SessionTabSwitched();
  6278. }
  6279. //---------------------------------------------------------------------------
  6280. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6281. {
  6282. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6283. DebugAssert(TransferList != NULL);
  6284. UnicodeString Name;
  6285. if (TransferList->ItemIndex <= 0)
  6286. {
  6287. Name = L"";
  6288. }
  6289. else
  6290. {
  6291. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6292. }
  6293. if (FCopyParamAutoSelected.IsEmpty())
  6294. {
  6295. // if previous preset was not autoselected, make new preset the "default"
  6296. FCopyParamDefault = Name;
  6297. }
  6298. GUIConfiguration->CopyParamCurrent = Name;
  6299. }
  6300. //---------------------------------------------------------------------------
  6301. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6302. {
  6303. // sanity check
  6304. bool ExistingPreset =
  6305. (FTransferListHoverIndex >= 0) &&
  6306. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6307. DebugAssert(ExistingPreset);
  6308. if (ExistingPreset)
  6309. {
  6310. HDC DC = GetDC(0);
  6311. TCanvas * Canvas = new TCanvas();
  6312. try
  6313. {
  6314. Canvas->Handle = DC;
  6315. Canvas->Font = GetToolbarFont(this);
  6316. UnicodeString Name;
  6317. if (FTransferListHoverIndex == 0)
  6318. {
  6319. Name = L"";
  6320. }
  6321. else
  6322. {
  6323. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6324. }
  6325. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6326. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6327. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6328. static_cast<TObject*>(GetComponent(fcTransferList)));
  6329. UnicodeString InfoStr =
  6330. GUIConfiguration->CopyParamPreset[Name].
  6331. GetInfoStr(L"; ",
  6332. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General));
  6333. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6334. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6335. {
  6336. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6337. {
  6338. InfoStr.SetLength(InfoStr.Length() - 1);
  6339. }
  6340. InfoStr += Ellipsis;
  6341. }
  6342. // UpdateCaption does not cause invalidation of whole submenu, while
  6343. // setting Caption property does.
  6344. // also it probably does not resize the label, even if necessary
  6345. // (we do not want that anyway)
  6346. TransferLabel->UpdateCaption(InfoStr);
  6347. }
  6348. __finally
  6349. {
  6350. Canvas->Handle = NULL;
  6351. ReleaseDC(0, DC);
  6352. delete Canvas;
  6353. }
  6354. }
  6355. }
  6356. //---------------------------------------------------------------------------
  6357. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6358. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6359. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6360. {
  6361. if (FTransferListHoverIndex != AHoverIndex)
  6362. {
  6363. FTransferListHoverIndex = AHoverIndex;
  6364. UpdateTransferLabel();
  6365. }
  6366. }
  6367. //---------------------------------------------------------------------------
  6368. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6369. {
  6370. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6371. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6372. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6373. {
  6374. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6375. {
  6376. OpenDirectory(GetSide(osCurrent));
  6377. Message.Result = 1;
  6378. }
  6379. else
  6380. {
  6381. TForm::Dispatch(&Message);
  6382. }
  6383. }
  6384. else
  6385. {
  6386. TForm::Dispatch(&Message);
  6387. }
  6388. }
  6389. //---------------------------------------------------------------------------
  6390. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6391. {
  6392. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6393. if ((FIgnoreNextDialogChar != 0) &&
  6394. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6395. {
  6396. Message.Result = 1;
  6397. }
  6398. else
  6399. {
  6400. TForm::Dispatch(&Message);
  6401. }
  6402. FIgnoreNextDialogChar = 0;
  6403. }
  6404. //---------------------------------------------------------------------------
  6405. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6406. {
  6407. // The four low-order bits are used internally by Windows
  6408. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6409. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6410. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6411. // 61730 - restore thru double click - undocumented
  6412. // 61490 - maximize thru double click - undocumented
  6413. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6414. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6415. {
  6416. SysResizing(Cmd);
  6417. }
  6418. TForm::Dispatch(&Message);
  6419. }
  6420. //---------------------------------------------------------------------------
  6421. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6422. {
  6423. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6424. // Also there no point returning TRUE as we are not able to
  6425. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6426. // Hence the process termination might be safer :)
  6427. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6428. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6429. {
  6430. Message.Result = FALSE;
  6431. }
  6432. else
  6433. {
  6434. Message.Result = TRUE;
  6435. }
  6436. // Do not call default handling as that triggers OnCloseQuery,
  6437. // where our implementation will popup confirmation dialogs, what we do not want,
  6438. // as per Vista guidelines:
  6439. // https://docs.microsoft.com/en-us/windows/win32/shutdown/shutting-down
  6440. }
  6441. //---------------------------------------------------------------------------
  6442. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6443. {
  6444. if (Message.EndSession && IsApplicationMinimized())
  6445. {
  6446. // WORKAROUND
  6447. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6448. // when it receives WM_ENDSESSION,
  6449. // but that sometimes (particularly when application is minimized) cause crashes.
  6450. // Crash popups message that blocks log off.
  6451. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6452. // so we call ExitProcess() immediately, not even trying to cleanup.
  6453. // It still causes beep, so there's likely some popup, but it does not block
  6454. // log off.
  6455. ExitProcess(0);
  6456. }
  6457. TForm::Dispatch(&Message);
  6458. }
  6459. #ifdef _DEBUG
  6460. //---------------------------------------------------------------------------
  6461. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6462. {
  6463. TForm::Dispatch(&Message);
  6464. }
  6465. #endif
  6466. //---------------------------------------------------------------------------
  6467. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6468. {
  6469. }
  6470. //---------------------------------------------------------------------------
  6471. void __fastcall TCustomScpExplorerForm::DoShow()
  6472. {
  6473. // only now are the controls resized finally, so the size constraints
  6474. // will not conflict with possibly very small window size
  6475. RestoreFormParams();
  6476. // This indicates that the form was rescaled after parameters were loaded and
  6477. // as most probably the parameters were actually saved with the current form scaling
  6478. // (windows was the last time likely closed on the same monitor, where it is starting now),
  6479. // rescaling happened twice already. Reload the parameters, as now they likely won't need any scaling
  6480. // and no rounding problems will occur.
  6481. // See also RestoreForm.
  6482. // (we should be safe to call RestoreParams unconditionally)
  6483. if (PixelsPerInch != Screen->PixelsPerInch)
  6484. {
  6485. RestoreParams();
  6486. }
  6487. FixControlsPlacement();
  6488. if (Position == poDefaultPosOnly)
  6489. {
  6490. CutFormToDesktop(this);
  6491. }
  6492. TForm::DoShow();
  6493. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6494. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6495. if (Terminal == NULL)
  6496. {
  6497. StartingDisconnected();
  6498. }
  6499. UpdatePixelsPerInchMainWindowCounter();
  6500. FShowing = true;
  6501. }
  6502. //---------------------------------------------------------------------------
  6503. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6504. {
  6505. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6506. }
  6507. //---------------------------------------------------------------------------
  6508. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  6509. {
  6510. DoTerminalListChanged();
  6511. }
  6512. //---------------------------------------------------------------------------
  6513. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6514. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6515. TNotifyEvent OnBalloonClick, TObject * UserData)
  6516. {
  6517. bool Do;
  6518. UnicodeString Message;
  6519. if (E == NULL)
  6520. {
  6521. Message = Str;
  6522. Do = true;
  6523. }
  6524. else
  6525. {
  6526. Do = ExceptionMessage(E, Message);
  6527. }
  6528. if (Do && WinConfiguration->BalloonNotifications)
  6529. {
  6530. UnicodeString Title;
  6531. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6532. {
  6533. Message = TrimLeft(Message);
  6534. }
  6535. else
  6536. {
  6537. Message = UnformatMessage(Message);
  6538. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6539. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6540. Title = LoadResourceString(Captions[Type]);
  6541. if (Terminal != NULL)
  6542. {
  6543. Title = FORMAT(L"%s - %s",
  6544. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), Title));
  6545. }
  6546. }
  6547. if (Seconds == 0)
  6548. {
  6549. Seconds = WinConfiguration->NotificationsTimeout;
  6550. }
  6551. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6552. }
  6553. }
  6554. //---------------------------------------------------------------------------
  6555. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6556. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6557. UnicodeString HelpKeyword, const TMessageParams * Params,
  6558. TTerminal * Terminal)
  6559. {
  6560. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6561. (FOnFeedSynchronizeError != NULL)) &&
  6562. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6563. {
  6564. if (FOnFeedSynchronizeError != NULL)
  6565. {
  6566. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6567. }
  6568. else
  6569. {
  6570. DebugAssert(FErrorList != NULL);
  6571. TStringList * MoreMessagesCopy = NULL;
  6572. if (MoreMessages)
  6573. {
  6574. MoreMessagesCopy = new TStringList();
  6575. MoreMessagesCopy->Assign(MoreMessages);
  6576. }
  6577. FErrorList->AddObject(Message, MoreMessagesCopy);
  6578. }
  6579. PopupTrayBalloon(Terminal, Message, Type);
  6580. return ContinueAnswer(Answers);
  6581. }
  6582. else
  6583. {
  6584. bool UseBalloon = FTrayIcon->Visible;
  6585. if (UseBalloon)
  6586. {
  6587. PopupTrayBalloon(Terminal, Message, Type);
  6588. }
  6589. unsigned int Result;
  6590. try
  6591. {
  6592. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6593. }
  6594. __finally
  6595. {
  6596. // cancel only balloon we popped up, otherwise we may cancel notification
  6597. // balloon that was there before the message dialog
  6598. // (such as "dd confirmation opt in balloon)
  6599. if (UseBalloon)
  6600. {
  6601. FTrayIcon->CancelBalloon();
  6602. }
  6603. }
  6604. return Result;
  6605. }
  6606. }
  6607. //---------------------------------------------------------------------------
  6608. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6609. TTerminal * Terminal, Exception * E)
  6610. {
  6611. if (FTrayIcon->Visible)
  6612. {
  6613. PopupTrayBalloon(Terminal, L"", qtError, E);
  6614. }
  6615. // particularly prevent opening new session from jump list,
  6616. // while exception is shown
  6617. NonVisualDataModule->StartBusy();
  6618. try
  6619. {
  6620. ShowExtendedExceptionEx(Terminal, E);
  6621. }
  6622. __finally
  6623. {
  6624. NonVisualDataModule->EndBusy();
  6625. FTrayIcon->CancelBalloon();
  6626. }
  6627. }
  6628. //---------------------------------------------------------------------------
  6629. void __fastcall TCustomScpExplorerForm::TerminalReady()
  6630. {
  6631. if (Terminal->Active)
  6632. {
  6633. InitStatusBar();
  6634. }
  6635. // cannot rely on active page being page for active terminal,
  6636. // as it can happen that active page is the "new session" page
  6637. // (e.g. when reconnecting active terminal, while login dialog
  6638. // invoked from "new session" page is modal)
  6639. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6640. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  6641. }
  6642. //---------------------------------------------------------------------------
  6643. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6644. TTerminal * Terminal, Exception * E)
  6645. {
  6646. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6647. if (!Terminal->Active)
  6648. {
  6649. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6650. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6651. {
  6652. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6653. }
  6654. }
  6655. }
  6656. //---------------------------------------------------------------------------
  6657. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6658. UnicodeString Message, TQueryType Type,
  6659. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6660. {
  6661. if ((E == NULL) ||
  6662. ExceptionMessage(E, Message))
  6663. {
  6664. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6665. if (Important)
  6666. {
  6667. Seconds *= 5;
  6668. }
  6669. UnicodeString NoteMessage(UnformatMessage(Message));
  6670. if (Terminal != NULL)
  6671. {
  6672. NoteMessage = FORMAT(L"%s: %s",
  6673. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), NoteMessage));
  6674. }
  6675. if (WinConfiguration->BalloonNotifications)
  6676. {
  6677. AddNote(NoteMessage);
  6678. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6679. }
  6680. else
  6681. {
  6682. FlashOnBackground();
  6683. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6684. }
  6685. }
  6686. }
  6687. //---------------------------------------------------------------------------
  6688. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6689. {
  6690. RestoreApp();
  6691. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6692. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6693. !NonVisualDataModule->Busy)
  6694. {
  6695. TManagedTerminal * Terminal = TerminalNoteData->Terminal;
  6696. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6697. if (!ComponentVisible[fcQueueView])
  6698. {
  6699. ToggleQueueVisibility();
  6700. GoToQueue();
  6701. }
  6702. }
  6703. }
  6704. //---------------------------------------------------------------------------
  6705. void __fastcall TCustomScpExplorerForm::QueueEvent(TManagedTerminal * ATerminal,
  6706. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6707. {
  6708. UnicodeString Message;
  6709. TNotifyEvent OnClick = NULL;
  6710. TObject * UserData = NULL;
  6711. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6712. switch (Event)
  6713. {
  6714. case qeEmptyButMonitored:
  6715. if ((ATerminal != Terminal) || QueueInvisible)
  6716. {
  6717. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6718. OnClick = QueueEmptyNoteClicked;
  6719. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6720. TerminalNoteData->Terminal = ATerminal;
  6721. UserData = TerminalNoteData;
  6722. }
  6723. break;
  6724. case qeEmpty:
  6725. OperationComplete(ATerminal->QueueOperationStart);
  6726. break;
  6727. case qePendingUserAction:
  6728. if ((ATerminal != Terminal) ||
  6729. (QueueInvisible && !IsQueueAutoPopup()))
  6730. {
  6731. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6732. }
  6733. break;
  6734. default:
  6735. DebugFail();
  6736. }
  6737. if (!Message.IsEmpty())
  6738. {
  6739. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6740. }
  6741. }
  6742. //---------------------------------------------------------------------------
  6743. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6744. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6745. {
  6746. if (FDDExtMapFile != NULL)
  6747. {
  6748. CloseHandle(FDDExtMapFile);
  6749. FDDExtMapFile = NULL;
  6750. }
  6751. if (WinConfiguration->DDFakeFile)
  6752. {
  6753. DDFakeFileInitDrag(FileList, Created);
  6754. }
  6755. }
  6756. //---------------------------------------------------------------------------
  6757. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  6758. {
  6759. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  6760. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  6761. {
  6762. FFakeFileDropTarget = FileName;
  6763. }
  6764. }
  6765. //---------------------------------------------------------------------------
  6766. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  6767. {
  6768. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6769. if (!ForceDirectories(ApiPath(Result)))
  6770. {
  6771. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  6772. }
  6773. FileSetAttr(ApiPath(Result), faHidden);
  6774. return Result;
  6775. }
  6776. //---------------------------------------------------------------------------
  6777. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  6778. bool & Created)
  6779. {
  6780. FFakeFileDropTarget = UnicodeString();
  6781. FDragFakeDirectory = CreateFakeTransferDirectory();
  6782. FileList->AddItem(NULL, FDragFakeDirectory);
  6783. Created = true;
  6784. if (!WinConfiguration->IsDDExtRunning() || WinConfiguration->IsDDExtBroken())
  6785. {
  6786. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  6787. }
  6788. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6789. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6790. {
  6791. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6792. TDragExtCommStruct* CommStruct;
  6793. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6794. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6795. DebugAssert(CommStruct != NULL);
  6796. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  6797. CommStruct->Dragging = true;
  6798. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  6799. LENOF(CommStruct->DropDest));
  6800. NULL_TERMINATE(CommStruct->DropDest);
  6801. UnmapViewOfFile(CommStruct);
  6802. }
  6803. }
  6804. //---------------------------------------------------------------------------
  6805. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  6806. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  6807. {
  6808. bool Result;
  6809. DebugAssert(!IsLocalBrowserMode());
  6810. if (Sender == RemoteDirView)
  6811. {
  6812. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  6813. }
  6814. else
  6815. {
  6816. DebugAssert(Sender == RemoteDriveView);
  6817. TStrings * FileList = RemoteDriveView->DragFileList();
  6818. try
  6819. {
  6820. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  6821. }
  6822. __finally
  6823. {
  6824. delete FileList;
  6825. }
  6826. }
  6827. if (FDDTargetControl == RemoteDriveView)
  6828. {
  6829. RemoteDriveView->UpdateDropTarget();
  6830. }
  6831. // foRemoteMove happens when file/dir is dragged within the remote tree view
  6832. // or from tree view to dir view.
  6833. // foMove happens when file/dir is dragged from remote tree view outside of
  6834. // application via dragex
  6835. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  6836. (DropSourceControl == RemoteDriveView))
  6837. {
  6838. RemoteDriveView->UpdateDropSource();
  6839. }
  6840. return Result;
  6841. }
  6842. //---------------------------------------------------------------------------
  6843. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  6844. {
  6845. DebugAssert(!IsLocalBrowserMode());
  6846. // This also handles drops of remote files to queue.
  6847. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  6848. SAFE_DESTROY(FDDFileList);
  6849. if (!FFakeFileDropTarget.IsEmpty())
  6850. {
  6851. RemoveDir(ApiPath(FFakeFileDropTarget));
  6852. }
  6853. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  6854. {
  6855. try
  6856. {
  6857. TDragResult DDResult = (Sender == RemoteDirView) ?
  6858. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  6859. // Focus is moved to the target application,
  6860. // but as we are going to present the UI, we need to steal the focus back.
  6861. // This most likely won't work though (windows does not allow application
  6862. // to steal focus most of the time)
  6863. Application->BringToFront();
  6864. // On older version of Windows we never got drMove here, see also comment below.
  6865. // On Windows 10, we get the "move".
  6866. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  6867. {
  6868. UnicodeString TargetDirectory;
  6869. TFileOperation Operation;
  6870. // drInvalid may mean drMove, see comment below
  6871. switch (DDResult)
  6872. {
  6873. case drCopy:
  6874. Operation = foCopy;
  6875. break;
  6876. case drMove:
  6877. Operation = foMove;
  6878. break;
  6879. default:
  6880. DebugFail();
  6881. case drInvalid:
  6882. // prefer "copy" for safety
  6883. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  6884. break;
  6885. }
  6886. TTransferOperationParam Param;
  6887. UnicodeString CounterName;
  6888. bool ForceQueue;
  6889. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  6890. {
  6891. // we get drInvalid both if d&d was intercepted by ddext,
  6892. // and when users drops on no-drop location.
  6893. // we tell the difference by existence of response from ddext,
  6894. // so we ignore absence of response in this case
  6895. if (DDResult != drInvalid)
  6896. {
  6897. // here we know that the extension is installed,
  6898. // as it is checked as soon as drag&drop starts from
  6899. // RemoteFileControlDDCreateDragFileList
  6900. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  6901. if (!WinConfiguration->DDExtInstalled || WinConfiguration->IsDDExtBroken())
  6902. {
  6903. throw ExtException(NULL, LoadStr(DD_TARGET_UNKNOWN), HELP_DD_TARGET_UNKNOWN);
  6904. }
  6905. else
  6906. {
  6907. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  6908. }
  6909. }
  6910. }
  6911. else
  6912. {
  6913. // download using fake file
  6914. Param.Temp = false;
  6915. Param.DragDrop = true;
  6916. if (ForceQueue)
  6917. {
  6918. Param.Queue = asOn;
  6919. }
  6920. if (Sender == RemoteDirView)
  6921. {
  6922. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  6923. }
  6924. if (RemoteFileControlFileOperation(Sender, Operation,
  6925. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  6926. {
  6927. Configuration->Usage->Inc(CounterName);
  6928. }
  6929. }
  6930. }
  6931. }
  6932. __finally
  6933. {
  6934. CloseHandle(FDDExtMapFile);
  6935. FDDExtMapFile = NULL;
  6936. delete FDragFakeMonitors;
  6937. FDragFakeMonitors = NULL;
  6938. RemoveDir(ApiPath(FDragFakeDirectory));
  6939. FDragFakeDirectory = L"";
  6940. FFakeFileDropTarget = UnicodeString();
  6941. }
  6942. }
  6943. if (FDragDropSshTerminate.get() != NULL)
  6944. {
  6945. FDragDropSshTerminate->Rethrow();
  6946. }
  6947. }
  6948. //---------------------------------------------------------------------------
  6949. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  6950. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  6951. {
  6952. // Remember drop effect so we know (when user drops files), if we copy or move
  6953. FLastDropEffect = dwEffect;
  6954. }
  6955. //---------------------------------------------------------------------------
  6956. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  6957. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  6958. {
  6959. bool Result;
  6960. if (FDDTargetControl == QueueView3)
  6961. {
  6962. Directory = DefaultDownloadTargetDirectory();
  6963. Result = true;
  6964. CounterName = L"DownloadsDragDropQueue";
  6965. ForceQueue = true;
  6966. }
  6967. else if (!FFakeFileDropTarget.IsEmpty())
  6968. {
  6969. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  6970. Result = true;
  6971. ForceQueue = false;
  6972. CounterName = L"DownloadsDragDropFakeFile";
  6973. }
  6974. else
  6975. {
  6976. ForceQueue = false;
  6977. Enabled = false;
  6978. try
  6979. {
  6980. int Timer = 0;
  6981. Result = false;
  6982. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  6983. {
  6984. {
  6985. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6986. TDragExtCommStruct* CommStruct;
  6987. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6988. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6989. DebugAssert(CommStruct != NULL);
  6990. Result = !CommStruct->Dragging;
  6991. if (Result)
  6992. {
  6993. Directory = ExtractFilePath(CommStruct->DropDest);
  6994. CounterName = L"DownloadsDragDropExternalExt";
  6995. }
  6996. UnmapViewOfFile(CommStruct);
  6997. }
  6998. if (!Result)
  6999. {
  7000. Sleep(50);
  7001. Timer += 50;
  7002. Application->ProcessMessages();
  7003. }
  7004. }
  7005. }
  7006. __finally
  7007. {
  7008. Enabled = true;
  7009. }
  7010. }
  7011. return Result;
  7012. }
  7013. //---------------------------------------------------------------------------
  7014. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  7015. const UnicodeString TempDir, int SecDelay)
  7016. {
  7017. TDateTime Alarm = Now() + (double)((double)SecDelay*OneMillisecond);
  7018. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  7019. if (FDelayedDeletionTimer == NULL)
  7020. {
  7021. DebugAssert(HandleAllocated());
  7022. FDelayedDeletionTimer = new TTimer(this);
  7023. FDelayedDeletionTimer->Interval = 10000;
  7024. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  7025. }
  7026. else
  7027. {
  7028. FDelayedDeletionTimer->Enabled = true;
  7029. }
  7030. }
  7031. //---------------------------------------------------------------------------
  7032. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  7033. {
  7034. DebugAssert(FDelayedDeletionList != NULL);
  7035. TDateTime N = Now();
  7036. TDateTime Alert;
  7037. UnicodeString Directory;
  7038. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  7039. {
  7040. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  7041. if ((N >= Alert) || (Sender == NULL))
  7042. {
  7043. Directory = FDelayedDeletionList->Strings[Index];
  7044. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  7045. {
  7046. FDelayedDeletionList->Delete(Index);
  7047. }
  7048. }
  7049. }
  7050. if (FDelayedDeletionList->Count == 0)
  7051. {
  7052. FDelayedDeletionTimer->Enabled = false;
  7053. }
  7054. }
  7055. //---------------------------------------------------------------------------
  7056. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  7057. {
  7058. DebugAssert(!IsLocalBrowserMode());
  7059. if (IsFileControl(FDDTargetControl, osRemote) ||
  7060. (FDDTargetControl == SessionsPageControl))
  7061. {
  7062. TTerminal * TargetTerminal = NULL;
  7063. TFileOperation Operation = foNone;
  7064. if (FDDTargetControl == SessionsPageControl)
  7065. {
  7066. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  7067. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  7068. // do not allow dropping on the "+" tab
  7069. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  7070. if (TargetTerminal != NULL)
  7071. {
  7072. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  7073. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  7074. {
  7075. Operation = foRemoteMove;
  7076. }
  7077. else
  7078. {
  7079. Operation = foRemoteCopy;
  7080. }
  7081. }
  7082. }
  7083. else
  7084. {
  7085. if (FLastDropEffect == DROPEFFECT_MOVE)
  7086. {
  7087. Operation = foRemoteMove;
  7088. }
  7089. else if (FLastDropEffect == DROPEFFECT_COPY)
  7090. {
  7091. Operation = foRemoteCopy;
  7092. }
  7093. }
  7094. if (Operation != foNone)
  7095. {
  7096. RemoteFileControlFileOperation(DropSourceControl,
  7097. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  7098. }
  7099. // abort drag&drop
  7100. Abort();
  7101. }
  7102. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  7103. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  7104. (FDDTargetControl != QueueView3))
  7105. {
  7106. DebugAssert(!FDragTempDir.IsEmpty());
  7107. TTransferType Type;
  7108. UnicodeString TempDir = FDragTempDir;
  7109. // We clear FDragTempDir before calling
  7110. // just in case it fail (raises exception)
  7111. FDragTempDir = L"";
  7112. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  7113. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  7114. // empty directory parameter means temp directory -> don't display it!
  7115. UnicodeString TargetDir = L"";
  7116. int Options = 0;
  7117. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  7118. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  7119. {
  7120. Abort();
  7121. }
  7122. // TargetDir is set when dropped on local file control
  7123. // (this was workaround for legacy dirview event handling, now it should be
  7124. // made prettier)
  7125. if (TargetDir.IsEmpty())
  7126. {
  7127. TargetDir = TempDir;
  7128. if (ForceDirectories(ApiPath(TargetDir)))
  7129. {
  7130. DebugAssert(Terminal && !TargetDir.IsEmpty());
  7131. FPendingTempSpaceWarn = true;
  7132. try
  7133. {
  7134. FDragDropOperation = true;
  7135. int Params = cpTemporary |
  7136. (Type == ttMove ? cpDelete : 0);
  7137. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  7138. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  7139. }
  7140. __finally
  7141. {
  7142. FDragDropOperation = false;
  7143. FPendingTempSpaceWarn = false;
  7144. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  7145. }
  7146. }
  7147. else
  7148. {
  7149. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  7150. }
  7151. }
  7152. }
  7153. }
  7154. //---------------------------------------------------------------------------
  7155. void __fastcall TCustomScpExplorerForm::DDDownload(
  7156. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  7157. {
  7158. DebugAssert(!IsLocalBrowserMode());
  7159. TAutoBatch AutoBatch(this);
  7160. UpdateCopyParamCounters(*CopyParam);
  7161. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  7162. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  7163. {
  7164. RemoteDriveView->UpdateDropSource();
  7165. }
  7166. }
  7167. //---------------------------------------------------------------------------
  7168. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  7169. {
  7170. public:
  7171. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  7172. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  7173. {
  7174. }
  7175. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  7176. {
  7177. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  7178. TDataObjectFilesEx::AllowData(FormatEtc);
  7179. }
  7180. };
  7181. //---------------------------------------------------------------------------
  7182. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  7183. TObject * Sender, TDataObject *& DataObject)
  7184. {
  7185. if (FDDExtMapFile != NULL)
  7186. {
  7187. TFileList * FileList = DragDropFiles(Sender)->FileList;
  7188. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  7189. {
  7190. Abort();
  7191. }
  7192. if (FileList->Count > 0)
  7193. {
  7194. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  7195. if (!FilesObject->IsValid(true, true))
  7196. {
  7197. FilesObject->_Release();
  7198. }
  7199. else
  7200. {
  7201. DataObject = FilesObject;
  7202. }
  7203. }
  7204. }
  7205. }
  7206. //---------------------------------------------------------------------------
  7207. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  7208. {
  7209. DebugFail();
  7210. }
  7211. //---------------------------------------------------------------------------
  7212. void __fastcall TCustomScpExplorerForm::GoToTree()
  7213. {
  7214. ComponentVisible[fcRemoteTree] = true;
  7215. DriveView(osRemote)->SetFocus();
  7216. }
  7217. //---------------------------------------------------------------------------
  7218. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7219. TPanelExport Export)
  7220. {
  7221. TCustomDirView * DirView = this->DirView(Side);
  7222. std::unique_ptr<TStrings> ExportData(new TStringList());
  7223. switch (Export)
  7224. {
  7225. case pePath:
  7226. ExportData->Add(DirView->PathName);
  7227. break;
  7228. case peFileList:
  7229. case peFullFileList:
  7230. {
  7231. bool FullPath = (Export == peFullFileList);
  7232. DirView->CreateFileList(false, FullPath, ExportData.get());
  7233. for (int Index = 0; Index < ExportData->Count; Index++)
  7234. {
  7235. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7236. {
  7237. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7238. }
  7239. }
  7240. }
  7241. break;
  7242. default:
  7243. DebugFail();
  7244. }
  7245. return ExportData.release();
  7246. }
  7247. //---------------------------------------------------------------------------
  7248. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7249. TPanelExport Export, TPanelExportDestination Destination)
  7250. {
  7251. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7252. PanelExportStore(Side, Export, Destination, ExportData.get());
  7253. }
  7254. //---------------------------------------------------------------------------
  7255. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7256. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7257. TStrings * ExportData)
  7258. {
  7259. if (Destination == pedClipboard)
  7260. {
  7261. TInstantOperationVisualizer Visualizer;
  7262. CopyToClipboard(ExportData);
  7263. }
  7264. else
  7265. {
  7266. DebugFail();
  7267. }
  7268. }
  7269. //---------------------------------------------------------------------------
  7270. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7271. {
  7272. TCustomDirView * DirView = this->DirView(Side);
  7273. UnicodeString Mask = DirView->Mask;
  7274. if (DoFilterMaskDialog(DirView, Mask))
  7275. {
  7276. DirView->Mask = TFileMasks::NormalizeMask(Mask);
  7277. Configuration->Usage->Inc(L"Filters");
  7278. }
  7279. }
  7280. //---------------------------------------------------------------------------
  7281. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7282. {
  7283. return FQueueController->DefaultOperation();
  7284. }
  7285. //---------------------------------------------------------------------------
  7286. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7287. TQueueOperation Operation, void ** Param)
  7288. {
  7289. switch (Operation)
  7290. {
  7291. case qoPreferences:
  7292. return true;
  7293. case qoGoTo:
  7294. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7295. case qoOnceEmpty:
  7296. return !FQueueController->Empty;
  7297. default:
  7298. return FQueueController->AllowOperation(Operation, Param);
  7299. }
  7300. }
  7301. //---------------------------------------------------------------------------
  7302. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7303. {
  7304. if (DebugAlwaysTrue(QueueView3->Visible))
  7305. {
  7306. QueueView3->SetFocus();
  7307. }
  7308. }
  7309. //---------------------------------------------------------------------------
  7310. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7311. TQueueOperation Operation, void * Param)
  7312. {
  7313. if (Operation == qoGoTo)
  7314. {
  7315. GoToQueue();
  7316. }
  7317. else if (Operation == qoPreferences)
  7318. {
  7319. PreferencesDialog(pmQueue);
  7320. }
  7321. else
  7322. {
  7323. FQueueController->ExecuteOperation(Operation, Param);
  7324. }
  7325. }
  7326. //---------------------------------------------------------------------------
  7327. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7328. {
  7329. return (Queue != NULL) && Queue->Enabled;
  7330. }
  7331. //---------------------------------------------------------------------------
  7332. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7333. {
  7334. DebugAssert(Queue != NULL);
  7335. if (Queue != NULL)
  7336. {
  7337. Queue->Enabled = !Queue->Enabled;
  7338. }
  7339. }
  7340. //---------------------------------------------------------------------------
  7341. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7342. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7343. {
  7344. FQueueActedItem = QueueView3->ItemFocused;
  7345. }
  7346. //---------------------------------------------------------------------------
  7347. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7348. {
  7349. return TopDock->Height + QueueSplitter->Height;
  7350. }
  7351. //---------------------------------------------------------------------------
  7352. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7353. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7354. {
  7355. // when queue is hidden by double-clicking splitter, stray attempt to
  7356. // resize the panel with strange value arrives, make sure it is ignored
  7357. if (ComponentVisible[fcQueueView])
  7358. {
  7359. int HeightLimit = ClientHeight - GetStaticComponentsHeight() -
  7360. RemotePanel->Constraints->MinHeight;
  7361. if (NewSize > HeightLimit)
  7362. {
  7363. NewSize = HeightLimit;
  7364. }
  7365. }
  7366. else
  7367. {
  7368. Accept = false;
  7369. }
  7370. }
  7371. //---------------------------------------------------------------------------
  7372. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7373. TDragObject *& /*DragObject*/)
  7374. {
  7375. FQueueActedItem = QueueView3->ItemFocused;
  7376. }
  7377. //---------------------------------------------------------------------------
  7378. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7379. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7380. {
  7381. Accept = true;
  7382. if (Source == QueueView3)
  7383. {
  7384. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7385. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7386. if (Accept)
  7387. {
  7388. TQueueItemProxy * QueueItem;
  7389. TQueueItemProxy * DestQueueItem;
  7390. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7391. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7392. Accept = (QueueItem != DestQueueItem) &&
  7393. (QueueItem->Status == TQueueItem::qsPending) &&
  7394. (DestQueueItem->Status == TQueueItem::qsPending);
  7395. }
  7396. }
  7397. }
  7398. //---------------------------------------------------------------------------
  7399. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7400. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7401. {
  7402. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7403. {
  7404. TQueueItemProxy * QueueItem;
  7405. TQueueItemProxy * DestQueueItem;
  7406. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7407. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7408. QueueItem->Move(DestQueueItem);
  7409. }
  7410. }
  7411. //---------------------------------------------------------------------------
  7412. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7413. {
  7414. if ((QueueView3->ItemFocused == NULL) &&
  7415. (QueueView3->Items->Count > 0))
  7416. {
  7417. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7418. }
  7419. QueueLabelUpdateStatus();
  7420. }
  7421. //---------------------------------------------------------------------------
  7422. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7423. {
  7424. QueueLabelUpdateStatus();
  7425. }
  7426. //---------------------------------------------------------------------------
  7427. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7428. {
  7429. QueueLabel->UpdateStatus();
  7430. }
  7431. //---------------------------------------------------------------------------
  7432. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7433. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7434. {
  7435. if (Selected)
  7436. {
  7437. NonVisualDataModule->UpdateNonVisibleActions();
  7438. }
  7439. }
  7440. //---------------------------------------------------------------------------
  7441. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7442. {
  7443. TDragDropFilesEx * Result = NULL;
  7444. if (Sender == SessionsPageControl)
  7445. {
  7446. Result = FSessionsDragDropFilesEx;
  7447. }
  7448. else if (Sender == QueueView3)
  7449. {
  7450. Result = FQueueDragDropFilesEx;
  7451. }
  7452. else
  7453. {
  7454. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7455. if (DirView != NULL)
  7456. {
  7457. Result = DirView->DragDropFilesEx;
  7458. }
  7459. else
  7460. {
  7461. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7462. if (DriveView != NULL)
  7463. {
  7464. Result = DriveView->DragDropFilesEx;
  7465. }
  7466. }
  7467. }
  7468. DebugAssert(Result != NULL);
  7469. return Result;
  7470. }
  7471. //---------------------------------------------------------------------------
  7472. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7473. {
  7474. return (DView->SelCount == DView->FilesCount);
  7475. }
  7476. //---------------------------------------------------------------------------
  7477. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7478. {
  7479. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7480. }
  7481. //---------------------------------------------------------------------------
  7482. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7483. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7484. {
  7485. DebugAssert(!IsLocalBrowserMode());
  7486. TFileOperation Operation;
  7487. switch (Effect)
  7488. {
  7489. case DROPEFFECT_MOVE:
  7490. Operation = foMove;
  7491. break;
  7492. case DROPEFFECT_COPY:
  7493. // occures on WinXP (reported by user)
  7494. default:
  7495. Operation = foCopy;
  7496. break;
  7497. };
  7498. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7499. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7500. if (DragDropFilesEx->FileList->Count > 0)
  7501. {
  7502. TStrings * FileList = new TStringList();
  7503. try
  7504. {
  7505. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7506. {
  7507. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7508. }
  7509. FDragDropOperation = true;
  7510. TTransferOperationParam Param;
  7511. Param.TargetDirectory = TargetPath;
  7512. // upload, no temp dirs
  7513. Param.Temp = false;
  7514. Param.DragDrop = DragDrop;
  7515. Param.Options =
  7516. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7517. if (ForceQueue)
  7518. {
  7519. Param.Queue = asOn;
  7520. }
  7521. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7522. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7523. {
  7524. if (IsFileControl(DropSourceControl, osLocal))
  7525. {
  7526. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7527. }
  7528. else
  7529. {
  7530. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7531. }
  7532. }
  7533. }
  7534. __finally
  7535. {
  7536. FDragDropOperation = false;
  7537. delete FileList;
  7538. }
  7539. }
  7540. }
  7541. //---------------------------------------------------------------------------
  7542. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7543. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7544. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7545. {
  7546. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7547. }
  7548. //---------------------------------------------------------------------------
  7549. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7550. TObject * Sender, int grfKeyState, int & dwEffect)
  7551. {
  7552. DebugAssert(!IsLocalBrowserMode());
  7553. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7554. if ((dwEffect != DROPEFFECT_NONE) &&
  7555. IsFileControl(DropSourceControl, osRemote))
  7556. {
  7557. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7558. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7559. (RemoteDirView->DropTarget == NULL))
  7560. {
  7561. dwEffect = DROPEFFECT_NONE;
  7562. }
  7563. else
  7564. {
  7565. if (dwEffect == DROPEFFECT_COPY)
  7566. {
  7567. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  7568. // currently we support copying always (at least via temporary directory);
  7569. // remove associated checks once this all proves stable and working
  7570. bool CopyCapable = true;
  7571. // if we do not support neither of operations, there's no discussion
  7572. if (!MoveCapable && !CopyCapable)
  7573. {
  7574. dwEffect = DROPEFFECT_NONE;
  7575. }
  7576. else
  7577. {
  7578. // The default effect inside remote panel is move,
  7579. // unless we do not support it, but support copy
  7580. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7581. {
  7582. dwEffect = MoveCapable ? DROPEFFECT_MOVE : DROPEFFECT_COPY;
  7583. }
  7584. else
  7585. {
  7586. // with ctrl-down, we want copy unless it is not supported
  7587. dwEffect = CopyCapable ? DROPEFFECT_COPY : DROPEFFECT_NONE;
  7588. }
  7589. }
  7590. }
  7591. }
  7592. }
  7593. }
  7594. //---------------------------------------------------------------------------
  7595. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7596. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7597. {
  7598. DebugAssert(!IsLocalBrowserMode());
  7599. if (FDDTotalSize >= 0)
  7600. {
  7601. if (File->IsDirectory)
  7602. {
  7603. FDDTotalSize = -1;
  7604. }
  7605. else
  7606. {
  7607. FDDTotalSize += File->Size;
  7608. }
  7609. }
  7610. DebugAssert(!FDragTempDir.IsEmpty());
  7611. // TODO: this is quite ineffective
  7612. // TODO: what if invalid character replacement is disabled?
  7613. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7614. UnicodeString TransferFileName;
  7615. if (Sender == RemoteDriveView)
  7616. {
  7617. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7618. }
  7619. else
  7620. {
  7621. TransferFileName = File->FileName;
  7622. }
  7623. FDDFileList->AddObject(TransferFileName, File);
  7624. }
  7625. //---------------------------------------------------------------------------
  7626. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7627. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7628. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7629. {
  7630. // sometimes we do not get DDEnd so the list is not released
  7631. SAFE_DESTROY(FDDFileList);
  7632. FDDFileList = new TStringList();
  7633. FDragTempDir = WinConfiguration->TemporaryDir();
  7634. FDDTotalSize = 0;
  7635. FDragDropSshTerminate.reset(NULL);
  7636. }
  7637. //---------------------------------------------------------------------------
  7638. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7639. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7640. HRESULT & Result)
  7641. {
  7642. if (Result == DRAGDROP_S_DROP)
  7643. {
  7644. try
  7645. {
  7646. GlobalDragImageList->HideDragImage();
  7647. try
  7648. {
  7649. RemoteFileControlDDTargetDrop();
  7650. }
  7651. catch(ESshTerminate & E)
  7652. {
  7653. FDragDropSshTerminate.reset(E.Clone());
  7654. }
  7655. }
  7656. catch (Exception &E)
  7657. {
  7658. // If downloading fails we need to cancel drag&drop, otherwise
  7659. // Explorer shows error
  7660. // But by the way exception probably never reach this point as
  7661. // it's catched on way.
  7662. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7663. Result = DRAGDROP_S_CANCEL;
  7664. ShowExtendedException(Terminal, &E);
  7665. }
  7666. }
  7667. }
  7668. //---------------------------------------------------------------------------
  7669. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7670. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7671. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7672. {
  7673. TFileMasks::TParams MaskParams;
  7674. MaskParams.Size = Size;
  7675. MaskParams.Modification = Modification;
  7676. // this does not re-parse the mask if it is the same as the last time
  7677. FDirViewMatchMask = Masks;
  7678. bool ImplicitMatch;
  7679. Matches =
  7680. // RecurseInclude parameter has no effect as the Path is empty
  7681. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7682. (AllowImplicitMatches || !ImplicitMatch);
  7683. }
  7684. //---------------------------------------------------------------------------
  7685. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7686. TObject * Sender, TListItem * Item, WORD & Indexes)
  7687. {
  7688. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7689. UnicodeString Ext;
  7690. if (dynamic_cast<TUnixDirView *>(DirView) != NULL)
  7691. {
  7692. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7693. }
  7694. else
  7695. {
  7696. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7697. }
  7698. if (SameText(Ext, Configuration->PartialExt))
  7699. {
  7700. Indexes |= oiPartial;
  7701. }
  7702. }
  7703. //---------------------------------------------------------------------------
  7704. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7705. {
  7706. return
  7707. DirViewEnabled(osCurrent) &&
  7708. DirView(osCurrent)->CanPasteFromClipBoard();
  7709. }
  7710. //---------------------------------------------------------------------------
  7711. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7712. {
  7713. bool Result = false;
  7714. if (CanPasteToDirViewFromClipBoard())
  7715. {
  7716. Result = true;
  7717. }
  7718. else
  7719. {
  7720. UnicodeString ClipboardText;
  7721. if (NonEmptyTextFromClipboard(ClipboardText))
  7722. {
  7723. if (StoredSessions->IsUrl(ClipboardText))
  7724. {
  7725. Result = true;
  7726. }
  7727. else
  7728. {
  7729. Result = DirViewEnabled(osCurrent);
  7730. }
  7731. }
  7732. }
  7733. return Result;
  7734. }
  7735. //---------------------------------------------------------------------------
  7736. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7737. {
  7738. if (DoesClipboardContainOurFiles())
  7739. {
  7740. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  7741. {
  7742. DebugAssert(!IsLocalBrowserMode());
  7743. TTerminalManager * Manager = TTerminalManager::Instance();
  7744. TTerminal * TergetTerminal = Manager->ActiveTerminal;
  7745. Manager->ActiveTerminal = FClipboardTerminal;
  7746. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TergetTerminal);
  7747. }
  7748. }
  7749. else if (CanPasteToDirViewFromClipBoard())
  7750. {
  7751. DirView(osCurrent)->PasteFromClipBoard();
  7752. }
  7753. else
  7754. {
  7755. UnicodeString ClipboardText;
  7756. if (NonEmptyTextFromClipboard(ClipboardText))
  7757. {
  7758. if (StoredSessions->IsUrl(ClipboardText))
  7759. {
  7760. NewSession(false, ClipboardText);
  7761. }
  7762. else
  7763. {
  7764. DirView(osCurrent)->Path = ClipboardText;
  7765. }
  7766. }
  7767. }
  7768. }
  7769. //---------------------------------------------------------------------------
  7770. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  7771. {
  7772. // TBD
  7773. }
  7774. //---------------------------------------------------------------------------
  7775. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  7776. {
  7777. TCustomDirView * ADirView = DirView(Side);
  7778. ADirView->SelectAll(Mode);
  7779. ADirView->SetFocus();
  7780. }
  7781. //---------------------------------------------------------------------------
  7782. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  7783. {
  7784. TCustomDirView * ADirView = DirView(Side);
  7785. TFileFilter Filter;
  7786. DefaultFileFilter(Filter);
  7787. if (DoSelectMaskDialog(ADirView, Select, Filter))
  7788. {
  7789. Configuration->Usage->Inc(L"MaskSelections");
  7790. ADirView->SelectFiles(Filter, Select);
  7791. ADirView->SetFocus();
  7792. }
  7793. }
  7794. //---------------------------------------------------------------------------
  7795. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  7796. {
  7797. TCustomDirView * ADirView = DirView(Side);
  7798. ADirView->RestoreSelectedNames();
  7799. ADirView->SetFocus();
  7800. }
  7801. //---------------------------------------------------------------------------
  7802. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  7803. {
  7804. TCustomDirView * CurrentDirView = DirView(osCurrent);
  7805. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  7806. {
  7807. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  7808. UnicodeString Ext;
  7809. if (!IsSideLocalBrowser(osCurrent))
  7810. {
  7811. Ext = UnixExtractFileExt(FileName);
  7812. }
  7813. else
  7814. {
  7815. Ext = ExtractFileExt(FileName);
  7816. }
  7817. if (Ext.IsEmpty())
  7818. {
  7819. Ext = L".";
  7820. }
  7821. TFileFilter Filter;
  7822. Filter.Masks = FORMAT(L"*%s", (Ext));
  7823. Filter.Directories = false;
  7824. CurrentDirView->SelectFiles(Filter, Select);
  7825. }
  7826. }
  7827. //---------------------------------------------------------------------------
  7828. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  7829. const TStatusFileInfo & FileInfo, TOperationSide Side)
  7830. {
  7831. UnicodeString Result;
  7832. if (!FIncrementalSearch.IsEmpty() && (Side == GetSide(osCurrent)))
  7833. {
  7834. Result = FormatIncrementalSearchStatus(FIncrementalSearch, FIncrementalSearchHaveNext);
  7835. }
  7836. else if (!IsSideLocalBrowser(Side) && ((Terminal == NULL) || Terminal->Disconnected))
  7837. {
  7838. // noop
  7839. }
  7840. else
  7841. {
  7842. Result =
  7843. FMTLOAD(FILE_INFO_FORMAT,
  7844. (FormatBytes(FileInfo.SelectedSize),
  7845. FormatBytes(FileInfo.FilesSize),
  7846. FormatNumber(FileInfo.SelectedCount),
  7847. FormatNumber(FileInfo.FilesCount)));
  7848. }
  7849. return Result;
  7850. }
  7851. //---------------------------------------------------------------------------
  7852. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  7853. TTBXStatusPanel * Panel, TOperationSide Side)
  7854. {
  7855. if (Panel->Index == 1)
  7856. {
  7857. ToggleShowHiddenFiles();
  7858. }
  7859. else if (Panel->Index == 2)
  7860. {
  7861. Filter(Side);
  7862. }
  7863. }
  7864. //---------------------------------------------------------------------------
  7865. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  7866. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  7867. {
  7868. DebugAssert(!StatusBar->SimplePanel);
  7869. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  7870. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  7871. }
  7872. //---------------------------------------------------------------------------
  7873. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  7874. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  7875. {
  7876. DebugAssert(StatusBar->Panels->Count >= 3);
  7877. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  7878. if (FileInfo.HiddenCount > 0)
  7879. {
  7880. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  7881. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7882. }
  7883. else
  7884. {
  7885. HiddenFilesPanel->ViewPriority = 0;
  7886. // not really necessary, just to cleanup no-longer-valid data
  7887. HiddenFilesPanel->Caption = L"";
  7888. }
  7889. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  7890. if (FileInfo.FilteredCount > 0)
  7891. {
  7892. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  7893. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7894. }
  7895. else
  7896. {
  7897. FilteredFilesPanel->ViewPriority = 0;
  7898. // not really necessary, just to cleanup no-longer-valid data
  7899. FilteredFilesPanel->Caption = L"";
  7900. }
  7901. }
  7902. //---------------------------------------------------------------------------
  7903. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  7904. TObject * /*Sender*/)
  7905. {
  7906. if (DirView(osRemote)->Enabled)
  7907. {
  7908. DirView(osRemote)->SetFocus();
  7909. }
  7910. }
  7911. //---------------------------------------------------------------------------
  7912. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  7913. {
  7914. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  7915. switch (Config.Show)
  7916. {
  7917. case qvShow:
  7918. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  7919. {
  7920. Config.Show = qvHide;
  7921. }
  7922. else
  7923. {
  7924. Config.Show = Config.LastHideShow;
  7925. }
  7926. break;
  7927. case qvHideWhenEmpty:
  7928. if (ComponentVisible[fcQueueView])
  7929. {
  7930. Config.Show = qvHide;
  7931. }
  7932. else
  7933. {
  7934. Config.LastHideShow = Config.Show;
  7935. Config.Show = qvShow;
  7936. }
  7937. break;
  7938. case qvHide:
  7939. Config.LastHideShow = Config.Show;
  7940. Config.Show = qvShow;
  7941. break;
  7942. }
  7943. WinConfiguration->QueueView = Config;
  7944. }
  7945. //---------------------------------------------------------------------------
  7946. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  7947. {
  7948. UnicodeString Result;
  7949. if (FTerminal != NULL)
  7950. {
  7951. switch (WinConfiguration->PathInCaption)
  7952. {
  7953. case picShort:
  7954. Result = TTerminalManager::Instance()->GetTerminalShortPath(FTerminal);
  7955. break;
  7956. case picFull:
  7957. Result = FTerminal->CurrentDirectory;
  7958. break;
  7959. }
  7960. }
  7961. return Result;
  7962. }
  7963. //---------------------------------------------------------------------------
  7964. TColor __fastcall TCustomScpExplorerForm::PanelColor()
  7965. {
  7966. TColor Result = GetWindowColor(FSessionColor);
  7967. return Result;
  7968. }
  7969. //---------------------------------------------------------------------------
  7970. TColor __fastcall TCustomScpExplorerForm::DisabledPanelColor()
  7971. {
  7972. TColor Result = (WinConfiguration->UseDarkTheme() ? static_cast<TColor>(RGB(0x40, 0x40, 0x40)) : clBtnFace);
  7973. return Result;
  7974. }
  7975. //---------------------------------------------------------------------------
  7976. void __fastcall TCustomScpExplorerForm::UpdateControls()
  7977. {
  7978. TTerminalManager::Instance()->UpdateAppTitle();
  7979. // WORAKRDOUND: Disabling list view when it is not showing yet does not set its
  7980. // background to gray on Windows 7 (works on Windows 10).
  7981. // See also EnableControl
  7982. if (Showing)
  7983. {
  7984. if (Terminal != NULL)
  7985. {
  7986. // Update path when it changes
  7987. if ((SessionsPageControl->ActivePage != NULL) && (GetSessionTabTerminal(SessionsPageControl->ActivePage) == Terminal))
  7988. {
  7989. UpdateSessionTab(SessionsPageControl->ActivePage);
  7990. }
  7991. }
  7992. NonVisualDataModule->ReconnectSessionAction->Update();
  7993. ReconnectToolbar->Visible = NonVisualDataModule->ReconnectSessionAction->Visible;
  7994. // ReconnectSessionAction is hidden when disabled, so enabling it actualy resizes the toolbar
  7995. CenterReconnectToolbar();
  7996. bool HasTerminal = (Terminal != NULL) && Terminal->Active;
  7997. if (HasTerminal)
  7998. {
  7999. // TODO needed yet with second local browser?
  8000. if (!RemoteDirView->Enabled)
  8001. {
  8002. RemoteDirView->Enabled = true;
  8003. if (FRemoteDirViewWasFocused)
  8004. {
  8005. ActiveControl = RemoteDirView;
  8006. }
  8007. }
  8008. RemoteDirView->Color = PanelColor();
  8009. }
  8010. else
  8011. {
  8012. if (RemoteDirView->Enabled)
  8013. {
  8014. // This is first called when the form is being constructed
  8015. // (not anymore due to Showing test above)
  8016. // but the false is overriden in the constructor later.
  8017. // An even later in TScpCommanderForm::DoShow()
  8018. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  8019. RemoteDirView->Enabled = false;
  8020. RemoteDirView->Color = DisabledPanelColor();
  8021. }
  8022. }
  8023. RemoteDirView->Font->Color = GetWindowTextColor(RemoteDirView->Color);
  8024. RemoteDriveView->Enabled = RemoteDirView->Enabled;
  8025. RemoteDriveView->Color = RemoteDirView->Color;
  8026. RemoteDriveView->Font->Color = RemoteDirView->Font->Color;
  8027. QueueView3->Enabled = HasTerminal && Terminal->IsCapable[fsBackgroundTransfers];
  8028. QueueView3->Color = QueueView3->Enabled ? GetWindowColor() : DisabledPanelColor();
  8029. QueueView3->Font->Color = GetWindowTextColor(QueueView3->Color);
  8030. QueueLabelUpdateStatus();
  8031. RemoteDirView->DarkMode = WinConfiguration->UseDarkTheme();
  8032. RemoteDriveView->DarkMode = RemoteDirView->DarkMode;
  8033. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  8034. }
  8035. }
  8036. //---------------------------------------------------------------------------
  8037. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  8038. {
  8039. UpdateControls();
  8040. }
  8041. //---------------------------------------------------------------------------
  8042. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  8043. TObject * Sender)
  8044. {
  8045. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8046. DebugAssert(DirView != NULL);
  8047. DoDirViewLoaded(DirView);
  8048. TransferPresetAutoSelect();
  8049. }
  8050. //---------------------------------------------------------------------------
  8051. void __fastcall TCustomScpExplorerForm::StartUpdates()
  8052. {
  8053. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  8054. // first run after installation
  8055. if (double(Updates.LastCheck) == 0)
  8056. {
  8057. // make sure next time there will be an update (if enabled)
  8058. Updates.LastCheck = TDateTime(1);
  8059. WinConfiguration->Updates = Updates;
  8060. }
  8061. else if ((double(Updates.Period) > 0) &&
  8062. (Now() - Updates.LastCheck >= Updates.Period))
  8063. {
  8064. TThreadMethod OnUpdatesChecked = NULL;
  8065. if (!IsUWP())
  8066. {
  8067. OnUpdatesChecked = UpdatesChecked;
  8068. }
  8069. StartUpdateThread(OnUpdatesChecked);
  8070. }
  8071. }
  8072. //---------------------------------------------------------------------------
  8073. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  8074. {
  8075. UnicodeString Message;
  8076. bool New;
  8077. TQueryType Type;
  8078. GetUpdatesMessage(Message, New, Type, false);
  8079. if (!Message.IsEmpty())
  8080. {
  8081. if (New)
  8082. {
  8083. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  8084. }
  8085. if (!New && (Type != qtWarning))
  8086. {
  8087. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  8088. }
  8089. else
  8090. {
  8091. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  8092. }
  8093. if (New)
  8094. {
  8095. Configuration->Usage->Inc(L"UpdateNotifications");
  8096. }
  8097. }
  8098. }
  8099. //---------------------------------------------------------------------------
  8100. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  8101. {
  8102. RestoreApp();
  8103. bool CanDisplay = !NonVisualDataModule->Busy;
  8104. if (!CanDisplay && (Screen->ActiveForm != NULL))
  8105. {
  8106. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  8107. }
  8108. if (CanDisplay)
  8109. {
  8110. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  8111. CheckForUpdates(true);
  8112. }
  8113. else
  8114. {
  8115. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  8116. }
  8117. }
  8118. //---------------------------------------------------------------------------
  8119. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  8120. TCopyParamRuleData & Data)
  8121. {
  8122. DebugAssert(Terminal != NULL);
  8123. Data.HostName = Terminal->SessionData->HostNameExpanded;
  8124. Data.UserName = Terminal->SessionData->UserNameExpanded;
  8125. Data.RemoteDirectory = RemoteDirView->PathName;
  8126. }
  8127. //---------------------------------------------------------------------------
  8128. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  8129. {
  8130. // Terminal can be null when we are changing local directory implicitly,
  8131. // because it has been deleted, while no session is connected
  8132. // (Login dialog is open)
  8133. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  8134. {
  8135. DebugAssert(!IsLocalBrowserMode());
  8136. TCopyParamRuleData Data;
  8137. GetTransferPresetAutoSelectData(Data);
  8138. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  8139. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  8140. if (CopyParamIndex < 0)
  8141. {
  8142. // there is no preset that matches autoselection
  8143. // set preset that we consider "default"
  8144. FCopyParamAutoSelected = L""; // forget last autoselected preset
  8145. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  8146. }
  8147. else
  8148. {
  8149. // there is preset matching autoselection
  8150. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  8151. if (CopyParamName == FCopyParamAutoSelected)
  8152. {
  8153. // autoselected the same preset as the last time
  8154. // make no change (i.e. preserve custom user preset, if any)
  8155. }
  8156. else
  8157. {
  8158. // autoselected the different preset then the last time (or there
  8159. // was default preset set)
  8160. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  8161. GUIConfiguration->CopyParamCurrent = CopyParamName;
  8162. }
  8163. }
  8164. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  8165. {
  8166. if (CopyParamIndex >= 0)
  8167. {
  8168. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  8169. }
  8170. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  8171. try
  8172. {
  8173. int Fmt =
  8174. (CopyParamIndex < 0) ?
  8175. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  8176. COPY_PARAM_AUTOSELECTED;
  8177. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  8178. Data->Message = MainInstructions(Message);
  8179. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  8180. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  8181. CopyParamAttrs);
  8182. if (CopyParamIndex >= 0)
  8183. {
  8184. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  8185. Info = FORMAT(L"%s\n \n%s", (Info,
  8186. FMTLOAD(COPY_PARAM_RULE,
  8187. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  8188. }
  8189. Data->Message += L"\n\n" + Info;
  8190. if (WinConfiguration->CopyParamAutoSelectNotice)
  8191. {
  8192. TransferPresetNoteMessage(Data, true);
  8193. }
  8194. else
  8195. {
  8196. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  8197. Data = NULL; // ownership passed
  8198. }
  8199. }
  8200. __finally
  8201. {
  8202. delete Data;
  8203. }
  8204. }
  8205. }
  8206. }
  8207. //---------------------------------------------------------------------------
  8208. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  8209. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  8210. {
  8211. DebugAssert(NoteData != NULL);
  8212. DebugAssert(!IsLocalBrowserMode());
  8213. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  8214. TQueryButtonAlias Aliases[1];
  8215. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  8216. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  8217. Params.Aliases = Aliases;
  8218. Params.AliasesCount = LENOF(Aliases);
  8219. unsigned int Result =
  8220. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  8221. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  8222. switch (Result)
  8223. {
  8224. case qaNeverAskAgain:
  8225. DebugAssert(AllowNeverAskAgain);
  8226. WinConfiguration->CopyParamAutoSelectNotice = false;
  8227. break;
  8228. case qaIgnore:
  8229. PreferencesDialog(pmPresets);
  8230. break;
  8231. }
  8232. }
  8233. //---------------------------------------------------------------------------
  8234. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  8235. {
  8236. // as of now useless, as this is used for notes only, never for balloons, ...
  8237. RestoreApp();
  8238. // .. and we should never be busy here
  8239. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  8240. {
  8241. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  8242. }
  8243. }
  8244. //---------------------------------------------------------------------------
  8245. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  8246. TPreferencesMode APreferencesMode)
  8247. {
  8248. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8249. TCopyParamRuleData Data;
  8250. if (Terminal != NULL)
  8251. {
  8252. PreferencesData.reset(new TPreferencesDialogData());
  8253. GetTransferPresetAutoSelectData(Data);
  8254. PreferencesData->CopyParamRuleData = &Data;
  8255. }
  8256. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8257. }
  8258. //---------------------------------------------------------------------------
  8259. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8260. const TCustomCommandType & Command)
  8261. {
  8262. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8263. {
  8264. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (Command.Command)));
  8265. }
  8266. }
  8267. //---------------------------------------------------------------------------
  8268. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8269. {
  8270. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8271. TCustomCommandType Command;
  8272. // make sure we use local custom command when remote are not supported
  8273. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8274. {
  8275. Command = FLastCustomCommand;
  8276. }
  8277. else
  8278. {
  8279. Command.Params = Command.Params | ccLocal;
  8280. }
  8281. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8282. FEditingFocusedAdHocCommand = OnFocused;
  8283. bool LocalSide = IsSideLocalBrowser(FCurrentSide);
  8284. int Options =
  8285. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8286. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8287. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8288. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8289. {
  8290. FLastCustomCommand = Command;
  8291. UpdateCustomCommandsToolbar();
  8292. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8293. }
  8294. }
  8295. //---------------------------------------------------------------------------
  8296. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8297. {
  8298. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8299. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8300. DebugAssert(State > 0);
  8301. if (State <= 0)
  8302. {
  8303. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (FLastCustomCommand.Command)));
  8304. }
  8305. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8306. }
  8307. //---------------------------------------------------------------------------
  8308. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8309. TCustomCommandType & Command, int & State)
  8310. {
  8311. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8312. if (Result)
  8313. {
  8314. Command = FLastCustomCommand;
  8315. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8316. }
  8317. return Result;
  8318. }
  8319. //---------------------------------------------------------------------------
  8320. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8321. {
  8322. ResetIncrementalSearch();
  8323. }
  8324. //---------------------------------------------------------------------------
  8325. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8326. {
  8327. DebugAssert(ComponentVisible[Component]);
  8328. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8329. }
  8330. //---------------------------------------------------------------------------
  8331. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8332. {
  8333. // when queue panel is resized here directly, the status bar is stretched
  8334. // over whole space the panel occupied
  8335. PostComponentHide(fcQueueView);
  8336. }
  8337. //---------------------------------------------------------------------------
  8338. void __fastcall TCustomScpExplorerForm::WMWinIniChange(TMessage & Message)
  8339. {
  8340. WinConfiguration->ResetSysDarkTheme();
  8341. ConfigurationChanged();
  8342. ConfigureInterface();
  8343. TForm::Dispatch(&Message);
  8344. }
  8345. //---------------------------------------------------------------------------
  8346. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8347. {
  8348. TMessage * M = static_cast<TMessage*>(Message);
  8349. switch (M->Msg)
  8350. {
  8351. case CM_DIALOGCHAR:
  8352. CMDialogChar(*M);
  8353. break;
  8354. case WM_APPCOMMAND:
  8355. WMAppCommand(*M);
  8356. break;
  8357. case WM_SYSCOMMAND:
  8358. WMSysCommand(*M);
  8359. break;
  8360. case WM_QUERYENDSESSION:
  8361. WMQueryEndSession(*M);
  8362. break;
  8363. case WM_ENDSESSION:
  8364. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8365. break;
  8366. #ifdef _DEBUG
  8367. case WM_WINDOWPOSCHANGED:
  8368. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8369. break;
  8370. case CM_CUSTOMSTYLECHANGED:
  8371. TForm::Dispatch(Message);
  8372. break;
  8373. #endif
  8374. case WM_COMPONENT_HIDE:
  8375. {
  8376. Byte Component = static_cast<Byte>(M->WParam);
  8377. // sanity check
  8378. if (ComponentVisible[Component])
  8379. {
  8380. // special treatment
  8381. if (Component == fcQueueView)
  8382. {
  8383. ToggleQueueVisibility();
  8384. DebugAssert(!ComponentVisible[fcQueueView]);
  8385. }
  8386. else
  8387. {
  8388. ComponentVisible[Component] = false;
  8389. }
  8390. }
  8391. }
  8392. break;
  8393. case WM_COPYDATA:
  8394. WMCopyData(*M);
  8395. break;
  8396. case WM_MANAGES_CAPTION:
  8397. // caption managed in TTerminalManager::UpdateAppTitle()
  8398. M->Result = 1;
  8399. break;
  8400. case WM_WANTS_MOUSEWHEEL:
  8401. M->Result = 1;
  8402. break;
  8403. case CM_SHOWINGCHANGED:
  8404. CMShowingChanged(*M);
  8405. break;
  8406. case WM_CLOSE:
  8407. WMClose(*M);
  8408. break;
  8409. case WM_DPICHANGED:
  8410. WMDpiChanged(*M);
  8411. break;
  8412. case CM_DPICHANGED:
  8413. CMDpiChanged(*M);
  8414. break;
  8415. case WM_WININICHANGE:
  8416. WMWinIniChange(*M);
  8417. break;
  8418. case CM_DIALOGKEY:
  8419. CMDialogKey(*reinterpret_cast<TWMKeyDown *>(M));
  8420. break;
  8421. default:
  8422. TForm::Dispatch(Message);
  8423. break;
  8424. }
  8425. }
  8426. //---------------------------------------------------------------------------
  8427. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8428. {
  8429. // noop
  8430. }
  8431. //---------------------------------------------------------------------------
  8432. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8433. {
  8434. TForm::Dispatch(&Message);
  8435. FSessionColors->Clear();
  8436. AddFixedSessionImages();
  8437. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8438. UpdateImages();
  8439. }
  8440. //---------------------------------------------------------------------------
  8441. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8442. {
  8443. TForm::Dispatch(&Message);
  8444. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8445. UpdatePixelsPerInchMainWindowCounter();
  8446. }
  8447. //---------------------------------------------------------------------------
  8448. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8449. {
  8450. // Cannot close window while we are busy.
  8451. // We cannot test this in FormCloseQuery as that is called also from
  8452. // Close(), which is called by CloseApplicationAction. So we can be busy
  8453. // there already even, when it is legitimate to close the application.
  8454. // Possibly a better place to handle this would be WMSysCommand.
  8455. if (NonVisualDataModule->Busy)
  8456. {
  8457. Message.Result = 1;
  8458. }
  8459. else
  8460. {
  8461. TForm::Dispatch(&Message);
  8462. }
  8463. }
  8464. //---------------------------------------------------------------------------
  8465. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8466. {
  8467. TForm::Dispatch(&Message);
  8468. if (Showing && (Terminal == NULL))
  8469. {
  8470. // When we are starting minimized (i.e. from an installer),
  8471. // postpone showing Login dialog until we get restored.
  8472. // Otherwise the Login dialog (and Authentication window) show restored
  8473. // over invisible (minimized) main window.
  8474. if (WindowState == wsMinimized)
  8475. {
  8476. FNeedSession = true;
  8477. }
  8478. else
  8479. {
  8480. // This happens before application ever goes idle, so the toolbars would
  8481. // stay enabled (initial state) until the Login dialog is dismissed.
  8482. UpdateActions();
  8483. NonVisualDataModule->StartBusy();
  8484. try
  8485. {
  8486. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8487. // otherwise the dialog does not receive focus.
  8488. // With Commander interface the ProcessMessages is called already
  8489. // by TDriveView, but with Explorer interface, we need to call it explicily
  8490. Application->ProcessMessages();
  8491. // do not reload sessions, they have been loaded just now (optimization)
  8492. NeedSession(true);
  8493. }
  8494. __finally
  8495. {
  8496. NonVisualDataModule->EndBusy();
  8497. }
  8498. }
  8499. }
  8500. }
  8501. //---------------------------------------------------------------------------
  8502. void __fastcall TCustomScpExplorerForm::CenterReconnectToolbar()
  8503. {
  8504. ReconnectToolbar->Left = (ReconnectToolbar->Parent->ClientWidth - ReconnectToolbar->Width) / 2;
  8505. ReconnectToolbar->Top = (ReconnectToolbar->Parent->ClientHeight - ReconnectToolbar->Height) / 2;
  8506. }
  8507. //---------------------------------------------------------------------------
  8508. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8509. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8510. int & MaxHeight)
  8511. {
  8512. // workaround for bug in TWinControl.CalcConstraints
  8513. // Check for empty rect (restore from iconic state) is done there only after
  8514. // call to AdjustClientRect, which enlarges the rect (for forms).
  8515. TRect R = GetClientRect();
  8516. // when restoring from iconic state, place no restrictions
  8517. if (IsRectEmpty(R))
  8518. {
  8519. MinWidth = 0;
  8520. MinHeight = 0;
  8521. MaxWidth = 32000;
  8522. MaxHeight = 32000;
  8523. }
  8524. }
  8525. //---------------------------------------------------------------------------
  8526. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8527. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8528. {
  8529. // terminal can be already closed (e.g. dropped connection)
  8530. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8531. {
  8532. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8533. try
  8534. {
  8535. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8536. }
  8537. catch(...)
  8538. {
  8539. if (!Terminal->Active)
  8540. {
  8541. Close = true;
  8542. }
  8543. throw;
  8544. }
  8545. }
  8546. }
  8547. //---------------------------------------------------------------------------
  8548. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8549. {
  8550. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8551. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8552. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8553. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8554. {
  8555. OnGetSpaceAvailable = GetSpaceAvailable;
  8556. }
  8557. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8558. OnGetSpaceAvailable);
  8559. }
  8560. //---------------------------------------------------------------------------
  8561. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8562. {
  8563. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8564. Terminal->FillSessionDataForCode(Data.get());
  8565. return Data.release();
  8566. }
  8567. //---------------------------------------------------------------------------
  8568. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8569. {
  8570. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8571. DoGenerateUrlDialog(Data.get(), Paths);
  8572. }
  8573. //---------------------------------------------------------------------------
  8574. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8575. {
  8576. GenerateUrl(NULL);
  8577. }
  8578. //---------------------------------------------------------------------------
  8579. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8580. {
  8581. std::unique_ptr<TStrings> Paths(new TStringList());
  8582. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8583. GenerateUrl(Paths.get());
  8584. }
  8585. //---------------------------------------------------------------------------
  8586. void __fastcall TCustomScpExplorerForm::UpdateSessionColor(TColor value)
  8587. {
  8588. FSessionColor = value;
  8589. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8590. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8591. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8592. DebugAssert(ColorItem != NULL);
  8593. ColorItem->Color = C;
  8594. NonVisualDataModule->ColorMenuItem->Color = C;
  8595. UpdateControls();
  8596. }
  8597. //---------------------------------------------------------------------------
  8598. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8599. {
  8600. if (value != FSessionColor)
  8601. {
  8602. UpdateSessionColor(value);
  8603. // Is null when called from LastTerminalClosed
  8604. if (Terminal != NULL)
  8605. {
  8606. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8607. }
  8608. }
  8609. }
  8610. //---------------------------------------------------------------------------
  8611. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8612. {
  8613. bool Result = FNoteTimer->Enabled;
  8614. if (Result)
  8615. {
  8616. // cannot cancel note too early
  8617. bool NotEarly =
  8618. (Now() - FNoteShown >
  8619. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8620. if (Force || NotEarly)
  8621. {
  8622. FNoteTimer->Enabled = false;
  8623. FNote = L"";
  8624. SAFE_DESTROY(FNoteData);
  8625. FOnNoteClick = NULL;
  8626. FNoteHints = FNotes->Text;
  8627. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8628. UpdateStatusBar();
  8629. }
  8630. }
  8631. return Result;
  8632. }
  8633. //---------------------------------------------------------------------------
  8634. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8635. {
  8636. DebugAssert(FNoteTimer->Enabled);
  8637. CancelNote(true);
  8638. }
  8639. //---------------------------------------------------------------------------
  8640. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8641. {
  8642. int P = Note.Pos(L"\n");
  8643. if (P > 0)
  8644. {
  8645. Note.SetLength(P - 1);
  8646. }
  8647. FNotes->Add(FORMAT(L"[%s] %s",
  8648. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8649. while (FNotes->Count > 10)
  8650. {
  8651. FNotes->Delete(0);
  8652. }
  8653. if (UpdateNow)
  8654. {
  8655. FNoteHints = FNotes->Text;
  8656. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8657. UpdateStatusBar();
  8658. }
  8659. }
  8660. //---------------------------------------------------------------------------
  8661. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8662. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8663. {
  8664. int P = Note.Pos(L"\n");
  8665. if (P > 0)
  8666. {
  8667. Note.SetLength(P - 1);
  8668. }
  8669. FNoteHints = FNotes->Text;
  8670. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8671. FNote = Note;
  8672. SAFE_DESTROY(FNoteData);
  8673. FNoteData = NoteData;
  8674. FOnNoteClick = OnNoteClick;
  8675. AddNote(Note, false);
  8676. UpdateStatusBar();
  8677. FNoteShown = Now();
  8678. FNoteTimer->Enabled = false;
  8679. if (Seconds == 0)
  8680. {
  8681. Seconds = WinConfiguration->NotificationsTimeout;
  8682. }
  8683. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8684. FNoteTimer->Enabled = true;
  8685. }
  8686. //---------------------------------------------------------------------------
  8687. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8688. {
  8689. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8690. }
  8691. //---------------------------------------------------------------------------
  8692. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8693. {
  8694. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  8695. {
  8696. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8697. }
  8698. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  8699. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  8700. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  8701. 0, NULL, NULL);
  8702. }
  8703. //---------------------------------------------------------------------------
  8704. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  8705. {
  8706. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  8707. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  8708. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  8709. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  8710. }
  8711. //---------------------------------------------------------------------------
  8712. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  8713. {
  8714. WinConfiguration->FormatSizeBytes = Style;
  8715. }
  8716. //---------------------------------------------------------------------------
  8717. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  8718. {
  8719. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  8720. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  8721. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  8722. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  8723. }
  8724. //---------------------------------------------------------------------------
  8725. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  8726. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  8727. {
  8728. if (Panel->Index == 0)
  8729. {
  8730. if (FOnNoteClick != NULL)
  8731. {
  8732. // prevent the user data from being freed by possible call
  8733. // to CancelNote or PostNote during call to OnNoteClick
  8734. std::unique_ptr<TObject> NoteData(FNoteData);
  8735. TNotifyEvent OnNoteClick = FOnNoteClick;
  8736. FNoteData = NULL;
  8737. // need to cancel the note as we are going to delete its user data
  8738. CancelNote(true);
  8739. OnNoteClick(NoteData.get());
  8740. }
  8741. }
  8742. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  8743. {
  8744. FileSystemInfo();
  8745. }
  8746. }
  8747. //---------------------------------------------------------------------------
  8748. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  8749. {
  8750. // workaround:
  8751. // 1) for unknown reason, disabling window, while minimized,
  8752. // prevents it from restoring, even if it was enabled again meanwhile
  8753. // 2) when disabling the main window, while another has focus
  8754. // minimize is no longer possible
  8755. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  8756. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  8757. // Shouldn't we use IsApplicationMinimized() here?
  8758. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  8759. {
  8760. Enabled = false;
  8761. }
  8762. FLockLevel++;
  8763. }
  8764. //---------------------------------------------------------------------------
  8765. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  8766. {
  8767. DebugAssert(FLockLevel > 0);
  8768. FLockLevel--;
  8769. if (FLockLevel == 0)
  8770. {
  8771. DebugAssert(FLockSuspendLevel == 0);
  8772. Enabled = true;
  8773. }
  8774. }
  8775. //---------------------------------------------------------------------------
  8776. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  8777. {
  8778. // We need to make sure that window is enabled when the last modal window closes
  8779. // otherwise focus is not restored correctly.
  8780. // So we re-enable the window when modal window opens and
  8781. // disable it back after is closes
  8782. if (FLockLevel > 0)
  8783. {
  8784. // while we have nesting counter, we know that we never be called
  8785. // recursivelly as Application->OnModalBegin is called only
  8786. // for the top-level modal window
  8787. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8788. {
  8789. // won't be disabled when conditions in LockWindow() were not satisfied
  8790. FDisabledOnLockSuspend = !Enabled;
  8791. // When minimized to tray (or actually when set to SW_HIDE),
  8792. // setting Enabled makes the window focusable even when there's
  8793. // modal window over it
  8794. if (!FTrayIcon->Visible)
  8795. {
  8796. Enabled = true;
  8797. }
  8798. }
  8799. FLockSuspendLevel++;
  8800. }
  8801. }
  8802. //---------------------------------------------------------------------------
  8803. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  8804. {
  8805. if (FLockSuspendLevel > 0)
  8806. {
  8807. DebugAssert(FLockLevel > 0);
  8808. FLockSuspendLevel--;
  8809. // see comment in SuspendWindowLock
  8810. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8811. {
  8812. // Note that window can be enabled here, when we were minized to tray when
  8813. // was SuspendWindowLock() called.
  8814. // We should possibly do the same check as in LockWindow(),
  8815. // if it is ever possible that the consitions change between
  8816. // SuspendWindowLock() and ResumeWindowLock()
  8817. Enabled = !FDisabledOnLockSuspend;
  8818. }
  8819. }
  8820. }
  8821. //---------------------------------------------------------------------------
  8822. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  8823. {
  8824. if (!TextOnly)
  8825. {
  8826. TTBXComboBoxItem * RemotePathComboBox =
  8827. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  8828. TStrings * Items = RemotePathComboBox->Strings;
  8829. Items->BeginUpdate();
  8830. try
  8831. {
  8832. Items->Clear();
  8833. if ((Terminal != NULL) && !RemoteDirView->Path.IsEmpty())
  8834. {
  8835. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8836. while (!IsUnixRootPath(APath))
  8837. {
  8838. int P = APath.LastDelimiter(L'/');
  8839. DebugAssert(P >= 0);
  8840. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  8841. APath.SetLength(P - 1);
  8842. }
  8843. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  8844. }
  8845. }
  8846. __finally
  8847. {
  8848. RemotePathComboBox->ItemIndex = Items->Count - 1;
  8849. // Setting ItemIndex to -1 does not reset its text
  8850. if (Items->Count == 0)
  8851. {
  8852. RemotePathComboBox->Text = L"";
  8853. }
  8854. Items->EndUpdate();
  8855. }
  8856. }
  8857. }
  8858. //---------------------------------------------------------------------------
  8859. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  8860. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  8861. int & ImageIndex)
  8862. {
  8863. if (AIndex < 0)
  8864. {
  8865. AIndex = Sender->ItemIndex;
  8866. }
  8867. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  8868. }
  8869. //---------------------------------------------------------------------------
  8870. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  8871. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  8872. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  8873. {
  8874. ARect.Left += (10 * AIndex);
  8875. }
  8876. //---------------------------------------------------------------------------
  8877. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  8878. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  8879. {
  8880. AWidth += (10 * AIndex);
  8881. }
  8882. //---------------------------------------------------------------------------
  8883. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  8884. TObject * Sender)
  8885. {
  8886. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  8887. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8888. int Index = RemotePathComboBox->ItemIndex;
  8889. while (Index < RemotePathComboBox->Strings->Count - 1)
  8890. {
  8891. APath = UnixExtractFileDir(APath);
  8892. Index++;
  8893. }
  8894. // VanDyke style paths
  8895. if (APath.IsEmpty())
  8896. {
  8897. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  8898. APath = ROOTDIRECTORY;
  8899. }
  8900. if (RemoteDirView->Path != APath)
  8901. {
  8902. RemoteDirView->Path = APath;
  8903. }
  8904. }
  8905. //---------------------------------------------------------------------------
  8906. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  8907. {
  8908. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  8909. DebugUsedParam(Sender);
  8910. UpdateRemotePathComboBox(true);
  8911. }
  8912. //---------------------------------------------------------------------------
  8913. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  8914. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  8915. {
  8916. ResetIncrementalSearch();
  8917. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8918. DebugAssert(DirView != NULL);
  8919. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  8920. {
  8921. HWND Edit = ListView_GetEditControl(DirView->Handle);
  8922. // OnEditing is called also from TCustomListView::CanEdit
  8923. if (Edit != NULL)
  8924. {
  8925. EditSelectBaseName(Edit);
  8926. }
  8927. }
  8928. }
  8929. //---------------------------------------------------------------------------
  8930. //---------------------------------------------------------------------------
  8931. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  8932. {
  8933. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  8934. Result->AutoDetectDnD = false;
  8935. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  8936. Result->RenderDataOn = rdoEnterAndDropSync;
  8937. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  8938. return Result;
  8939. }
  8940. //---------------------------------------------------------------------------
  8941. void __fastcall TCustomScpExplorerForm::CreateWnd()
  8942. {
  8943. TForm::CreateWnd();
  8944. if (FSessionsDragDropFilesEx == NULL)
  8945. {
  8946. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  8947. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  8948. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  8949. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  8950. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  8951. }
  8952. if (FQueueDragDropFilesEx == NULL)
  8953. {
  8954. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  8955. // No need to set OnDragOver as we do not have any restrictions
  8956. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  8957. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  8958. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  8959. }
  8960. }
  8961. //---------------------------------------------------------------------------
  8962. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  8963. {
  8964. TForm::DestroyWnd();
  8965. FSessionsDragDropFilesEx->DragDropControl = NULL;
  8966. FQueueDragDropFilesEx->DragDropControl = NULL;
  8967. }
  8968. //---------------------------------------------------------------------------
  8969. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  8970. {
  8971. SideEnter(FCurrentSide);
  8972. FEverShown = true;
  8973. }
  8974. //---------------------------------------------------------------------------
  8975. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  8976. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  8977. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  8978. {
  8979. if (!NonVisualDataModule->Busy)
  8980. {
  8981. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  8982. Configuration->Usage->Inc(L"FileFinds");
  8983. LockWindow(true);
  8984. NonVisualDataModule->StartBusy();
  8985. WinConfiguration->LockedInterface = true;
  8986. try
  8987. {
  8988. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  8989. }
  8990. __finally
  8991. {
  8992. WinConfiguration->LockedInterface = false;
  8993. NonVisualDataModule->EndBusy();
  8994. UnlockWindow();
  8995. }
  8996. }
  8997. }
  8998. //---------------------------------------------------------------------------
  8999. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  9000. {
  9001. if (!NonVisualDataModule->Busy)
  9002. {
  9003. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9004. SetFocus();
  9005. RemoteDirView->Path = UnixExtractFilePath(Path);
  9006. UnicodeString FileName = UnixExtractFileName(Path);
  9007. TListItem * Item = RemoteDirView->FindFileItem(FileName);
  9008. // If not found, it can because we have loaded directory from cache and the file is new, try again after reload.
  9009. // (tbd: we should not even try this, if we have not loaded the directory from the cache)
  9010. if (Item == NULL)
  9011. {
  9012. ReloadDirectory(osRemote);
  9013. Item = RemoteDirView->FindFileItem(FileName);
  9014. }
  9015. if (Item != NULL)
  9016. {
  9017. RemoteDirView->ItemFocused = Item;
  9018. RemoteDirView->ItemFocused->MakeVisible(false);
  9019. RemoteDirView->SetFocus();
  9020. }
  9021. }
  9022. }
  9023. //---------------------------------------------------------------------------
  9024. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  9025. {
  9026. bool Result = !NonVisualDataModule->Busy;
  9027. if (Result)
  9028. {
  9029. TTerminalManager::Instance()->ActiveTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  9030. }
  9031. return Result;
  9032. }
  9033. //---------------------------------------------------------------------------
  9034. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  9035. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9036. {
  9037. if (CanOperateOnFoundFiles(ATerminal))
  9038. {
  9039. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  9040. FOnFileOperationFinished = OnFileOperationFinished;
  9041. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  9042. }
  9043. }
  9044. //---------------------------------------------------------------------------
  9045. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  9046. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9047. {
  9048. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  9049. }
  9050. //---------------------------------------------------------------------------
  9051. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  9052. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9053. {
  9054. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  9055. }
  9056. //---------------------------------------------------------------------------
  9057. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  9058. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  9059. {
  9060. if (CanOperateOnFoundFiles(ATerminal))
  9061. {
  9062. for (int Index = 0; Index < FileList->Count; Index++)
  9063. {
  9064. UnicodeString FileName = FileList->Strings[Index];
  9065. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  9066. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  9067. OnFileOperationFinished(FileName, true);
  9068. }
  9069. }
  9070. }
  9071. //---------------------------------------------------------------------------
  9072. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  9073. {
  9074. FFileFindTerminal = Terminal;
  9075. ShowFileFindDialog(
  9076. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  9077. DoDownloadFoundFiles, DoEditFoundFiles);
  9078. }
  9079. //---------------------------------------------------------------------------
  9080. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  9081. {
  9082. FTaskbarList = TaskbarList;
  9083. }
  9084. //---------------------------------------------------------------------------
  9085. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  9086. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  9087. {
  9088. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9089. if (Index >= 0)
  9090. {
  9091. if (Button == mbLeft)
  9092. {
  9093. // "Mouse down" is raised only after tab is switched.
  9094. // If switching tab (switching session) takes long enough for user
  9095. // to actually release the button, "mouse down" is still raised,
  9096. // but we do not get "mouse up" event, so dragging is not cancelled,
  9097. // prevent that by not beginning dragging in the first place.
  9098. if (IsKeyPressed(VK_LBUTTON))
  9099. {
  9100. // when user clicks the "+", we get mouse down only after the session
  9101. // is closed, when new session tab is already on X:Y, so dragging
  9102. // starts, prevent that
  9103. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  9104. {
  9105. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9106. if (Terminal != NULL)
  9107. {
  9108. SessionsPageControl->BeginDrag(false);
  9109. }
  9110. }
  9111. }
  9112. }
  9113. else if (Button == mbMiddle)
  9114. {
  9115. // ignore middle-click for "New session tab"
  9116. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9117. if (Terminal != NULL)
  9118. {
  9119. CloseSessionTab(Index);
  9120. }
  9121. }
  9122. }
  9123. }
  9124. //---------------------------------------------------------------------------
  9125. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  9126. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  9127. {
  9128. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9129. // do not allow dropping on the "+" tab
  9130. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9131. if ((TargetTerminal != NULL) &&
  9132. (SessionsPageControl->ActivePage->PageIndex != Index))
  9133. {
  9134. Configuration->Usage->Inc(L"SessionTabMoves");
  9135. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  9136. // but we want to actually prevent that to avoid flicker
  9137. SessionsPageControl->ActivePage->PageIndex = Index;
  9138. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  9139. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  9140. }
  9141. }
  9142. //---------------------------------------------------------------------------
  9143. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  9144. TObject * Sender, TObject * Source, int X, int Y,
  9145. TDragState /*State*/, bool & Accept)
  9146. {
  9147. Accept = (Sender == Source);
  9148. if (Accept)
  9149. {
  9150. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  9151. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9152. // do not allow dragging to the "+" tab
  9153. Accept = (Terminal != NULL);
  9154. }
  9155. }
  9156. //---------------------------------------------------------------------------
  9157. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(int /*KeyState*/,
  9158. const TPoint & Point, int & Effect)
  9159. {
  9160. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9161. if (Index < 0)
  9162. {
  9163. Effect = DROPEFFECT_NONE;
  9164. }
  9165. else
  9166. {
  9167. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9168. // do not allow dropping on the "+" tab
  9169. if (TargetTerminal == NULL)
  9170. {
  9171. Effect = DROPEFFECT_NONE;
  9172. }
  9173. else if ((TargetTerminal != Terminal) && (Effect == DROPEFFECT_MOVE))
  9174. {
  9175. Effect = DROPEFFECT_COPY;
  9176. }
  9177. }
  9178. }
  9179. //---------------------------------------------------------------------------
  9180. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  9181. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  9182. {
  9183. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  9184. // do not allow dropping on the "+" tab
  9185. TManagedTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  9186. if (TargetTerminal != NULL)
  9187. {
  9188. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  9189. if (!IsFileControl(DropSourceControl, osRemote))
  9190. {
  9191. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  9192. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  9193. // Why don't we use Terminal->CurrentDirectory directly?
  9194. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  9195. // do not force queue, drag drop
  9196. false, true);
  9197. }
  9198. }
  9199. }
  9200. //---------------------------------------------------------------------------
  9201. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  9202. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  9203. {
  9204. // Downloads are handled in RemoteFileControlDDEnd
  9205. if (!IsFileControl(DropSourceControl, osRemote))
  9206. {
  9207. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  9208. Terminal->CurrentDirectory,
  9209. // force queue, drag drop
  9210. true, true);
  9211. }
  9212. }
  9213. //---------------------------------------------------------------------------
  9214. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  9215. {
  9216. FShowing = false;
  9217. // Do not save empty workspace
  9218. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  9219. {
  9220. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  9221. UnicodeString Name = WorkspaceName();
  9222. DoSaveWorkspace(
  9223. Name, DataList.get(),
  9224. !Configuration->DisablePasswordStoring &&
  9225. WinConfiguration->AutoSaveWorkspacePasswords, false);
  9226. WinConfiguration->LastStoredSession = Name;
  9227. }
  9228. }
  9229. //---------------------------------------------------------------------------
  9230. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  9231. {
  9232. TManagedTerminal * ManagedTerminal =
  9233. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  9234. if (ManagedTerminal != NULL)
  9235. {
  9236. ManagedTerminal->DirectoryLoaded = Now();
  9237. }
  9238. }
  9239. //---------------------------------------------------------------------------
  9240. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  9241. TListItem * /*Item*/, bool /*Selected*/)
  9242. {
  9243. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  9244. switch (DirView->LastSelectMethod)
  9245. {
  9246. case smKeyboard:
  9247. Configuration->Usage->Inc(L"KeyboardSelections");
  9248. break;
  9249. case smMouse:
  9250. Configuration->Usage->Inc(L"MouseSelections");
  9251. break;
  9252. }
  9253. }
  9254. //---------------------------------------------------------------------------
  9255. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  9256. {
  9257. if (Color != 0)
  9258. {
  9259. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  9260. }
  9261. else
  9262. {
  9263. return FSessionTabImageIndex;
  9264. }
  9265. }
  9266. //---------------------------------------------------------------------------
  9267. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  9268. {
  9269. TPngImageCollectionItem * Item =
  9270. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  9271. FSessionColors->AddPng(Item->PngImage);
  9272. return FSessionColors->Count - 1;
  9273. }
  9274. //---------------------------------------------------------------------------
  9275. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  9276. {
  9277. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  9278. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  9279. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9280. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9281. }
  9282. //---------------------------------------------------------------------------
  9283. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9284. {
  9285. for (int Index = 0; Index < Control->ControlCount; Index++)
  9286. {
  9287. TControl * AControl = Control->Controls[Index];
  9288. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9289. if (WinControl != NULL)
  9290. {
  9291. CollectItemsWithTextDisplayMode(WinControl);
  9292. }
  9293. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9294. if (Toolbar != NULL)
  9295. {
  9296. // we care for top-level items only
  9297. TTBCustomItem * Items = Toolbar->Items;
  9298. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9299. {
  9300. TTBCustomItem * Item = Items->Items[ItemIndex];
  9301. if (((Item->DisplayMode == nbdmImageAndText) ||
  9302. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9303. EligibleForImageDisplayMode(Item))
  9304. {
  9305. FItemsWithTextDisplayMode.insert(Item);
  9306. }
  9307. }
  9308. }
  9309. }
  9310. }
  9311. //---------------------------------------------------------------------------
  9312. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9313. {
  9314. return true;
  9315. }
  9316. //---------------------------------------------------------------------------
  9317. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9318. {
  9319. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9320. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9321. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9322. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9323. bool Result = true;
  9324. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9325. {
  9326. TTBCustomItem * Item = *i;
  9327. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9328. if (Label != NULL)
  9329. {
  9330. // optimization
  9331. if (Label->Visible == SelectiveToolbarText)
  9332. {
  9333. Result = false;
  9334. }
  9335. else
  9336. {
  9337. Label->Visible = SelectiveToolbarText;
  9338. }
  9339. }
  9340. else
  9341. {
  9342. // optimization
  9343. if (Item->DisplayMode == DisplayMode)
  9344. {
  9345. Result = false;
  9346. }
  9347. else
  9348. {
  9349. Item->DisplayMode = DisplayMode;
  9350. }
  9351. }
  9352. ++i;
  9353. }
  9354. if (Result)
  9355. {
  9356. UpdateNewSessionTab();
  9357. }
  9358. return Result;
  9359. }
  9360. //---------------------------------------------------------------------------
  9361. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9362. {
  9363. DebugFail();
  9364. }
  9365. //---------------------------------------------------------------------------
  9366. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9367. {
  9368. // Among other, a lock level is non zero, while directory is being loaded,
  9369. // even when it happens as a result of dirview navigation,
  9370. // i.e. when TNonVisualDataModule is NOT FBusy.
  9371. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9372. // Among other this prevents a panel auto update to occur while
  9373. // directory is changing.
  9374. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9375. }
  9376. //---------------------------------------------------------------------------
  9377. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  9378. {
  9379. // While the window is disabled, we still seem to process menu shortcuts at least,
  9380. // so stop it at least here.
  9381. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9382. return
  9383. (Allowed == aaUpdate) ||
  9384. !NonVisualDataModule->Busy;
  9385. }
  9386. //---------------------------------------------------------------------------
  9387. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9388. {
  9389. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9390. }
  9391. //---------------------------------------------------------------------------
  9392. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9393. {
  9394. // This is somewhat redundant to LockWindow() call from
  9395. // TTerminalManager::TerminalReadDirectoryProgress.
  9396. // But disabling window is known not to block keyboard shorcuts
  9397. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9398. if (Busy > 0)
  9399. {
  9400. if (NonVisualDataModule->Busy)
  9401. {
  9402. State = false;
  9403. }
  9404. else
  9405. {
  9406. NonVisualDataModule->StartBusy();
  9407. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9408. LockWindow();
  9409. }
  9410. }
  9411. else if (Busy < 0)
  9412. {
  9413. UnlockWindow();
  9414. NonVisualDataModule->EndBusy();
  9415. }
  9416. else
  9417. {
  9418. State = NonVisualDataModule->Busy;
  9419. }
  9420. }
  9421. //---------------------------------------------------------------------------
  9422. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9423. {
  9424. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9425. // no context menu for "New session tab"
  9426. if ((Index >= 0) && (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != NULL))
  9427. {
  9428. SessionsPageControl->ActivePageIndex = Index;
  9429. if (DebugAlwaysTrue(SessionTabSwitched()))
  9430. {
  9431. // copied from TControl.WMContextMenu
  9432. SendCancelMode(SessionsPageControl);
  9433. // explicit popup instead of using PopupMenu property
  9434. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9435. // while connecting yet not-connected session and hence
  9436. // allowing an access to commands over not-completelly connected session
  9437. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9438. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  9439. PopupMenu->PopupComponent = SessionsPageControl;
  9440. PopupMenu->Popup(Point.x, Point.y);
  9441. }
  9442. }
  9443. Handled = true;
  9444. }
  9445. //---------------------------------------------------------------------------
  9446. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9447. {
  9448. return (Terminal != NULL) && Terminal->Active && Terminal->IsCapable[fcChangePassword];
  9449. }
  9450. //---------------------------------------------------------------------------
  9451. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9452. {
  9453. Configuration->Usage->Inc(L"PasswordChanges");
  9454. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9455. ChangePasswordData->DisableAuthentationsExceptPassword();
  9456. ChangePasswordData->ChangePassword = true;
  9457. std::unique_ptr<TTerminal> ChangePasswordSession(
  9458. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9459. ChangePasswordSession->AutoReadDirectory = false;
  9460. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9461. {
  9462. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9463. }
  9464. else
  9465. {
  9466. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9467. }
  9468. }
  9469. //---------------------------------------------------------------------------
  9470. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9471. {
  9472. // No nice way to assert SSH2
  9473. return (Terminal != NULL) && Terminal->Active && (Terminal->FSProtocol == cfsSFTP);
  9474. }
  9475. //---------------------------------------------------------------------------
  9476. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9477. {
  9478. TOperationVisualizer OperationVisualizer;
  9479. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9480. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9481. }
  9482. //---------------------------------------------------------------------------
  9483. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9484. {
  9485. TForm::ChangeScale(M, D);
  9486. int APixelsPerInch = GetControlPixelsPerInch(this);
  9487. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9488. try
  9489. {
  9490. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9491. }
  9492. __finally
  9493. {
  9494. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9495. }
  9496. }
  9497. //---------------------------------------------------------------------------
  9498. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9499. {
  9500. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9501. }
  9502. //---------------------------------------------------------------------
  9503. class TPublicControl : public TControl
  9504. {
  9505. friend class TCustomScpExplorerForm;
  9506. };
  9507. //---------------------------------------------------------------------------
  9508. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9509. {
  9510. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9511. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9512. }
  9513. //---------------------------------------------------------------------------
  9514. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9515. {
  9516. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9517. OpenBookmark(Side, Bookmark);
  9518. }
  9519. //---------------------------------------------------------------------------
  9520. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9521. {
  9522. DoBookmarkClick(osLocal, Sender);
  9523. }
  9524. //---------------------------------------------------------------------------
  9525. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9526. {
  9527. DoBookmarkClick(osRemote, Sender);
  9528. }
  9529. //---------------------------------------------------------------------------
  9530. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9531. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9532. {
  9533. // TODO
  9534. if (BookmarkList != NULL)
  9535. {
  9536. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9537. if (!WinConfiguration->UseLocationProfiles)
  9538. {
  9539. std::unique_ptr<TStringList> Directories(new TStringList());
  9540. Directories->CaseSensitive = (Side == osRemote);
  9541. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9542. {
  9543. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9544. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9545. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9546. {
  9547. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9548. Item->Caption = EscapeHotkey(Directory);
  9549. Item->ShortCut = Bookmark->ShortCut;
  9550. Item->OnClick = OnBookmarkClick;
  9551. Item->Tag = reinterpret_cast<int>(Bookmark);
  9552. Directories->Add(Directory);
  9553. Menu->Add(Item.release());
  9554. }
  9555. }
  9556. }
  9557. else
  9558. {
  9559. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9560. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9561. {
  9562. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9563. if (!Bookmark->Node.IsEmpty())
  9564. {
  9565. Folders->Add(Bookmark->Node);
  9566. }
  9567. }
  9568. for (int Index = 0; Index < Folders->Count; Index++)
  9569. {
  9570. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9571. Item->Caption = Folders->Strings[Index];
  9572. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction->ImageIndex;
  9573. Folders->Objects[Index] = Item.get();
  9574. Menu->Add(Item.release());
  9575. }
  9576. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9577. {
  9578. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9579. TTBCustomItem * Parent;
  9580. if (!Bookmark->Node.IsEmpty())
  9581. {
  9582. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9583. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9584. }
  9585. else
  9586. {
  9587. Parent = Menu;
  9588. }
  9589. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9590. Item->Caption = Bookmark->Name;
  9591. Item->ShortCut = Bookmark->ShortCut;
  9592. Item->OnClick = OnBookmarkClick;
  9593. Item->Tag = reinterpret_cast<int>(Bookmark);
  9594. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9595. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9596. {
  9597. // No hint for location profiles that are actually mere "bookmarks"
  9598. }
  9599. else
  9600. {
  9601. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  9602. UnicodeString LongHint;
  9603. if (!Bookmark->Local.IsEmpty())
  9604. {
  9605. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  9606. }
  9607. if (!Bookmark->Remote.IsEmpty())
  9608. {
  9609. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  9610. }
  9611. AddToList(Hint, LongHint, L"|");
  9612. Item->Hint = Hint;
  9613. }
  9614. Parent->Add(Item.release());
  9615. }
  9616. }
  9617. AddMenuSeparator(Menu);
  9618. }
  9619. }
  9620. //---------------------------------------------------------------------------
  9621. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  9622. {
  9623. Menu->Clear();
  9624. std::unique_ptr<TTBCustomItem> Item;
  9625. Item.reset(new TTBXItem(Owner));
  9626. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  9627. Item->Options = Item->Options << tboDefault;
  9628. Menu->Add(Item.release());
  9629. AddMenuSeparator(Menu);
  9630. if (Terminal != NULL)
  9631. {
  9632. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  9633. }
  9634. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  9635. Item.reset(new TTBXItem(Owner));
  9636. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction : NonVisualDataModule->RemoteAddBookmarkAction;
  9637. Menu->Add(Item.release());
  9638. }
  9639. //---------------------------------------------------------------------------
  9640. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  9641. {
  9642. bool Result = true;
  9643. try
  9644. {
  9645. if (Side == osRemote)
  9646. {
  9647. Terminal->ExceptionOnFail = true;
  9648. }
  9649. try
  9650. {
  9651. DirView(Side)->Path = Path;
  9652. }
  9653. __finally
  9654. {
  9655. if (Side == osRemote)
  9656. {
  9657. Terminal->ExceptionOnFail = false;
  9658. }
  9659. }
  9660. }
  9661. catch (Exception & E)
  9662. {
  9663. if ((Side != osRemote) || Terminal->Active)
  9664. {
  9665. ShowExtendedExceptionEx(Terminal, &E);
  9666. Result = false;
  9667. }
  9668. else
  9669. {
  9670. throw;
  9671. }
  9672. }
  9673. return Result;
  9674. }
  9675. //---------------------------------------------------------------------------
  9676. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  9677. {
  9678. // Make sure there some feedback even when loading tiny local folders
  9679. TInstantOperationVisualizer Visualizer;
  9680. DirView(Side)->ReloadDirectory();
  9681. }
  9682. //---------------------------------------------------------------------------
  9683. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  9684. {
  9685. if (Index == SessionsPageControl->TabIndex)
  9686. {
  9687. CloseSession();
  9688. }
  9689. else
  9690. {
  9691. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  9692. TTerminal * Terminal = GetSessionTabTerminal(TabSheet);
  9693. TTerminalManager * Manager = TTerminalManager::Instance();
  9694. if (!Terminal->Active)
  9695. {
  9696. Manager->FreeTerminal(Terminal);
  9697. }
  9698. else
  9699. {
  9700. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  9701. if (CanCloseQueue(Queue))
  9702. {
  9703. Manager->FreeTerminal(Terminal);
  9704. }
  9705. }
  9706. }
  9707. }
  9708. //---------------------------------------------------------------------------
  9709. void __fastcall TCustomScpExplorerForm::SessionsPageControlCloseButtonClick(TPageControl * /*Sender*/, int Index)
  9710. {
  9711. CloseSessionTab(Index);
  9712. }
  9713. //---------------------------------------------------------------------------
  9714. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side, bool OnFocused)
  9715. {
  9716. if (DebugAlwaysTrue(!IsSideLocalBrowser(Side)))
  9717. {
  9718. TInstantOperationVisualizer Visualizer;
  9719. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  9720. // before we create a new data object
  9721. ClipboardClear();
  9722. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  9723. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  9724. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  9725. // Intermediate variable to WORKAROUND a crash in compiler
  9726. TObjectList * ClipboardFakeMonitors =
  9727. StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated);
  9728. FClipboardFakeMonitors.reset(ClipboardFakeMonitors);
  9729. if (FClipboardDragDropFilesEx.get() == NULL)
  9730. {
  9731. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  9732. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  9733. }
  9734. FClipboardDragDropFilesEx->FileList->Clear();
  9735. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  9736. FClipboardDragDropFilesEx->CopyToClipboard();
  9737. FClipboardTerminal = FTerminal;
  9738. // Need full paths, as cwd can be different once files are pasted
  9739. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(OnFocused, true)));
  9740. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  9741. {
  9742. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  9743. }
  9744. }
  9745. }
  9746. //---------------------------------------------------------------------------
  9747. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  9748. {
  9749. if (OpenClipboard(0))
  9750. {
  9751. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  9752. CloseClipboard();
  9753. }
  9754. }
  9755. //---------------------------------------------------------------------------
  9756. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  9757. {
  9758. return !FClipboardFakeDirectory.IsEmpty();
  9759. }
  9760. //---------------------------------------------------------------------------
  9761. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  9762. {
  9763. RemoveDir(ApiPath(FClipboardFakeDirectory));
  9764. FClipboardFakeDirectory = UnicodeString();
  9765. FClipboardTerminal = NULL;
  9766. // Also called in Idle()
  9767. FClipboardFakeMonitors.reset(NULL);
  9768. }
  9769. //---------------------------------------------------------------------------
  9770. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  9771. {
  9772. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  9773. {
  9774. ClipboardStop();
  9775. }
  9776. }
  9777. //---------------------------------------------------------------------------
  9778. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  9779. {
  9780. TTerminalManager * Manager = TTerminalManager::Instance();
  9781. Manager->ActiveTerminal = FClipboardTerminal;
  9782. TTransferOperationParam Params;
  9783. Params.TargetDirectory = TargetDirectory;
  9784. Params.DragDrop = DragDrop;
  9785. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  9786. }
  9787. //---------------------------------------------------------------------------
  9788. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  9789. {
  9790. // It can actually rarelly happen that some random file is created, while we are shuttting down the monitor
  9791. // (as it pumps a Windows message queue while being shutted down)
  9792. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  9793. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  9794. {
  9795. // Can fail as it can be e.g. locked by AV, so we retry that later
  9796. bool Removed = RemoveDir(ApiPath(FileName));
  9797. try
  9798. {
  9799. if (!NonVisualDataModule->Busy)
  9800. {
  9801. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  9802. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  9803. }
  9804. }
  9805. __finally
  9806. {
  9807. if (!Removed)
  9808. {
  9809. RemoveDir(ApiPath(FileName));
  9810. }
  9811. }
  9812. }
  9813. }
  9814. //---------------------------------------------------------------------------
  9815. void __fastcall TCustomScpExplorerForm::DirViewGetItemColor(
  9816. TObject * Sender, UnicodeString FileName, bool Directory, __int64 Size, TDateTime Modification, TColor & Color)
  9817. {
  9818. TFileMasks::TParams MaskParams;
  9819. MaskParams.Size = Size;
  9820. MaskParams.Modification = Modification;
  9821. TCustomDirView * DirView = DebugNotNull(dynamic_cast<TCustomDirView *>(Sender));
  9822. for (TFileColorData::TList::const_iterator Iter = FFileColors.begin(); Iter != FFileColors.end(); Iter++)
  9823. {
  9824. bool ImplicitMatch;
  9825. if (Iter->FileMask.Matches(FileName, Directory, DirView->PathName, &MaskParams, false, ImplicitMatch) &&
  9826. !ImplicitMatch)
  9827. {
  9828. Color = Iter->Color;
  9829. break;
  9830. }
  9831. }
  9832. }
  9833. //---------------------------------------------------------------------------
  9834. void __fastcall TCustomScpExplorerForm::DirViewKeyDown(TObject * Sender, WORD & Key, TShiftState)
  9835. {
  9836. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9837. if (!DirView->IsEditing())
  9838. {
  9839. // Handled here, instead of RemoteDirViewKeyPress (as on Login dialog),
  9840. // as VK_BACK is handled by TCustomDirView.KeyDown and we can swallow it here,
  9841. // while searching
  9842. if (Key == VK_BACK)
  9843. {
  9844. if (!FIncrementalSearch.IsEmpty())
  9845. {
  9846. if (FIncrementalSearch.Length() == 1)
  9847. {
  9848. ResetIncrementalSearch();
  9849. }
  9850. else
  9851. {
  9852. UnicodeString NewText = FIncrementalSearch.SubString(1, FIncrementalSearch.Length() - 1);
  9853. IncrementalSearch(NewText, false, false);
  9854. }
  9855. Key = 0;
  9856. }
  9857. }
  9858. }
  9859. }
  9860. //---------------------------------------------------------------------------
  9861. void __fastcall TCustomScpExplorerForm::DirViewKeyPress(TObject * Sender, wchar_t & Key)
  9862. {
  9863. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  9864. if (!DirView->IsEditing() && (WinConfiguration->PanelSearch != isOff))
  9865. {
  9866. // Filter control sequences.
  9867. // When not searching yet, prefer use of the space for toggling file selection
  9868. // (so we cannot incrementally search for a string starting with a space).
  9869. if ((Key > VK_SPACE) ||
  9870. ((Key == VK_SPACE) && (GetKeyState(VK_CONTROL) >= 0) && !FIncrementalSearch.IsEmpty()))
  9871. {
  9872. IncrementalSearch(FIncrementalSearch + Key, false, false);
  9873. Key = 0;
  9874. }
  9875. }
  9876. }
  9877. //---------------------------------------------------------------------------
  9878. void __fastcall TCustomScpExplorerForm::ResetIncrementalSearch()
  9879. {
  9880. if (!FIncrementalSearch.IsEmpty())
  9881. {
  9882. FIncrementalSearch = L"";
  9883. DirView(osCurrent)->UpdateStatusBar();
  9884. }
  9885. }
  9886. //---------------------------------------------------------------------------
  9887. void __fastcall TCustomScpExplorerForm::IncrementalSearch(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9888. {
  9889. TListItem * Item = SearchFile(Text, SkipCurrent, Reverse);
  9890. if (Item == NULL)
  9891. {
  9892. MessageBeep(MB_ICONHAND);
  9893. }
  9894. else
  9895. {
  9896. TCustomDirView * ADirView = DirView(osCurrent);
  9897. {
  9898. TAutoNestingCounter Guard(FIncrementalSearching);
  9899. ADirView->FocusItem(Item);
  9900. if (ADirView->NortonLike == nlOff)
  9901. {
  9902. if ((ADirView->Selected != Item) ||
  9903. (ADirView->SelCount > 1))
  9904. {
  9905. ADirView->ClearSelection();
  9906. ADirView->Selected = Item;
  9907. }
  9908. }
  9909. }
  9910. FIncrementalSearch = Text;
  9911. Item->MakeVisible(false);
  9912. TListItem * NextItem = SearchFile(Text, true, Reverse);
  9913. FIncrementalSearchHaveNext = (NextItem != NULL) && (NextItem != Item);
  9914. ADirView->UpdateStatusBar();
  9915. }
  9916. }
  9917. //---------------------------------------------------------------------------
  9918. TListItem * __fastcall TCustomScpExplorerForm::GetNextFile(TListItem * Item, bool Reverse)
  9919. {
  9920. int Index = Item->Index;
  9921. if (!Reverse)
  9922. {
  9923. Index++;
  9924. if (Index >= Item->Owner->Count)
  9925. {
  9926. Index = 0;
  9927. }
  9928. }
  9929. else
  9930. {
  9931. Index--;
  9932. if (Index < 0)
  9933. {
  9934. Index = Item->Owner->Count - 1;
  9935. }
  9936. }
  9937. return Item->Owner->Item[Index];
  9938. }
  9939. //---------------------------------------------------------------------------
  9940. TListItem * __fastcall TCustomScpExplorerForm::SearchFile(const UnicodeString & Text, bool SkipCurrent, bool Reverse)
  9941. {
  9942. TCustomDirView * ADirView = DirView(osCurrent);
  9943. if (ADirView->Items->Count == 0)
  9944. {
  9945. return NULL;
  9946. }
  9947. else
  9948. {
  9949. TListItem * CurrentItem = (ADirView->ItemFocused != NULL) ? ADirView->ItemFocused : ADirView->Items->Item[0];
  9950. TListItem * Item = CurrentItem;
  9951. if (SkipCurrent)
  9952. {
  9953. Item = DebugNotNull(GetNextFile(Item, Reverse));
  9954. }
  9955. while (true)
  9956. {
  9957. bool Matches = false;
  9958. switch (WinConfiguration->PanelSearch)
  9959. {
  9960. case isNameStartOnly:
  9961. Matches = ContainsTextSemiCaseSensitive(Item->Caption.SubString(1, Text.Length()), Text);
  9962. break;
  9963. case isName:
  9964. Matches = ContainsTextSemiCaseSensitive(Item->Caption, Text);
  9965. break;
  9966. case isAll:
  9967. int ColCount = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Count : 1;
  9968. int Index = 0;
  9969. while ((Index < ColCount) && !Matches)
  9970. {
  9971. bool Visible = (ADirView->ViewStyle == vsReport) ? ADirView->ColProperties->Visible[Index] : true;
  9972. if (Visible)
  9973. {
  9974. Matches = ContainsTextSemiCaseSensitive(ADirView->GetColumnText(Item, Index), Text);
  9975. }
  9976. Index++;
  9977. }
  9978. break;
  9979. }
  9980. if (Matches)
  9981. {
  9982. return Item;
  9983. }
  9984. Item = GetNextFile(Item, Reverse);
  9985. if (Item == CurrentItem)
  9986. {
  9987. return NULL;
  9988. }
  9989. }
  9990. }
  9991. }
  9992. //---------------------------------------------------------------------------
  9993. void __fastcall TCustomScpExplorerForm::DirViewExit(TObject *)
  9994. {
  9995. ResetIncrementalSearch();
  9996. }
  9997. //---------------------------------------------------------------------------
  9998. void __fastcall TCustomScpExplorerForm::CMDialogKey(TWMKeyDown & Message)
  9999. {
  10000. if (Message.CharCode == VK_TAB)
  10001. {
  10002. if (!FIncrementalSearch.IsEmpty())
  10003. {
  10004. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  10005. bool Reverse = Shift.Contains(ssShift);
  10006. IncrementalSearch(FIncrementalSearch, true, Reverse);
  10007. Message.Result = 1;
  10008. return;
  10009. }
  10010. }
  10011. else if (Message.CharCode == VK_ESCAPE)
  10012. {
  10013. if (!FIncrementalSearch.IsEmpty())
  10014. {
  10015. ResetIncrementalSearch();
  10016. Message.Result = 1;
  10017. return;
  10018. }
  10019. }
  10020. TForm::Dispatch(&Message);
  10021. }
  10022. //---------------------------------------------------------------------------
  10023. void __fastcall TCustomScpExplorerForm::Deactivate()
  10024. {
  10025. TForm::Deactivate();
  10026. // E.g. when switching to an internal editor window
  10027. ResetIncrementalSearch();
  10028. }
  10029. //---------------------------------------------------------------------------
  10030. void __fastcall TCustomScpExplorerForm::ApplicationEventsDeactivate(TObject *)
  10031. {
  10032. // When switching to another application
  10033. ResetIncrementalSearch();
  10034. }
  10035. //---------------------------------------------------------------------------
  10036. void __fastcall TCustomScpExplorerForm::ApplicationEventsModalBegin(TObject *)
  10037. {
  10038. ResetIncrementalSearch();
  10039. }
  10040. //---------------------------------------------------------------------------
  10041. void __fastcall TCustomScpExplorerForm::DirViewChangeFocus(TObject *, TListItem *)
  10042. {
  10043. if (FIncrementalSearching <= 0)
  10044. {
  10045. ResetIncrementalSearch();
  10046. }
  10047. }
  10048. //---------------------------------------------------------------------------
  10049. void __fastcall TCustomScpExplorerForm::RemoteStatusBarMouseDown(TObject *, TMouseButton, TShiftState, int, int)
  10050. {
  10051. CountClicksForWindowPrint(this);
  10052. }
  10053. //---------------------------------------------------------------------------
  10054. void __fastcall TCustomScpExplorerForm::RemoteDirViewResize(TObject *)
  10055. {
  10056. CenterReconnectToolbar();
  10057. }
  10058. //---------------------------------------------------------------------------
  10059. void __fastcall TCustomScpExplorerForm::BrowseFile()
  10060. {
  10061. if (RemoteDirView->ItemFocused != NULL)
  10062. {
  10063. RemoteDirView->ItemFocused->Selected = true;
  10064. }
  10065. }
  10066. //---------------------------------------------------------------------------