CustomScpExplorer.cpp 328 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073
  1. //---------------------------------------------------------------------------
  2. #define NO_WIN32_LEAN_AND_MEAN
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include <Common.h>
  6. #include "CustomScpExplorer.h"
  7. #include <Bookmarks.h>
  8. #include <Interface.h>
  9. #include <Exceptions.h>
  10. #include <CoreMain.h>
  11. #include <FileSystems.h>
  12. #include <TextsCore.h>
  13. #include <TextsWin.h>
  14. #include <HelpWin.h>
  15. #include <VCLCommon.h>
  16. #include <Progress.h>
  17. #include <SynchronizeProgress.h>
  18. #include <DragExt.h>
  19. #include <WinApi.h>
  20. #include "GUITools.h"
  21. #include "NonVisual.h"
  22. #include "Glyphs.h"
  23. #include "Tools.h"
  24. #include "WinConfiguration.h"
  25. #include "TerminalManager.h"
  26. #include "EditorManager.h"
  27. #include "ProgParams.h"
  28. #include "Setup.h"
  29. #include <Consts.hpp>
  30. #include <DateUtils.hpp>
  31. #include <TB2Common.hpp>
  32. #include <DirectoryMonitor.hpp>
  33. //---------------------------------------------------------------------------
  34. #pragma package(smart_init)
  35. #pragma link "CustomDirView"
  36. #pragma link "CustomUnixDirView"
  37. #pragma link "IEListView"
  38. #pragma link "NortonLikeListView"
  39. #pragma link "UnixDirView"
  40. #pragma link "CustomDriveView"
  41. #pragma link "UnixDriveView"
  42. #pragma link "CustomDriveView"
  43. #pragma link "TB2Dock"
  44. #pragma link "TBXStatusBars"
  45. #pragma link "TB2Item"
  46. #pragma link "TB2Toolbar"
  47. #pragma link "ThemePageControl"
  48. #pragma link "PathLabel"
  49. #ifndef NO_RESOURCES
  50. #pragma resource "*.dfm"
  51. #endif
  52. //---------------------------------------------------------------------------
  53. #define SAVE_SELECTION(DIRVIEW) \
  54. UnicodeString FocusFile = L""; \
  55. UnicodeString LastFocusedFile = L""; \
  56. if (DIRVIEW->ItemFocused) LastFocusedFile = DIRVIEW->ItemFocused->Caption; \
  57. { TListItem * ClosestUnselected = DIRVIEW->ClosestUnselected(DIRVIEW->ItemFocused); \
  58. if (ClosestUnselected) FocusFile = ClosestUnselected->Caption; }
  59. #define RESTORE_SELECTION(DIRVIEW) \
  60. if (!LastFocusedFile.IsEmpty() && \
  61. (!DIRVIEW->ItemFocused || (DIRVIEW->ItemFocused->Caption != LastFocusedFile))) \
  62. { \
  63. TListItem *ItemToSelect = DIRVIEW->FindFileItem(FocusFile); \
  64. if (ItemToSelect) \
  65. { \
  66. DIRVIEW->ItemFocused = ItemToSelect; \
  67. DIRVIEW->ItemFocused->MakeVisible(False); \
  68. } \
  69. }
  70. //---------------------------------------------------------------------------
  71. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  72. static const int SessionPanelCount = 4;
  73. //---------------------------------------------------------------------------
  74. class TMutexGuard
  75. {
  76. public:
  77. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  78. int Timeout = 5000)
  79. {
  80. FMutex = NULL;
  81. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  82. if (WaitResult == WAIT_TIMEOUT)
  83. {
  84. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  85. }
  86. else
  87. {
  88. FMutex = AMutex;
  89. }
  90. }
  91. ~TMutexGuard()
  92. {
  93. if (FMutex != NULL)
  94. {
  95. ReleaseMutex(FMutex);
  96. }
  97. }
  98. private:
  99. HANDLE FMutex;
  100. };
  101. //---------------------------------------------------------------------------
  102. class TWindowLock
  103. {
  104. public:
  105. TWindowLock(TCustomScpExplorerForm * Form) :
  106. FForm(Form)
  107. {
  108. FForm->LockWindow();
  109. }
  110. ~TWindowLock()
  111. {
  112. FForm->UnlockWindow();
  113. }
  114. private:
  115. TCustomScpExplorerForm * FForm;
  116. };
  117. //---------------------------------------------------------------------------
  118. 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. TTerminal * 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. RestoreParams();
  156. ConfigurationChanged();
  157. RemoteDirView->Invalidate();
  158. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  159. NonVisualDataModule->ScpExplorer = this;
  160. FAutoOperation = false;
  161. FOnFileOperationFinished = NULL;
  162. FForceExecution = false;
  163. FIgnoreNextDialogChar = 0;
  164. FErrorList = NULL;
  165. FSynchronizeProgressForm = NULL;
  166. FProgressForm = NULL;
  167. FRefreshLocalDirectory = false;
  168. FRefreshRemoteDirectory = false;
  169. FDDMoveSlipped = false;
  170. FDDExtMapFile = NULL;
  171. // CreateMutexW keeps failing with ERROR_NOACCESS
  172. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  173. DebugAssert(FDDExtMutex != NULL);
  174. FDDTargetControl = NULL;
  175. FDelayedDeletionTimer = NULL;
  176. FDelayedDeletionList = new TStringList();
  177. FDDFileList = NULL;
  178. FPendingTempSpaceWarn = false;
  179. FCapturedLog = NULL;
  180. FDragDropOperation = false;
  181. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  182. FAllowTransferPresetAutoSelect = true;
  183. FCopyParamDefault = L"";
  184. FSynchronizeController = NULL;
  185. FPendingQueueActionItem = NULL;
  186. FLockLevel = 0;
  187. FLockSuspendLevel = 0;
  188. FDisabledOnLockSuspend = false;
  189. FAlternativeDelete = false;
  190. FTrayIcon = new ::TTrayIcon(0);
  191. FTrayIcon->OnClick = TrayIconClick;
  192. FMaxQueueLength = 0;
  193. FLastContextPopupScreenPoint = TPoint(-1, -1);
  194. FTransferResumeList = NULL;
  195. FMoveToQueue = false;
  196. FStandaloneEditing = false;
  197. FOnFeedSynchronizeError = NULL;
  198. FNeedSession = false;
  199. FDoNotIdleCurrentTerminal = 0;
  200. FEditorManager = new TEditorManager();
  201. FEditorManager->OnFileChange = ExecutedFileChanged;
  202. FEditorManager->OnFileReload = ExecutedFileReload;
  203. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  204. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  205. FLocalEditors = new TList();
  206. FQueueStatus = NULL;
  207. FQueueStatusSection = new TCriticalSection();
  208. FQueueStatusInvalidated = false;
  209. FQueueItemInvalidated = false;
  210. FQueueActedItem = NULL;
  211. FQueueController = new TQueueController(QueueView3);
  212. FUserActionTimer = new TTimer(this);
  213. FUserActionTimer->Enabled = false;
  214. FUserActionTimer->Interval = 10;
  215. FUserActionTimer->OnTimer = UserActionTimer;
  216. FNotes = new TStringList();
  217. FNoteTimer = new TTimer(this);
  218. FNoteTimer->Enabled = false;
  219. FNoteTimer->OnTimer = NoteTimer;
  220. FOnNoteClick = NULL;
  221. FOle32Library = LoadLibrary(L"Ole32.dll");
  222. FDragMoveCursor = FOle32Library != NULL ?
  223. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  224. UseSystemSettings(this);
  225. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  226. static_cast<TObject*>(GetComponent(fcTransferList)));
  227. DebugAssert(TransferList != NULL);
  228. FTransferListHoverIndex = -1;
  229. TransferList->OnChange = TransferListChange;
  230. TransferList->OnDrawItem = TransferListDrawItem;
  231. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))));
  232. SetSubmenu(NonVisualDataModule->ColorMenuItem);
  233. UseDesktopFont(SessionsPageControl);
  234. UpdateSessionsPageControlHeight();
  235. UseDesktopFont(RemoteDirView);
  236. UseDesktopFont(RemoteDriveView);
  237. UseDesktopFont(QueueView3);
  238. UseDesktopFont(QueueLabel);
  239. UseDesktopFont(RemoteStatusBar);
  240. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  241. QueueSplitter->ShowHint = true;
  242. RemotePanelSplitter->ShowHint = true;
  243. UpdateImages();
  244. FCustomCommandMenu = CreateTBXPopupMenu(this);
  245. FCustomCommandLocalFileList = NULL;
  246. FCustomCommandRemoteFileList = NULL;
  247. FSessionColors = new TPngImageList(this);
  248. FSessionColors->ColorDepth = cd32Bit;
  249. AddFixedSessionImages();
  250. SessionsPageControl->Images = FSessionColors;
  251. QueueLabel->FocusControl = QueueView3;
  252. UpdateQueueLabel();
  253. FRemoteDirViewWasFocused = true;
  254. CreateHiddenWindow();
  255. StartUpdates();
  256. }
  257. //---------------------------------------------------------------------------
  258. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  259. {
  260. // this has to be one of the very first things to do
  261. StopUpdateThread();
  262. SessionsPageControl->Images = NULL;
  263. SAFE_DESTROY(FSessionColors);
  264. SAFE_DESTROY(FSessionsDragDropFilesEx);
  265. delete FCustomCommandLocalFileList;
  266. delete FCustomCommandRemoteFileList;
  267. delete FCustomCommandMenu;
  268. delete FTrayIcon;
  269. FTrayIcon = NULL;
  270. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  271. delete FEditorManager;
  272. ForceCloseLocalEditors();
  273. delete FLocalEditors;
  274. HideFileFindDialog();
  275. if (FDelayedDeletionTimer)
  276. {
  277. DoDelayedDeletion(NULL);
  278. SAFE_DESTROY(FDelayedDeletionTimer);
  279. }
  280. SAFE_DESTROY(FDelayedDeletionList);
  281. // sometimes we do not get DDEnd so the list is not released
  282. SAFE_DESTROY(FDDFileList);
  283. DebugAssert(FSynchronizeController == NULL);
  284. CloseHandle(FDDExtMutex);
  285. FDDExtMutex = NULL;
  286. FreeLibrary(FOle32Library);
  287. FOle32Library = NULL;
  288. FDragMoveCursor = NULL;
  289. DebugAssert(!FErrorList);
  290. if (FEverShown)
  291. {
  292. // when window is never shown (like when running command-line operation),
  293. // particularly window site is not restored correctly (BoundsRect value set
  294. // in RestoreForm gets lost during handle allocation), so we do not want
  295. // it to be stored
  296. StoreParams();
  297. }
  298. Terminal = NULL;
  299. Queue = NULL;
  300. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  301. NonVisualDataModule->ScpExplorer = NULL;
  302. delete FQueueController;
  303. FQueueController = NULL;
  304. delete FQueueStatusSection;
  305. FQueueStatusSection = NULL;
  306. delete FQueueStatus;
  307. FQueueStatus = NULL;
  308. delete FUserActionTimer;
  309. FUserActionTimer = NULL;
  310. delete FNoteTimer;
  311. delete FNotes;
  312. delete FNoteData;
  313. SAFE_DESTROY(FDocks);
  314. SAFE_DESTROY(FHistoryMenu[0][0]);
  315. SAFE_DESTROY(FHistoryMenu[0][1]);
  316. SAFE_DESTROY(FHistoryMenu[1][0]);
  317. SAFE_DESTROY(FHistoryMenu[1][1]);
  318. if (FHiddenWindow != NULL)
  319. {
  320. DestroyWindow(FHiddenWindow);
  321. FHiddenWindow = NULL;
  322. }
  323. }
  324. //---------------------------------------------------------------------------
  325. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  326. {
  327. std::unique_ptr<TSessionData> Data;
  328. if (!Session.IsEmpty())
  329. {
  330. bool DefaultsOnly;
  331. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  332. }
  333. TTerminalManager * Manager = TTerminalManager::Instance();
  334. for (int Index = 0; Index < Manager->Count; Index++)
  335. {
  336. TTerminal * Terminal = Manager->Terminals[Index];
  337. if (Session.IsEmpty() ||
  338. Terminal->SessionData->IsSameSite(Data.get()))
  339. {
  340. if (Path.IsEmpty())
  341. {
  342. Terminal->ClearCaches();
  343. }
  344. else
  345. {
  346. Terminal->DirectoryModified(Path, true);
  347. }
  348. }
  349. }
  350. // We should flag a pending refresh for the background terminals or busy foreground terminals
  351. if ((Terminal != NULL) && Terminal->Active &&
  352. CanCommandLineFromAnotherInstance() &&
  353. (Session.IsEmpty() || Terminal->SessionData->IsSameSite(Data.get())) &&
  354. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  355. {
  356. Terminal->ReloadDirectory();
  357. }
  358. }
  359. //---------------------------------------------------------------------------
  360. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  361. {
  362. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  363. size_t MessageSize = sizeof(TCopyDataMessage);
  364. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  365. if (Result)
  366. {
  367. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  368. Result = (Message.Version == TCopyDataMessage::Version1);
  369. if (Result)
  370. {
  371. switch (Message.Command)
  372. {
  373. case TCopyDataMessage::CommandCanCommandLine:
  374. Result = CanCommandLineFromAnotherInstance();
  375. break;
  376. case TCopyDataMessage::CommandCommandLine:
  377. {
  378. UnicodeString CommandLine(Message.CommandLine);
  379. Result = CommandLineFromAnotherInstance(CommandLine);
  380. }
  381. break;
  382. case TCopyDataMessage::RefreshPanel:
  383. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  384. break;
  385. case TCopyDataMessage::MainWindowCheck:
  386. Result = true;
  387. break;
  388. default:
  389. Result = false;
  390. break;
  391. }
  392. }
  393. }
  394. Message.Result = Result ? 1 : 0;
  395. }
  396. //---------------------------------------------------------------------------
  397. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  398. {
  399. WNDCLASS WindowClass = {0};
  400. WindowClass.lpfnWndProc = DefWindowProc;
  401. WindowClass.hInstance = HInstance;
  402. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  403. FHiddenWindow = NULL;
  404. if (RegisterClass(&WindowClass))
  405. {
  406. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  407. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  408. }
  409. }
  410. //---------------------------------------------------------------------------
  411. bool __fastcall TCustomScpExplorerForm::CanConsole()
  412. {
  413. return (Terminal != NULL) && (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  414. }
  415. //---------------------------------------------------------------------------
  416. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  417. {
  418. bool Result = Visible && !NonVisualDataModule->Busy;
  419. return Result;
  420. }
  421. //---------------------------------------------------------------------------
  422. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  423. const UnicodeString & CommandLine)
  424. {
  425. TProgramParams Params(CommandLine);
  426. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  427. if (Result)
  428. {
  429. NonVisualDataModule->StartBusy();
  430. try
  431. {
  432. // this action is initiated from another process,
  433. // so it's likely that our window is not visible,
  434. // and user won't see what is going on
  435. Application->BringToFront();
  436. // reload sessions as we may be asked to open a session
  437. // just stored by another instance
  438. StoredSessions->Reload();
  439. UnicodeString SessionName = Params.Param[1];
  440. std::unique_ptr<TObjectList> DataList(new TObjectList());
  441. UnicodeString DownloadFile; // unused
  442. try
  443. {
  444. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this);
  445. }
  446. catch (EAbort &)
  447. {
  448. // Happens when opening session in PuTTY and in other situations,
  449. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  450. // to process the command-line on its own (or to pass it to yet another instance)
  451. }
  452. if (DataList->Count > 0)
  453. {
  454. TTerminalManager * Manager = TTerminalManager::Instance();
  455. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  456. }
  457. }
  458. __finally
  459. {
  460. NonVisualDataModule->EndBusy();
  461. }
  462. }
  463. return Result;
  464. }
  465. //---------------------------------------------------------------------------
  466. void __fastcall TCustomScpExplorerForm::SetTerminal(TTerminal * value)
  467. {
  468. if (FTerminal != value)
  469. {
  470. TerminalChanging();
  471. ReplaceTerminal(value);
  472. }
  473. }
  474. //---------------------------------------------------------------------------
  475. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TTerminal * value)
  476. {
  477. FTerminal = value;
  478. bool PrevAllowTransferPresetAutoSelect = FAllowTransferPresetAutoSelect;
  479. FAllowTransferPresetAutoSelect = false;
  480. try
  481. {
  482. TerminalChanged();
  483. }
  484. __finally
  485. {
  486. FAllowTransferPresetAutoSelect = PrevAllowTransferPresetAutoSelect;
  487. }
  488. if (Terminal != NULL)
  489. {
  490. TransferPresetAutoSelect();
  491. }
  492. }
  493. //---------------------------------------------------------------------------
  494. void __fastcall TCustomScpExplorerForm::TerminalChanging()
  495. {
  496. if (FTerminal != NULL)
  497. {
  498. UpdateTerminal(Terminal);
  499. }
  500. }
  501. //---------------------------------------------------------------------------
  502. void __fastcall TCustomScpExplorerForm::TerminalChanged()
  503. {
  504. RemoteDirView->Terminal = Terminal;
  505. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  506. TManagedTerminal * ManagedTerminal = NULL;
  507. if (Terminal != NULL)
  508. {
  509. if (Terminal->Active)
  510. {
  511. Terminal->RefreshDirectory();
  512. }
  513. ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  514. DebugAssert(ManagedTerminal != NULL);
  515. if (WinConfiguration->PreservePanelState)
  516. {
  517. if (ManagedTerminal->RemoteExplorerState != NULL)
  518. {
  519. RemoteDirView->RestoreState(ManagedTerminal->RemoteExplorerState);
  520. }
  521. else
  522. {
  523. RemoteDirView->ClearState();
  524. }
  525. }
  526. InitStatusBar();
  527. }
  528. DoTerminalListChanged();
  529. if (ManagedTerminal != NULL)
  530. {
  531. // this has to be set only after the tab is switched from DoTerminalListChanged,
  532. // otherwise we are changing color of wrong tab
  533. SessionColor = (TColor)ManagedTerminal->StateData->Color;
  534. }
  535. UpdateTransferList();
  536. // Update panels Enable state before refreshing the labels
  537. UpdateControls();
  538. }
  539. //---------------------------------------------------------------------------
  540. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  541. {
  542. if (Queue != value)
  543. {
  544. if (FQueue != NULL)
  545. {
  546. FQueue->OnListUpdate = NULL;
  547. FQueue->OnQueueItemUpdate = NULL;
  548. }
  549. FQueue = value;
  550. if (FQueue != NULL)
  551. {
  552. DebugAssert(FQueue->OnListUpdate == NULL);
  553. FQueue->OnListUpdate = QueueListUpdate;
  554. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  555. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  556. }
  557. QueueChanged();
  558. }
  559. }
  560. //---------------------------------------------------------------------------
  561. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  562. TListItem * Item)
  563. {
  564. if (FQueueActedItem == Item)
  565. {
  566. FQueueActedItem = NULL;
  567. if ((QueueView3->PopupMenu != NULL) &&
  568. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  569. {
  570. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  571. ReleaseCapture();
  572. }
  573. }
  574. if (Item->Data == FPendingQueueActionItem)
  575. {
  576. FPendingQueueActionItem = NULL;
  577. }
  578. }
  579. //---------------------------------------------------------------------------
  580. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  581. {
  582. {
  583. TGuard Guard(FQueueStatusSection);
  584. FQueueStatusInvalidated = false;
  585. if (FQueue != NULL)
  586. {
  587. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  588. }
  589. }
  590. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  591. {
  592. FMaxQueueLength = FQueueStatus->Count;
  593. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  594. }
  595. FQueueController->UpdateQueueStatus(FQueueStatus);
  596. SetQueueProgress();
  597. UpdateQueueView();
  598. bool IsEmpty = (FQueueStatus == NULL) || (FQueueStatus->Count == 0);
  599. if (IsEmpty && (Terminal != NULL))
  600. {
  601. TOnceDoneOperation OnceDoneOperation =
  602. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  603. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  604. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  605. {
  606. FQueue->Enabled = false;
  607. }
  608. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  609. {
  610. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  611. }
  612. }
  613. }
  614. //---------------------------------------------------------------------------
  615. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  616. {
  617. UnicodeString Result;
  618. if (FQueueStatus != NULL)
  619. {
  620. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  621. {
  622. TFileOperationProgressType * ProgressData =
  623. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  624. if ((ProgressData != NULL) && ProgressData->InProgress)
  625. {
  626. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  627. }
  628. }
  629. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  630. {
  631. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  632. }
  633. }
  634. return Result;
  635. }
  636. //---------------------------------------------------------------------------
  637. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  638. {
  639. ComponentVisible[fcQueueView] =
  640. (WinConfiguration->QueueView.Show == qvShow) ||
  641. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  642. (FQueueStatus != NULL) && (FQueueStatus->Count > 0));
  643. }
  644. //---------------------------------------------------------------------------
  645. void __fastcall TCustomScpExplorerForm::QueueChanged()
  646. {
  647. if (FQueueStatus != NULL)
  648. {
  649. delete FQueueStatus;
  650. FQueueStatus = NULL;
  651. }
  652. UpdateQueueStatus(true);
  653. }
  654. //---------------------------------------------------------------------------
  655. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  656. {
  657. if (FQueue == Queue)
  658. {
  659. FQueueStatusInvalidated = true;
  660. }
  661. }
  662. //---------------------------------------------------------------------------
  663. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  664. TQueueItem * Item)
  665. {
  666. if (FQueue == Queue)
  667. {
  668. TGuard Guard(FQueueStatusSection);
  669. // this may be running in parallel with QueueChanged
  670. if (FQueueStatus != NULL)
  671. {
  672. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  673. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  674. {
  675. FRefreshLocalDirectory = (QueueItem == NULL) ||
  676. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  677. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  678. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  679. }
  680. if (QueueItem != NULL)
  681. {
  682. QueueItem->UserData = (void*)true;
  683. FQueueItemInvalidated = true;
  684. }
  685. }
  686. }
  687. }
  688. //---------------------------------------------------------------------------
  689. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  690. {
  691. // during standalone editing, we have no way to see/control queue,
  692. // so we have to always popup prompts automatically
  693. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  694. }
  695. //---------------------------------------------------------------------------
  696. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  697. {
  698. if (FQueueStatus != NULL)
  699. {
  700. bool QueueAutoPopup = IsQueueAutoPopup();
  701. bool NeedRefresh = FQueueController->NeedRefresh();
  702. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  703. FQueueItemInvalidated = false;
  704. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  705. bool Updated = false;
  706. TQueueItemProxy * QueueItem;
  707. bool WasUserAction;
  708. for (int Index = 0; Index < Limit; Index++)
  709. {
  710. bool Update = false;
  711. QueueItem = FQueueStatus->Items[Index];
  712. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  713. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  714. (FPendingQueueActionItem == NULL))
  715. {
  716. FPendingQueueActionItem = QueueItem;
  717. FUserActionTimer->Enabled = true;
  718. }
  719. if ((bool)QueueItem->UserData)
  720. {
  721. QueueItem->UserData = (void*)false;
  722. QueueItem->Update();
  723. Updated = true;
  724. Update = true;
  725. }
  726. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  727. {
  728. Update = true;
  729. }
  730. if (Update)
  731. {
  732. FQueueController->RefreshQueueItem(QueueItem);
  733. }
  734. }
  735. if (Updated)
  736. {
  737. NonVisualDataModule->UpdateNonVisibleActions();
  738. SetQueueProgress();
  739. }
  740. }
  741. }
  742. //---------------------------------------------------------------------------
  743. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  744. {
  745. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  746. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  747. }
  748. //---------------------------------------------------------------------------
  749. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  750. {
  751. if (ProgressData->Operation != foCalculateSize)
  752. {
  753. int OverallProgress;
  754. // FProgressForm is null when this is called from SetQueueProgress
  755. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  756. {
  757. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  758. }
  759. else
  760. {
  761. OverallProgress = ProgressData->OverallProgress();
  762. }
  763. // implies TBPF_NORMAL
  764. FTaskbarList->SetProgressValue(GetMainForm()->Handle, OverallProgress, 100);
  765. }
  766. else
  767. {
  768. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  769. }
  770. }
  771. //---------------------------------------------------------------------------
  772. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  773. {
  774. TTerminalManager::Instance()->QueueStatusUpdated();
  775. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  776. {
  777. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  778. {
  779. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  780. {
  781. TFileOperationProgressType * ProgressData = NULL;
  782. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  783. {
  784. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  785. }
  786. if ((ProgressData != NULL) &&
  787. ProgressData->InProgress)
  788. {
  789. SetTaskbarListProgressValue(ProgressData);
  790. }
  791. else
  792. {
  793. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  794. }
  795. }
  796. else
  797. {
  798. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  799. }
  800. }
  801. else
  802. {
  803. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  804. }
  805. }
  806. UpdateQueueLabel();
  807. }
  808. //---------------------------------------------------------------------------
  809. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  810. {
  811. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  812. if (FQueueStatus != NULL)
  813. {
  814. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  815. if (ActiveAndPendingPrimaryCount > 0)
  816. {
  817. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  818. }
  819. }
  820. QueueLabel->Caption = Caption;
  821. }
  822. //---------------------------------------------------------------------------
  823. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  824. {
  825. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  826. static_cast<TComponent*>(GetComponent(fcTransferList)));
  827. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  828. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  829. TransferList->Strings->BeginUpdate();
  830. try
  831. {
  832. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  833. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  834. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  835. if (FTransferDropDownHint.IsEmpty())
  836. {
  837. FTransferDropDownHint = TransferDropDown->Hint;
  838. }
  839. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  840. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  841. TransferDropDown->Text = StripHotkey(Name);
  842. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  843. (FTransferDropDownHint, StripHotkey(Name),
  844. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  845. FLAGMASK(Terminal != NULL, Terminal->UsableCopyParamAttrs(0).General))));
  846. // update the label, otherwise when it is updated only on the first draw
  847. // of the list, it is drawn "bold" for some reason
  848. FTransferListHoverIndex = TransferList->ItemIndex;
  849. UpdateTransferLabel();
  850. }
  851. __finally
  852. {
  853. TransferList->Strings->EndUpdate();
  854. }
  855. }
  856. //---------------------------------------------------------------------------
  857. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  858. {
  859. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  860. DebugAssert(Toolbar != NULL);
  861. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  862. }
  863. //---------------------------------------------------------------------------
  864. void __fastcall TCustomScpExplorerForm::UpdateActions()
  865. {
  866. TForm::UpdateActions();
  867. if (ComponentVisible[fcCustomCommandsBand])
  868. {
  869. UpdateCustomCommandsToolbar();
  870. }
  871. }
  872. //---------------------------------------------------------------------------
  873. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  874. {
  875. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  876. }
  877. //---------------------------------------------------------------------------
  878. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  879. {
  880. DebugAssert(Configuration && RemoteDirView);
  881. RemoteDirView->DDAllowMove = WinConfiguration->DDAllowMoveInit;
  882. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  883. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  884. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  885. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  886. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  887. if (RemoteDirView->RowSelect != WinConfiguration->FullRowSelect)
  888. {
  889. RemoteDirView->RowSelect = WinConfiguration->FullRowSelect;
  890. // selection is not redrawn automatically when RowSelect changes
  891. RemoteDirView->Invalidate();
  892. }
  893. RemoteDriveView->DDAllowMove = WinConfiguration->DDAllowMoveInit;
  894. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  895. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  896. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  897. UpdateSessionsPageControlHeight();
  898. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  899. UpdateToolbarDisplayMode();
  900. UpdateTransferList();
  901. if (Terminal != NULL)
  902. {
  903. TransferPresetAutoSelect();
  904. }
  905. UpdateQueueView();
  906. UpdateControls();
  907. // this can be called even before constuctor finishes.
  908. if (FEditorManager != NULL)
  909. {
  910. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  911. }
  912. // this can be called even before constuctor finishes.
  913. if (FLocalEditors != NULL)
  914. {
  915. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  916. {
  917. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  918. }
  919. }
  920. if (ComponentVisible[fcCustomCommandsBand])
  921. {
  922. UpdateCustomCommandsToolbar();
  923. }
  924. // show only when keeping queue items forever,
  925. // otherwise, its enough to have in in the context menu
  926. QueueDeleteAllDoneQueueToolbarItem->Visible =
  927. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  928. }
  929. //---------------------------------------------------------------------------
  930. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  931. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  932. void * /*Arg*/)
  933. {
  934. if (Token != NULL)
  935. {
  936. TForm * Editor = dynamic_cast<TForm*>(Token);
  937. DebugAssert(Editor != NULL);
  938. ReconfigureEditorForm(Editor);
  939. }
  940. }
  941. //---------------------------------------------------------------------------
  942. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  943. {
  944. WinConfiguration->DDTransferConfirmation = asOn;
  945. }
  946. //---------------------------------------------------------------------------
  947. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  948. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  949. {
  950. // noop
  951. }
  952. //---------------------------------------------------------------------------
  953. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  954. TTransferDirection Direction, TTransferType Type, bool Temp,
  955. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  956. bool Confirm, bool DragDrop, int Options)
  957. {
  958. bool Result = true;
  959. DebugAssert(Terminal && Terminal->Active);
  960. // Temp means d&d here so far, may change in future!
  961. if (Temp && (Direction == tdToLocal) && (Type == ttMove) &&
  962. !WinConfiguration->DDAllowMove)
  963. {
  964. TMessageParams Params(mpNeverAskAgainCheck);
  965. unsigned int Answer = MessageDialog(LoadStr(DND_DOWNLOAD_MOVE_WARNING), qtWarning,
  966. qaOK | qaCancel, HELP_DND_DOWNLOAD_MOVE_WARNING, &Params);
  967. if (Answer == qaNeverAskAgain)
  968. {
  969. WinConfiguration->DDAllowMove = true;
  970. }
  971. else if (Answer == qaCancel)
  972. {
  973. Result = false;
  974. }
  975. }
  976. // these parameters are known in advance
  977. int Params =
  978. FLAGMASK(Type == ttMove, cpDelete);
  979. bool ToTemp = (Temp && (Direction == tdToLocal));
  980. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  981. {
  982. bool DisableNewerOnly =
  983. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  984. ToTemp;
  985. Options |=
  986. FLAGMASK(ToTemp, coTemp) |
  987. FLAGMASK(!Terminal->IsCapable[fsBackgroundTransfers], coDisableQueue) |
  988. coDoNotShowAgain;
  989. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  990. int CopyParamAttrs = (Direction == tdToRemote ?
  991. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  992. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  993. int OutputOptions =
  994. FLAGMASK(DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto),
  995. cooDoNotShowAgain);
  996. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  997. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  998. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  999. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions);
  1000. if (Result)
  1001. {
  1002. if (FLAGSET(OutputOptions, cooDoNotShowAgain))
  1003. {
  1004. if (DragDrop)
  1005. {
  1006. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1007. {
  1008. PopupTrayBalloon(NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation,
  1009. NULL, 0, EnableDDTransferConfirmation, NULL);
  1010. }
  1011. WinConfiguration->DDTransferConfirmation = asOff;
  1012. }
  1013. else
  1014. {
  1015. WinConfiguration->ConfirmTransferring = false;
  1016. }
  1017. }
  1018. else
  1019. {
  1020. // User exclicitly unchecked "do not show again",
  1021. // so show him the dialog the next time
  1022. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1023. {
  1024. WinConfiguration->DDTransferConfirmation = asOn;
  1025. }
  1026. }
  1027. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1028. }
  1029. }
  1030. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fsBackgroundTransfers])
  1031. {
  1032. Configuration->Usage->Inc(L"TransfersOnBackground");
  1033. // these parameter are known only after transfer dialog
  1034. Params |=
  1035. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1036. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1037. Result = false;
  1038. ClearTransferSourceSelection(Direction);
  1039. }
  1040. return Result;
  1041. }
  1042. //---------------------------------------------------------------------------
  1043. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1044. {
  1045. if (FOnFileOperationFinished != NULL)
  1046. {
  1047. FOnFileOperationFinished(UnicodeString(), true);
  1048. }
  1049. else
  1050. {
  1051. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1052. if (HasDirView[Side])
  1053. {
  1054. DirView(Side)->SelectAll(smNone);
  1055. }
  1056. }
  1057. }
  1058. //---------------------------------------------------------------------------
  1059. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1060. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1061. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1062. int Params)
  1063. {
  1064. DebugAssert(Queue != NULL);
  1065. bool SingleFile = false;
  1066. if (FileList->Count == 1)
  1067. {
  1068. if (Direction == tdToRemote)
  1069. {
  1070. UnicodeString FileName = FileList->Strings[0];
  1071. SingleFile = FileExists(ApiPath(FileName));
  1072. }
  1073. else
  1074. {
  1075. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1076. SingleFile = !File->IsDirectory;
  1077. }
  1078. }
  1079. TQueueItem * QueueItem;
  1080. if (Direction == tdToRemote)
  1081. {
  1082. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1083. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1084. }
  1085. else
  1086. {
  1087. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1088. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1089. }
  1090. AddQueueItem(Queue, QueueItem, Terminal);
  1091. }
  1092. //---------------------------------------------------------------------------
  1093. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TTerminal * Terminal)
  1094. {
  1095. if (Queue->IsEmpty)
  1096. {
  1097. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  1098. ManagedTerminal->QueueOperationStart = Now();
  1099. }
  1100. Queue->AddItem(QueueItem);
  1101. }
  1102. //---------------------------------------------------------------------------
  1103. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1104. {
  1105. }
  1106. //---------------------------------------------------------------------------
  1107. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1108. {
  1109. DebugAssert(FDocks != NULL);
  1110. for (int Index = 0; Index < ComponentCount; Index++)
  1111. {
  1112. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1113. if ((Dock != NULL) && (Dock->Tag == 0))
  1114. {
  1115. FDocks->Add(Dock);
  1116. }
  1117. }
  1118. CollectItemsWithTextDisplayMode(this);
  1119. ConfigurationChanged();
  1120. QueuePanel->Height =
  1121. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1122. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1123. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1124. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1125. }
  1126. //---------------------------------------------------------------------------
  1127. void __fastcall TCustomScpExplorerForm::StoreParams()
  1128. {
  1129. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1130. WinConfiguration->QueueView.HeightPixelsPerInch = GetControlPixelsPerInch(this);
  1131. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1132. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1133. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1134. }
  1135. //---------------------------------------------------------------------------
  1136. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1137. {
  1138. if (!FFormRestored)
  1139. {
  1140. FFormRestored = true;
  1141. RestoreFormParams();
  1142. }
  1143. TForm::CreateParams(Params);
  1144. }
  1145. //---------------------------------------------------------------------------
  1146. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1147. {
  1148. DebugAssert(FDocks != NULL);
  1149. for (int Index = 0; Index < FDocks->Count; Index++)
  1150. {
  1151. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1152. }
  1153. }
  1154. //---------------------------------------------------------------------------
  1155. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1156. {
  1157. SetDockAllowDrag(true);
  1158. while (!ButtonsStr.IsEmpty())
  1159. {
  1160. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1161. UnicodeString Name = CutToChar(S, L'=', true);
  1162. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1163. {
  1164. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1165. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1166. {
  1167. while (!S.IsEmpty())
  1168. {
  1169. UnicodeString S2 = CutToChar(S, L',', true);
  1170. UnicodeString Name = CutToChar(S2, L':', true);
  1171. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1172. {
  1173. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1174. if (SameText(GetToolbarItemName(Item), Name))
  1175. {
  1176. // forward compatibility
  1177. UnicodeString S3 = CutToChar(S2, L':', true);
  1178. bool Visible = StrToIntDef(S3, 0);
  1179. Item->Visible = Visible;
  1180. }
  1181. }
  1182. }
  1183. }
  1184. }
  1185. }
  1186. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1187. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1188. }
  1189. //---------------------------------------------------------------------------
  1190. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1191. {
  1192. UnicodeString Result;
  1193. SetDockAllowDrag(true);
  1194. Result = ::GetToolbarsLayoutStr(this);
  1195. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1196. return Result;
  1197. }
  1198. //---------------------------------------------------------------------------
  1199. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1200. {
  1201. UnicodeString Result;
  1202. if (Item->Action != NULL)
  1203. {
  1204. Result = Item->Action->Name;
  1205. Result = RemoveSuffix(Result, L"Action");
  1206. }
  1207. else
  1208. {
  1209. Result = Item->Name;
  1210. Result = RemoveSuffix(Result, L"SubmenuItem");
  1211. Result = RemoveSuffix(Result, L"Item");
  1212. }
  1213. return Result;
  1214. }
  1215. //---------------------------------------------------------------------------
  1216. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1217. {
  1218. UnicodeString Result;
  1219. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1220. {
  1221. UnicodeString ToolbarStr;
  1222. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1223. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1224. {
  1225. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1226. {
  1227. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1228. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1229. // Once we introduce any buttons that aer hidden by default, we would have to remember their initial state
  1230. // and save the changes here only.
  1231. if (!Item->Visible)
  1232. {
  1233. UnicodeString Name = GetToolbarItemName(Item);
  1234. DebugAssert(Name.Pos(L"TBX") == 0);
  1235. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1236. }
  1237. }
  1238. }
  1239. if (!ToolbarStr.IsEmpty())
  1240. {
  1241. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1242. }
  1243. }
  1244. return Result;
  1245. }
  1246. //---------------------------------------------------------------------------
  1247. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1248. {
  1249. DebugAssert(FProgressForm == NULL);
  1250. FProgressForm =
  1251. new TProgressForm(Application, (FTransferResumeList != NULL), Terminal->IsCapable[fsSkipTransfer], SynchronizeProgress);
  1252. FProgressForm->DeleteLocalToRecycleBin =
  1253. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1254. FProgressForm->DeleteRemoteToRecycleBin =
  1255. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1256. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1257. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1258. LockWindow();
  1259. if (FTaskbarList != NULL)
  1260. {
  1261. // Actually, do not know what hides the progress once the operation finishes
  1262. SetTaskbarListProgressState(TBPF_NORMAL);
  1263. }
  1264. }
  1265. //---------------------------------------------------------------------------
  1266. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1267. {
  1268. UnlockWindow();
  1269. SAFE_DESTROY(FProgressForm);
  1270. SetQueueProgress();
  1271. }
  1272. //---------------------------------------------------------------------------
  1273. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1274. TFileOperationProgressType & ProgressData)
  1275. {
  1276. // Download to temporary local directory
  1277. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1278. {
  1279. bool Continue = true;
  1280. FPendingTempSpaceWarn = false;
  1281. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1282. if (!Continue)
  1283. {
  1284. Abort();
  1285. }
  1286. }
  1287. // operation is being executed and we still didn't show up progress form
  1288. if (ProgressData.InProgress && !FProgressForm)
  1289. {
  1290. CreateProgressForm(NULL);
  1291. // When main window is hidden, synchronisation form does not exist (we do not get here during synchronization - only comparing),
  1292. // we suppose "/upload" or URL download mode
  1293. if (!Visible && (ProgressData.Operation != foCalculateSize) &&
  1294. (ProgressData.Operation != foCalculateChecksum) &&
  1295. (FSynchronizeProgressForm == NULL) &&
  1296. !FStandaloneEditing)
  1297. {
  1298. FProgressForm->OnceDoneOperation = odoDisconnect;
  1299. }
  1300. }
  1301. // operation is finished (or terminated), so we hide progress form
  1302. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1303. {
  1304. DestroyProgressForm();
  1305. if (ProgressData.Operation == foCalculateSize)
  1306. {
  1307. // When calculating size before transfer, the abort caused by
  1308. // cancel flag set due to "MoveToQueue" below,
  1309. // is not propagated back to us in ExecuteFileOperation,
  1310. // as it is silently swallowed.
  1311. // So we explicitly (re)throw it here.
  1312. if (FMoveToQueue)
  1313. {
  1314. Abort();
  1315. }
  1316. }
  1317. else
  1318. {
  1319. if ((ProgressData.Cancel == csContinue) &&
  1320. !FAutoOperation)
  1321. {
  1322. OperationComplete(ProgressData.StartTime);
  1323. }
  1324. }
  1325. }
  1326. if (FProgressForm &&
  1327. // Can happen only during synchronization, when ending one of the operations
  1328. ProgressData.InProgress)
  1329. {
  1330. FProgressForm->SetProgressData(ProgressData);
  1331. if (FTaskbarList != NULL)
  1332. {
  1333. DebugAssert(ProgressData.InProgress);
  1334. SetTaskbarListProgressValue(&ProgressData);
  1335. }
  1336. if (FProgressForm->Cancel > csContinue)
  1337. {
  1338. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1339. if (FProgressForm->Cancel == csCancelFile)
  1340. {
  1341. FProgressForm->ClearCancel();
  1342. }
  1343. // cancel cancels even the move
  1344. FMoveToQueue = false;
  1345. }
  1346. else if (FProgressForm->MoveToQueue)
  1347. {
  1348. FMoveToQueue = true;
  1349. ProgressData.SetCancelAtLeast(csCancel);
  1350. }
  1351. if ((FTransferResumeList != NULL) &&
  1352. ProgressData.InProgress &&
  1353. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1354. !ProgressData.FullFileName.IsEmpty())
  1355. {
  1356. if ((FTransferResumeList->Count == 0) ||
  1357. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1358. {
  1359. // note that we do not recognize directories from files here
  1360. FTransferResumeList->Add(ProgressData.FullFileName);
  1361. }
  1362. }
  1363. }
  1364. }
  1365. //---------------------------------------------------------------------------
  1366. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1367. const TDateTime & StartTime)
  1368. {
  1369. if (GUIConfiguration->BeepOnFinish &&
  1370. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1371. {
  1372. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1373. DWORD Sound;
  1374. if (!ExtractFileExt(BeepSound).IsEmpty())
  1375. {
  1376. Sound = SND_FILENAME;
  1377. }
  1378. else
  1379. {
  1380. Sound = SND_ALIAS;
  1381. }
  1382. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1383. }
  1384. }
  1385. //---------------------------------------------------------------------------
  1386. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1387. TFileOperationProgressType & ProgressData)
  1388. {
  1389. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1390. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1391. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1392. FileOperationProgress(ProgressData);
  1393. }
  1394. //---------------------------------------------------------------------------
  1395. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1396. {
  1397. UnicodeString Result;
  1398. if (FProgressForm != NULL)
  1399. {
  1400. Result = FProgressForm->ProgressStr();
  1401. }
  1402. return Result;
  1403. }
  1404. //---------------------------------------------------------------------------
  1405. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1406. bool DragDrop)
  1407. {
  1408. return (!DragDrop && (DropSourceControl == NULL)) ||
  1409. (DropSourceControl == RemoteDirView);
  1410. }
  1411. //---------------------------------------------------------------------------
  1412. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1413. TFileOperation Operation, TOperationSide Side,
  1414. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1415. TOnceDoneOperation & OnceDoneOperation)
  1416. {
  1417. if (!FAutoOperation)
  1418. {
  1419. // no selection on "/upload", form servers only as event handler
  1420. // (it is not displayed)
  1421. if (PanelOperation(Side, FDragDropOperation) &&
  1422. Visible && (Operation != foCalculateSize) &&
  1423. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1424. {
  1425. if (FOnFileOperationFinished != NULL)
  1426. {
  1427. FOnFileOperationFinished(FileName, Success);
  1428. }
  1429. else
  1430. {
  1431. TCustomDirView * DView = DirView(Side);
  1432. UnicodeString FileNameOnly = ExtractFileName(FileName, (Side == osRemote));
  1433. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1434. // this can happen when local drive is unplugged in the middle of the operation
  1435. if (Item != NULL)
  1436. {
  1437. if (Success) Item->Selected = false;
  1438. DView->MakeProgressVisible(Item);
  1439. }
  1440. }
  1441. }
  1442. if ((Operation == foCopy) || (Operation == foMove))
  1443. {
  1444. if (Side == osLocal)
  1445. {
  1446. Configuration->Usage->Inc(L"UploadedFiles");
  1447. }
  1448. else
  1449. {
  1450. Configuration->Usage->Inc(L"DownloadedFiles");
  1451. }
  1452. }
  1453. }
  1454. if (Success && (FSynchronizeController != NULL))
  1455. {
  1456. if (Operation == foCopy)
  1457. {
  1458. DebugAssert(Side == osLocal);
  1459. FSynchronizeController->LogOperation(soUpload, FileName);
  1460. }
  1461. else if (Operation == foDelete)
  1462. {
  1463. DebugAssert(Side == osRemote);
  1464. FSynchronizeController->LogOperation(soDelete, FileName);
  1465. }
  1466. }
  1467. if (FProgressForm)
  1468. {
  1469. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1470. }
  1471. }
  1472. //---------------------------------------------------------------------------
  1473. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1474. TFileOperation Operation, TOperationSide Side,
  1475. bool Temp, const UnicodeString & FileName, Boolean Success,
  1476. TOnceDoneOperation & OnceDoneOperation)
  1477. {
  1478. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1479. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1480. OnceDoneOperation);
  1481. }
  1482. //---------------------------------------------------------------------------
  1483. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1484. {
  1485. DebugAssert(GetSide(Side) == osRemote);
  1486. DebugUsedParam(Side);
  1487. return RemoteDirView;
  1488. }
  1489. //---------------------------------------------------------------------------
  1490. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1491. {
  1492. DebugAssert(GetSide(Side) == osRemote);
  1493. DebugUsedParam(Side);
  1494. return (Terminal != NULL);
  1495. }
  1496. //---------------------------------------------------------------------------
  1497. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1498. TOperationSide Side, int FilesOnly)
  1499. {
  1500. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1501. }
  1502. //---------------------------------------------------------------------------
  1503. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1504. TOperationSide Side, int FilesOnly)
  1505. {
  1506. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1507. }
  1508. //---------------------------------------------------------------------------
  1509. struct THistoryItemData
  1510. {
  1511. short int Side;
  1512. short int Index;
  1513. };
  1514. //---------------------------------------------------------------------------
  1515. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1516. {
  1517. DirView(Side)->HistoryGo(Index);
  1518. }
  1519. //---------------------------------------------------------------------------
  1520. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1521. {
  1522. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1523. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1524. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1525. }
  1526. //---------------------------------------------------------------------------
  1527. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1528. bool Back)
  1529. {
  1530. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1531. {
  1532. TCustomDirView * DView = DirView(Side);
  1533. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1534. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1535. if (ICount > 10)
  1536. {
  1537. ICount = 10;
  1538. }
  1539. Menu->Items->Clear();
  1540. THistoryItemData Data;
  1541. Data.Side = Side;
  1542. for (short int i = 1; i <= ICount; i++)
  1543. {
  1544. TTBCustomItem * Item = new TTBXItem(Menu);
  1545. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1546. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, (Side == osRemote));
  1547. Item->Hint = DView->HistoryPath[Data.Index];
  1548. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1549. Item->Tag = *reinterpret_cast<int*>(&Data);
  1550. Item->OnClick = HistoryItemClick;
  1551. Menu->Items->Add(Item);
  1552. }
  1553. }
  1554. }
  1555. //---------------------------------------------------------------------------
  1556. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1557. TOperationSide Side, bool Back)
  1558. {
  1559. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1560. {
  1561. // workaround
  1562. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1563. // operator new allocates memory in Pascal code, but calls inline
  1564. // contructor in C++, leading in problems, the function does
  1565. // both in Pascal code
  1566. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1567. UpdateHistoryMenu(Side, Back);
  1568. }
  1569. return FHistoryMenu[Side == osLocal][Back];
  1570. }
  1571. //---------------------------------------------------------------------------
  1572. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1573. TCustomDirView *Sender)
  1574. {
  1575. TOperationSide Side = (Sender == DirView(osRemote) ? osRemote : osLocal);
  1576. UpdateHistoryMenu(Side, true);
  1577. UpdateHistoryMenu(Side, false);
  1578. }
  1579. //---------------------------------------------------------------------------
  1580. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1581. {
  1582. // remote custom commands can be executed only if the server supports shell commands
  1583. // or have secondary shell
  1584. return (FTerminal != NULL) && (FTerminal->IsCapable[fcSecondaryShell] || FTerminal->IsCapable[fcShellAnyCommand]);
  1585. }
  1586. //---------------------------------------------------------------------------
  1587. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1588. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1589. {
  1590. int Result;
  1591. TFileCustomCommand RemoteCustomCommand;
  1592. TLocalCustomCommand LocalCustomCommand;
  1593. TFileCustomCommand * NonInteractiveCustomCommand =
  1594. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1595. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1596. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1597. if (FLAGCLEAR(Command.Params, ccLocal))
  1598. {
  1599. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1600. // custom command that does not operate with files can be executed anytime ...
  1601. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1602. {
  1603. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1604. {
  1605. // ... but do not show such command in remote file menu (TODO)
  1606. Result = AllowedState;
  1607. }
  1608. else
  1609. {
  1610. Result = -1;
  1611. }
  1612. }
  1613. else
  1614. {
  1615. if ((ListType == ccltAll) || (ListType == ccltFile))
  1616. {
  1617. Result = ((FCurrentSide == osRemote) && DirView(osRemote)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1618. }
  1619. else
  1620. {
  1621. Result = -1;
  1622. }
  1623. }
  1624. }
  1625. else
  1626. {
  1627. // custom command that does not operate with files can be executed anytime
  1628. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1629. {
  1630. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1631. }
  1632. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1633. {
  1634. if ((ListType == ccltAll) || (ListType == ccltFile))
  1635. {
  1636. // special case is "diff"-style command that can be executed over any side,
  1637. // if we have both sides
  1638. Result =
  1639. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1640. // directly (instead of EnableSelectedOperation) to pass
  1641. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1642. ((HasDirView[osLocal] && DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1643. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1644. }
  1645. else if (ListType == ccltBoth)
  1646. {
  1647. Result = 1;
  1648. }
  1649. else
  1650. {
  1651. Result = -1;
  1652. }
  1653. }
  1654. else
  1655. {
  1656. if ((ListType == ccltAll) || (ListType == ccltFile))
  1657. {
  1658. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1659. }
  1660. else
  1661. {
  1662. Result = -1;
  1663. }
  1664. }
  1665. }
  1666. return Result;
  1667. }
  1668. //---------------------------------------------------------------------------
  1669. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1670. TStrings * FileList, const TCustomCommandType & ACommand,
  1671. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1672. {
  1673. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1674. {
  1675. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1676. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1677. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1678. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1679. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1680. bool Capture =
  1681. FLAGSET(ACommand.Params, ccShowResults) ||
  1682. FLAGSET(ACommand.Params, ccCopyResults) ||
  1683. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1684. TCaptureOutputEvent OutputEvent = NULL;
  1685. DebugAssert(FCapturedLog == NULL);
  1686. if (Capture)
  1687. {
  1688. FCapturedLog = new TStringList();
  1689. OutputEvent = TerminalCaptureLog;
  1690. }
  1691. try
  1692. {
  1693. if (!RemoteCustomCommand.IsFileCommand(Command))
  1694. {
  1695. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1696. OutputEvent);
  1697. }
  1698. else
  1699. {
  1700. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1701. }
  1702. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1703. {
  1704. if (FLAGSET(ACommand.Params, ccCopyResults))
  1705. {
  1706. CopyToClipboard(FCapturedLog);
  1707. }
  1708. if (FLAGSET(ACommand.Params, ccShowResults))
  1709. {
  1710. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1711. }
  1712. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1713. {
  1714. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1715. }
  1716. }
  1717. }
  1718. __finally
  1719. {
  1720. SAFE_DESTROY(FCapturedLog);
  1721. }
  1722. }
  1723. }
  1724. //---------------------------------------------------------------------------
  1725. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1726. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1727. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1728. {
  1729. TStrings * LocalFileList = NULL;
  1730. TStrings * RemoteFileList = NULL;
  1731. TStrings * RemoteFileListFull = NULL;
  1732. try
  1733. {
  1734. if (LocalFileCommand)
  1735. {
  1736. if (ALocalFileList == NULL)
  1737. {
  1738. DebugAssert(HasDirView[osLocal]);
  1739. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1740. // directly (instead of EnableSelectedOperation) to pass
  1741. // false to FocusedFileOnlyWhenFocused
  1742. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1743. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1744. }
  1745. else
  1746. {
  1747. LocalFileList = ALocalFileList;
  1748. }
  1749. if (FileListCommand)
  1750. {
  1751. if (LocalFileList->Count != 1)
  1752. {
  1753. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1754. }
  1755. }
  1756. else
  1757. {
  1758. if ((LocalFileList->Count != 1) &&
  1759. (FileList->Count != 1) &&
  1760. (LocalFileList->Count != FileList->Count))
  1761. {
  1762. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1763. }
  1764. }
  1765. }
  1766. UnicodeString RootTempDir;
  1767. UnicodeString TempDir;
  1768. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1769. if (!RemoteFiles)
  1770. {
  1771. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1772. }
  1773. try
  1774. {
  1775. TDateTimes RemoteFileTimes;
  1776. if (RemoteFiles)
  1777. {
  1778. RemoteFileList = FileList;
  1779. }
  1780. else
  1781. {
  1782. RemoteFileList = new TStringList();
  1783. TMakeLocalFileListParams MakeFileListParam;
  1784. MakeFileListParam.FileList = RemoteFileList;
  1785. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1786. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1787. MakeFileListParam.Recursive =
  1788. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1789. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1790. if (!MakeFileListParam.Recursive)
  1791. {
  1792. RemoteFileTimes.clear();
  1793. RemoteFileListFull = new TStringList();
  1794. MakeFileListParam.FileList = RemoteFileListFull;
  1795. MakeFileListParam.Recursive = true;
  1796. MakeFileListParam.IncludeDirs = false;
  1797. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1798. }
  1799. else
  1800. {
  1801. RemoteFileListFull = RemoteFileList;
  1802. }
  1803. }
  1804. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1805. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1806. if (!NonBlocking)
  1807. {
  1808. Progress.Start(foCustomCommand, osRemote, FileListCommand ? 1 : FileList->Count);
  1809. DebugAssert(FProgressForm != NULL);
  1810. FProgressForm->ReadOnly = true;
  1811. }
  1812. try
  1813. {
  1814. if (FileListCommand)
  1815. {
  1816. UnicodeString LocalFile;
  1817. // MakeFileList does not delimit filenames
  1818. UnicodeString FileList = MakeFileList(RemoteFileList);
  1819. if (LocalFileCommand)
  1820. {
  1821. DebugAssert(LocalFileList->Count == 1);
  1822. LocalFile = LocalFileList->Strings[0];
  1823. }
  1824. TLocalCustomCommand CustomCommand(Data,
  1825. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1826. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1827. if (NonBlocking)
  1828. {
  1829. DebugAssert(!POutput);
  1830. ExecuteShellChecked(ShellCommand);
  1831. }
  1832. else
  1833. {
  1834. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1835. }
  1836. }
  1837. else if (LocalFileCommand)
  1838. {
  1839. if (LocalFileList->Count == 1)
  1840. {
  1841. UnicodeString LocalFile = LocalFileList->Strings[0];
  1842. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1843. {
  1844. UnicodeString FileName = RemoteFileList->Strings[Index];
  1845. TLocalCustomCommand CustomCommand(Data,
  1846. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  1847. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1848. }
  1849. }
  1850. else if (RemoteFileList->Count == 1)
  1851. {
  1852. UnicodeString FileName = RemoteFileList->Strings[0];
  1853. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1854. {
  1855. TLocalCustomCommand CustomCommand(
  1856. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1857. FileName, LocalFileList->Strings[Index], L"");
  1858. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1859. }
  1860. }
  1861. else
  1862. {
  1863. if (LocalFileList->Count != RemoteFileList->Count)
  1864. {
  1865. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  1866. }
  1867. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1868. {
  1869. UnicodeString FileName = RemoteFileList->Strings[Index];
  1870. TLocalCustomCommand CustomCommand(
  1871. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1872. FileName, LocalFileList->Strings[Index], L"");
  1873. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1874. }
  1875. }
  1876. }
  1877. else
  1878. {
  1879. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1880. {
  1881. TLocalCustomCommand CustomCommand(Data,
  1882. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1883. RemoteFileList->Strings[Index], L"", L"");
  1884. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1885. }
  1886. }
  1887. }
  1888. __finally
  1889. {
  1890. if (!NonBlocking)
  1891. {
  1892. Progress.Stop();
  1893. }
  1894. }
  1895. DebugAssert(!FAutoOperation);
  1896. if (!RemoteFiles)
  1897. {
  1898. TempDir = IncludeTrailingBackslash(TempDir);
  1899. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  1900. {
  1901. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  1902. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  1903. // Skip directories, process only nested files.
  1904. // The check is redundant as FileAge fails for directories anyway.
  1905. !DirectoryExists(FileName))
  1906. {
  1907. UnicodeString RemoteDir =
  1908. UnixExtractFileDir(
  1909. UnixIncludeTrailingBackslash(FTerminal->CurrentDirectory) +
  1910. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  1911. TDateTime NewTime;
  1912. if (FileAge(FileName, NewTime) &&
  1913. (NewTime != RemoteFileTimes[Index]))
  1914. {
  1915. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  1916. TemporaryFileCopyParam(CopyParam);
  1917. CopyParam.FileMask = L"";
  1918. FAutoOperation = true;
  1919. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  1920. TemporaryFilesList->Add(FileName);
  1921. FTerminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  1922. }
  1923. }
  1924. }
  1925. }
  1926. }
  1927. __finally
  1928. {
  1929. FAutoOperation = false;
  1930. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  1931. {
  1932. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  1933. }
  1934. }
  1935. }
  1936. __finally
  1937. {
  1938. if (RemoteFileList != FileList)
  1939. {
  1940. delete RemoteFileList;
  1941. }
  1942. if (LocalFileList != ALocalFileList)
  1943. {
  1944. delete LocalFileList;
  1945. }
  1946. if (RemoteFileListFull != RemoteFileList)
  1947. {
  1948. delete RemoteFileListFull;
  1949. }
  1950. }
  1951. }
  1952. //---------------------------------------------------------------------------
  1953. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithRemoteFiles(
  1954. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  1955. bool FileListCommand, UnicodeString * POutput)
  1956. {
  1957. std::unique_ptr<TStrings> SelectedFileList(DirView(osLocal)->CreateFileList(false, true, NULL));
  1958. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  1959. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  1960. {
  1961. UnicodeString FileName = SelectedFileList->Strings[Index];
  1962. if (DirectoryExists(FileName))
  1963. {
  1964. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  1965. {
  1966. LocalFileList->Add(FileName);
  1967. }
  1968. if (FLAGSET(ACommand.Params, ccRecursive))
  1969. {
  1970. TMakeLocalFileListParams MakeFileListParam;
  1971. MakeFileListParam.FileList = LocalFileList.get();
  1972. MakeFileListParam.FileTimes = NULL;
  1973. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1974. MakeFileListParam.Recursive = true;
  1975. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  1976. }
  1977. }
  1978. else
  1979. {
  1980. LocalFileList->Add(FileName);
  1981. }
  1982. }
  1983. if (FileListCommand)
  1984. {
  1985. UnicodeString FileList = MakeFileList(LocalFileList.get());
  1986. TLocalCustomCommand CustomCommand(
  1987. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1988. L"", L"", FileList);
  1989. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1990. }
  1991. else
  1992. {
  1993. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1994. Progress.Start(foCustomCommand, osRemote, FileListCommand ? 1 : LocalFileList->Count);
  1995. DebugAssert(FProgressForm != NULL);
  1996. FProgressForm->ReadOnly = true;
  1997. try
  1998. {
  1999. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2000. {
  2001. UnicodeString FileName = LocalFileList->Strings[Index];
  2002. TLocalCustomCommand CustomCommand(
  2003. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2004. FileName, L"", L"");
  2005. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2006. }
  2007. }
  2008. __finally
  2009. {
  2010. Progress.Stop();
  2011. }
  2012. }
  2013. }
  2014. //---------------------------------------------------------------------------
  2015. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2016. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2017. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2018. {
  2019. TLocalCustomCommand LocalCustomCommand(Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory());
  2020. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2021. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2022. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2023. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2024. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2025. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2026. std::unique_ptr<UnicodeString> POutput;
  2027. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2028. FLAGSET(ACommand.Params, ccCopyResults))
  2029. {
  2030. POutput.reset(new UnicodeString());
  2031. }
  2032. if (!LocalCustomCommand.IsFileCommand(Command))
  2033. {
  2034. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2035. }
  2036. // remote files?
  2037. else if ((FCurrentSide == osRemote) || LocalFileCommand)
  2038. {
  2039. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2040. }
  2041. // local files
  2042. else
  2043. {
  2044. LocalCustomCommandWithRemoteFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2045. }
  2046. if (POutput.get() != NULL)
  2047. {
  2048. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2049. int P = POutput->Pos(sLineBreak);
  2050. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2051. {
  2052. POutput->SetLength(P - 1);
  2053. }
  2054. // Copy even empty output to clipboard
  2055. if (FLAGSET(ACommand.Params, ccCopyResults))
  2056. {
  2057. CopyToClipboard(*POutput);
  2058. }
  2059. // But do not show an empty message box.
  2060. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2061. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2062. !POutput->IsEmpty())
  2063. {
  2064. TClipboardHandler ClipboardHandler;
  2065. ClipboardHandler.Text = *POutput;
  2066. TMessageParams Params;
  2067. TQueryButtonAlias Aliases[1];
  2068. Aliases[0].Button = qaRetry;
  2069. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2070. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2071. Params.Aliases = Aliases;
  2072. Params.AliasesCount = LENOF(Aliases);
  2073. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2074. }
  2075. }
  2076. }
  2077. //---------------------------------------------------------------------------
  2078. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2079. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2080. {
  2081. TCustomCommandData Data(Terminal);
  2082. UnicodeString Site = Terminal->SessionData->SessionKey;
  2083. UnicodeString HelpKeyword = ACommand.HomePage;
  2084. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2085. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2086. {
  2087. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2088. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2089. {
  2090. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, Terminal->CurrentDirectory));
  2091. }
  2092. else
  2093. {
  2094. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory()));
  2095. }
  2096. if (!DoCustomCommandOptionsDialog(
  2097. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2098. {
  2099. Abort();
  2100. }
  2101. }
  2102. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2103. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2104. {
  2105. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2106. }
  2107. else
  2108. {
  2109. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2110. }
  2111. }
  2112. //---------------------------------------------------------------------------
  2113. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2114. const TCustomCommandType & Command)
  2115. {
  2116. DebugAssert(FCustomCommandLocalFileList != NULL);
  2117. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2118. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2119. TStrings * LocalFileList = new TStringList();
  2120. TStrings * RemoteFileList = new TStringList();
  2121. try
  2122. {
  2123. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2124. {
  2125. LocalFileList->Clear();
  2126. LocalFileList->AddObject(
  2127. FCustomCommandLocalFileList->Strings[Index],
  2128. FCustomCommandLocalFileList->Objects[Index]);
  2129. RemoteFileList->Clear();
  2130. RemoteFileList->AddObject(
  2131. FCustomCommandRemoteFileList->Strings[Index],
  2132. FCustomCommandRemoteFileList->Objects[Index]);
  2133. CustomCommand(RemoteFileList, Command, LocalFileList);
  2134. }
  2135. }
  2136. __finally
  2137. {
  2138. delete LocalFileList;
  2139. delete RemoteFileList;
  2140. }
  2141. }
  2142. //---------------------------------------------------------------------------
  2143. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2144. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2145. {
  2146. delete FCustomCommandLocalFileList;
  2147. delete FCustomCommandRemoteFileList;
  2148. // takeover ownership,
  2149. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2150. FCustomCommandLocalFileList = LocalFileList;
  2151. FCustomCommandRemoteFileList = RemoteFileList;
  2152. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2153. if (Button != NULL)
  2154. {
  2155. FCustomCommandMenu->Items->Clear();
  2156. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth);
  2157. MenuPopup(FCustomCommandMenu, Button);
  2158. }
  2159. else
  2160. {
  2161. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2162. }
  2163. }
  2164. //---------------------------------------------------------------------------
  2165. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2166. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2167. {
  2168. DebugAssert(FCapturedLog != NULL);
  2169. if ((OutputType == cotOutput) || (OutputType == cotError))
  2170. {
  2171. FCapturedLog->Add(AddedLine);
  2172. }
  2173. }
  2174. //---------------------------------------------------------------------------
  2175. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2176. TOperationSide Side)
  2177. {
  2178. return (Side == osRemote) &&
  2179. ((Control == RemoteDirView) || (Control == RemoteDriveView));
  2180. }
  2181. //---------------------------------------------------------------------------
  2182. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2183. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2184. {
  2185. TTBItemOptions O;
  2186. O = Item->Options;
  2187. TCustomDirView * DView = DirView(Side);
  2188. if ((DView->ItemFocused != NULL) &&
  2189. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2190. // when resolving links is disabled, default action is to enter the directory,
  2191. // no matter what DoubleClickAction is configured to
  2192. ((Side != osRemote) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2193. // Can only Edit files, but can Open/Copy even directories
  2194. ((DoubleClickAction != dcaEdit) ||
  2195. !DView->ItemIsDirectory(DView->ItemFocused)))
  2196. {
  2197. Item->Options = O << tboDefault;
  2198. }
  2199. else
  2200. {
  2201. Item->Options = O >> tboDefault;
  2202. }
  2203. }
  2204. //---------------------------------------------------------------------------
  2205. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2206. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2207. {
  2208. TCustomDirView * DView = DirView(Side);
  2209. TListItem * Item = DView->ItemFocused;
  2210. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2211. EnableFocusedOperation[Side])
  2212. {
  2213. TPoint ClientPoint;
  2214. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2215. TPoint(0, 0) : MousePos;
  2216. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2217. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2218. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2219. }
  2220. }
  2221. //---------------------------------------------------------------------------
  2222. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2223. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2224. {
  2225. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2226. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2227. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2228. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2229. Handled = true;
  2230. }
  2231. //---------------------------------------------------------------------------
  2232. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2233. {
  2234. }
  2235. //---------------------------------------------------------------------------
  2236. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2237. {
  2238. DebugAssert(FErrorList == NULL);
  2239. if (WinConfiguration->ContinueOnError)
  2240. {
  2241. FErrorList = new TStringList();
  2242. Configuration->Usage->Inc(L"ContinuationsOnError");
  2243. }
  2244. NonVisualDataModule->StartBusy();
  2245. }
  2246. //---------------------------------------------------------------------------
  2247. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2248. {
  2249. NonVisualDataModule->EndBusy();
  2250. if (FErrorList)
  2251. {
  2252. HandleErrorList(FErrorList);
  2253. }
  2254. }
  2255. //---------------------------------------------------------------------------
  2256. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2257. const TCopyParamType & CopyParam)
  2258. {
  2259. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2260. {
  2261. Configuration->Usage->Inc(L"FileMaskUses");
  2262. }
  2263. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2264. {
  2265. Configuration->Usage->Inc(L"OperationMaskUses");
  2266. }
  2267. }
  2268. //---------------------------------------------------------------------------
  2269. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2270. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2271. {
  2272. void * BatchStorage;
  2273. BatchStart(BatchStorage);
  2274. bool Result;
  2275. try
  2276. {
  2277. if ((Operation == foCopy) || (Operation == foMove))
  2278. {
  2279. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2280. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2281. UnicodeString TargetDirectory;
  2282. bool Temp = false;
  2283. bool DragDrop = false;
  2284. int Options = 0;
  2285. TAutoSwitch UseQueue = asAuto;
  2286. if (Param != NULL)
  2287. {
  2288. TTransferOperationParam& TParam =
  2289. *static_cast<TTransferOperationParam*>(Param);
  2290. TargetDirectory = TParam.TargetDirectory;
  2291. Temp = TParam.Temp;
  2292. DragDrop = TParam.DragDrop;
  2293. Options = TParam.Options;
  2294. UseQueue = TParam.Queue;
  2295. }
  2296. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2297. switch (UseQueue)
  2298. {
  2299. case asOn:
  2300. CopyParam.Queue = true;
  2301. break;
  2302. case asOff:
  2303. CopyParam.Queue = false;
  2304. break;
  2305. case asAuto:
  2306. default:
  2307. // keep default
  2308. break;
  2309. }
  2310. Result =
  2311. CopyParamDialog(Direction, Type, Temp, FileList, TargetDirectory,
  2312. CopyParam, !NoConfirmation, DragDrop, Options);
  2313. if (Result)
  2314. {
  2315. DebugAssert(Terminal);
  2316. bool SelectionRestored = false;
  2317. TCustomDirView * DView = NULL;
  2318. if (HasDirView[Side])
  2319. {
  2320. DView = DirView(Side);
  2321. DView->SaveSelection();
  2322. DView->SaveSelectedNames();
  2323. }
  2324. UpdateCopyParamCounters(CopyParam);
  2325. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2326. DebugAssert(FTransferResumeList == NULL);
  2327. FTransferResumeList =
  2328. Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2329. FMoveToQueue = false;
  2330. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2331. try
  2332. {
  2333. TStrings * PermanentFileList;
  2334. std::unique_ptr<TStrings> PermanentFileListOwner;
  2335. try
  2336. {
  2337. if (Side == osLocal)
  2338. {
  2339. PermanentFileList = FileList;
  2340. Params |= FLAGMASK(Temp, cpTemporary);
  2341. Terminal->CopyToRemote(FileList, TargetDirectory, &CopyParam, Params, NULL);
  2342. if (Operation == foMove)
  2343. {
  2344. ReloadLocalDirectory();
  2345. if (DView != NULL)
  2346. {
  2347. DView->RestoreSelection();
  2348. }
  2349. SelectionRestored = true;
  2350. }
  2351. }
  2352. else
  2353. {
  2354. // Clone the file list as it may refer to current directory files,
  2355. // which get destroyed, when the source directory is reloaded after foMove operation.
  2356. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2357. // But for a hotfix, we are not going to do such a big change.
  2358. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2359. PermanentFileList = PermanentFileListOwner.get();
  2360. try
  2361. {
  2362. Terminal->CopyToLocal(
  2363. FileList, TargetDirectory, &CopyParam, Params, NULL);
  2364. }
  2365. __finally
  2366. {
  2367. if (Operation == foMove)
  2368. {
  2369. if (DView != NULL)
  2370. {
  2371. DView->RestoreSelection();
  2372. }
  2373. SelectionRestored = true;
  2374. }
  2375. ReloadLocalDirectory(TargetDirectory);
  2376. }
  2377. }
  2378. }
  2379. catch (EAbort &)
  2380. {
  2381. if (FMoveToQueue)
  2382. {
  2383. Params |=
  2384. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2385. DebugAssert(CopyParam.TransferSkipList == NULL);
  2386. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2387. if (TransferResumeList->Count > 0)
  2388. {
  2389. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2390. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2391. }
  2392. CopyParam.TransferSkipList = TransferResumeList.release();
  2393. // not really needed, just to keep it consistent with TransferResumeList
  2394. FTransferResumeList = NULL;
  2395. FMoveToQueue = false;
  2396. Configuration->Usage->Inc("MovesToBackground");
  2397. AddQueueItem(Queue, Direction, PermanentFileList, TargetDirectory, CopyParam, Params);
  2398. ClearTransferSourceSelection(Direction);
  2399. }
  2400. throw;
  2401. }
  2402. }
  2403. __finally
  2404. {
  2405. if (!SelectionRestored && (DView != NULL))
  2406. {
  2407. DView->DiscardSavedSelection();
  2408. }
  2409. FTransferResumeList = NULL;
  2410. }
  2411. }
  2412. }
  2413. else if (Operation == foRename)
  2414. {
  2415. DebugAssert(DirView(Side)->ItemFocused);
  2416. DirView(Side)->ItemFocused->EditCaption();
  2417. Result = true;
  2418. }
  2419. else if (Operation == foDelete)
  2420. {
  2421. DebugAssert(FileList->Count);
  2422. // We deliberately do not toggle alternative flag (Param), but use OR,
  2423. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2424. // shortcut of CurrentDeleteAlternativeAction
  2425. bool Alternative =
  2426. bool(Param) || UseAlternativeFunction();
  2427. bool Recycle;
  2428. if (Side == osLocal)
  2429. {
  2430. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2431. }
  2432. else
  2433. {
  2434. Recycle = (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2435. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2436. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2437. }
  2438. Result =
  2439. !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2440. if (!Result)
  2441. {
  2442. UnicodeString Query;
  2443. if (FileList->Count == 1)
  2444. {
  2445. if (Side == osLocal)
  2446. {
  2447. Query = ExtractFileName(FileList->Strings[0]);
  2448. }
  2449. else
  2450. {
  2451. Query = UnixExtractFileName(FileList->Strings[0]);
  2452. }
  2453. Query = FMTLOAD(
  2454. (Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2455. }
  2456. else
  2457. {
  2458. Query = FMTLOAD(
  2459. (Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2460. }
  2461. TMessageParams Params(mpNeverAskAgainCheck);
  2462. Params.ImageName = L"Delete file";
  2463. unsigned int Answer = MessageDialog(MainInstructions(Query), qtConfirmation,
  2464. qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2465. if (Answer == qaNeverAskAgain)
  2466. {
  2467. Result = true;
  2468. if (Recycle)
  2469. {
  2470. WinConfiguration->ConfirmRecycling = false;
  2471. }
  2472. else
  2473. {
  2474. WinConfiguration->ConfirmDeleting = false;
  2475. }
  2476. }
  2477. else
  2478. {
  2479. Result = (Answer == qaOK);
  2480. }
  2481. }
  2482. if (Result)
  2483. {
  2484. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2485. }
  2486. }
  2487. else if (Operation == foSetProperties)
  2488. {
  2489. RemoteDirView->SaveSelectedNames();
  2490. Result = SetProperties(Side, FileList);
  2491. }
  2492. else if (Operation == foCustomCommand)
  2493. {
  2494. DebugAssert(Param);
  2495. DebugAssert(Side == osRemote);
  2496. RemoteDirView->SaveSelectedNames();
  2497. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2498. CustomCommand(FileList, *Command, NULL);
  2499. Result = true;
  2500. }
  2501. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2502. {
  2503. DebugAssert(Side == osRemote);
  2504. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2505. (Operation == foRemoteMove), reinterpret_cast<TTerminal *>(Param));
  2506. }
  2507. else if (Operation == foLock)
  2508. {
  2509. DebugAssert(Side == osRemote);
  2510. LockFiles(FileList, true);
  2511. Result = true;
  2512. }
  2513. else if (Operation == foUnlock)
  2514. {
  2515. DebugAssert(Side == osRemote);
  2516. LockFiles(FileList, false);
  2517. Result = true;
  2518. }
  2519. else
  2520. {
  2521. DebugFail();
  2522. }
  2523. }
  2524. __finally
  2525. {
  2526. BatchEnd(BatchStorage);
  2527. }
  2528. return Result;
  2529. }
  2530. //---------------------------------------------------------------------------
  2531. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2532. {
  2533. if (Side == osCurrent)
  2534. {
  2535. Side = FCurrentSide;
  2536. }
  2537. return Side;
  2538. }
  2539. //---------------------------------------------------------------------------
  2540. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2541. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2542. {
  2543. Side = GetSide(Side);
  2544. bool Result;
  2545. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, (Side == osLocal), NULL);
  2546. try
  2547. {
  2548. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2549. }
  2550. __finally
  2551. {
  2552. delete FileList;
  2553. }
  2554. return Result;
  2555. }
  2556. //---------------------------------------------------------------------------
  2557. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2558. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2559. bool NoConfirmation, void * Param)
  2560. {
  2561. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2562. {
  2563. if ((Operation == foCopy) || (Operation == foMove))
  2564. {
  2565. if (GetSide(Side) == osLocal)
  2566. {
  2567. Configuration->Usage->Inc(L"UploadsCommand");
  2568. }
  2569. else
  2570. {
  2571. Configuration->Usage->Inc(L"DownloadsCommand");
  2572. }
  2573. }
  2574. }
  2575. }
  2576. //---------------------------------------------------------------------------
  2577. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2578. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2579. {
  2580. TTransferOperationParam Param;
  2581. if ((WinConfiguration->Interface != ifCommander) ||
  2582. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2583. {
  2584. Flags &= ~cocShortCutHint;
  2585. }
  2586. TCustomDirView * DView = DirView(Side);
  2587. Param.Options =
  2588. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2589. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2590. if (FLAGSET(Flags, cocQueue))
  2591. {
  2592. Param.Queue = asOn;
  2593. }
  2594. else if (FLAGSET(Flags, cocNonQueue))
  2595. {
  2596. Param.Queue = asOff;
  2597. }
  2598. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2599. }
  2600. //---------------------------------------------------------------------------
  2601. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2602. {
  2603. try
  2604. {
  2605. if (ErrorList->Count)
  2606. {
  2607. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2608. if (MessageDialog(Message, qtError,
  2609. qaOK | qaCancel, HELP_NONE) == qaOK)
  2610. {
  2611. unsigned int Answer;
  2612. int Index = 0;
  2613. do
  2614. {
  2615. DebugAssert(Index >= 0 && Index < ErrorList->Count);
  2616. TQueryButtonAlias Aliases[2];
  2617. Aliases[0].Button = qaYes;
  2618. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2619. Aliases[1].Button = qaNo;
  2620. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2621. TMessageParams Params;
  2622. Params.Aliases = Aliases;
  2623. Params.AliasesCount = LENOF(Aliases);
  2624. Answer = MoreMessageDialog(
  2625. FMTLOAD(ERROR_LIST_NUMBER, (Index+1, ErrorList->Count, ErrorList->Strings[Index])),
  2626. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2627. (Index ? qaYes : 0) | (Index < ErrorList->Count - 1 ? qaNo : 0) |
  2628. qaOK, HELP_NONE, &Params);
  2629. if (Answer == qaNo)
  2630. {
  2631. Index++;
  2632. }
  2633. if (Answer == qaYes)
  2634. {
  2635. Index--;
  2636. }
  2637. }
  2638. while (Answer != qaOK);
  2639. }
  2640. }
  2641. }
  2642. __finally
  2643. {
  2644. TStrings * List = ErrorList;
  2645. ErrorList = NULL;
  2646. for (int i = 0; i < List->Count; i++)
  2647. {
  2648. delete List->Objects[i];
  2649. }
  2650. delete List;
  2651. }
  2652. }
  2653. //---------------------------------------------------------------------------
  2654. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2655. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2656. {
  2657. // needed for checking filemasks, as there's no directory object
  2658. // associated with the file object
  2659. File->FullFileName = FullFileName;
  2660. TFileMasks::TParams MaskParams;
  2661. MaskParams.Size = File->Size;
  2662. MaskParams.Modification = File->Modification;
  2663. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2664. }
  2665. //---------------------------------------------------------------------------
  2666. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2667. {
  2668. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2669. Side = GetSide(Side);
  2670. TCustomDirView * DView = DirView(Side);
  2671. TListItem * FocusedItem = DView->ItemFocused;
  2672. UnicodeString Name;
  2673. if ((FocusedItem != NULL) && !DView->ItemIsParentDirectory(FocusedItem))
  2674. {
  2675. Name = DView->ItemFileName(FocusedItem);
  2676. }
  2677. else
  2678. {
  2679. Name = LoadStr(NEW_FILE);
  2680. }
  2681. UnicodeString Names = Name;
  2682. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2683. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2684. HELP_EDIT_NEW, History.get(), true))
  2685. {
  2686. while (!Names.IsEmpty())
  2687. {
  2688. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2689. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2690. UnicodeString TargetFileName;
  2691. UnicodeString LocalFileName;
  2692. UnicodeString RootTempDir;
  2693. UnicodeString TempDir;
  2694. UnicodeString RemoteDirectory;
  2695. bool ExistingFile = false;
  2696. if (Side == osRemote)
  2697. {
  2698. Name = AbsolutePath(FTerminal->CurrentDirectory, Name);
  2699. TRemoteFile * File = NULL;
  2700. if (FTerminal->FileExists(Name, &File))
  2701. {
  2702. try
  2703. {
  2704. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2705. ExistingFile = true;
  2706. }
  2707. __finally
  2708. {
  2709. delete File;
  2710. }
  2711. }
  2712. if (!ExistingFile)
  2713. {
  2714. RemoteDirectory = UnixExtractFilePath(Name);
  2715. TemporaryDirectoryForRemoteFiles(
  2716. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2717. TargetFileName = UnixExtractFileName(Name);
  2718. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2719. LocalFileName = TempDir +
  2720. // We probably do not want to trim the VMS version here
  2721. FTerminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2722. }
  2723. }
  2724. else
  2725. {
  2726. if (ExtractFilePath(Name).IsEmpty())
  2727. {
  2728. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2729. }
  2730. else
  2731. {
  2732. LocalFileName = ExpandFileName(Name);
  2733. }
  2734. TargetFileName = ExtractFileName(Name);
  2735. }
  2736. if (!ExistingFile)
  2737. {
  2738. if (!FileExists(ApiPath(LocalFileName)))
  2739. {
  2740. int File = FileCreate(ApiPath(LocalFileName));
  2741. if (File < 0)
  2742. {
  2743. if (!RootTempDir.IsEmpty())
  2744. {
  2745. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2746. }
  2747. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2748. }
  2749. else
  2750. {
  2751. FileClose(File);
  2752. }
  2753. }
  2754. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2755. const TEditorData * ExternalEditor = NULL;
  2756. TFileMasks::TParams MaskParams; // size not known
  2757. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2758. false, MaskParams);
  2759. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2760. ExternalEditor, RootTempDir, RemoteDirectory);
  2761. }
  2762. }
  2763. }
  2764. }
  2765. //---------------------------------------------------------------------------
  2766. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2767. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2768. {
  2769. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2770. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2771. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2772. return
  2773. ((ExecuteFileBy == efInternalEditor)) ||
  2774. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2775. }
  2776. //---------------------------------------------------------------------------
  2777. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2778. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2779. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2780. UnicodeString RemoteDirectory)
  2781. {
  2782. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2783. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2784. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2785. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2786. Side = GetSide(Side);
  2787. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2788. if (Side == osRemote)
  2789. {
  2790. Data->Terminal = Terminal;
  2791. Data->Queue = Queue;
  2792. Data->SessionData = CloneCurrentSessionData();
  2793. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2794. Data->RemoteDirectory = RemoteDirectory;
  2795. Data->SessionName = Terminal->SessionData->SessionName;
  2796. Data->LocalRootDirectory = LocalRootDirectory;
  2797. Data->OriginalFileName = OriginalFileName;
  2798. Data->Command = L""; // will be changed later for external editor
  2799. }
  2800. if (ExecuteFileBy == efInternalEditor)
  2801. {
  2802. if (Side == osRemote)
  2803. {
  2804. UnicodeString Caption = UnixIncludeTrailingBackslash(RemoteDirectory) + OriginalFileName +
  2805. L" - " + Terminal->SessionData->SessionName;
  2806. TForm * Editor;
  2807. try
  2808. {
  2809. TNotifyEvent OnFileChanged = NULL;
  2810. TNotifyEvent OnSaveAll = NULL;
  2811. TAnyModifiedEvent OnAnyModified = NULL;
  2812. // Edited files are uploaded in background queue, what we do not support with encrypted files
  2813. if (Terminal->IsCapable[fsBackgroundTransfers])
  2814. {
  2815. OnFileChanged = FEditorManager->FileChanged;
  2816. OnSaveAll = SaveAllInternalEditors;
  2817. OnAnyModified = AnyInternalEditorModified;
  2818. }
  2819. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  2820. FEditorManager->FileReload, FEditorManager->FileClosed,
  2821. OnSaveAll, OnAnyModified,
  2822. Caption, FStandaloneEditing, SessionColor, Terminal->SessionData->InternalEditorEncoding);
  2823. }
  2824. catch(...)
  2825. {
  2826. if (!LocalRootDirectory.IsEmpty())
  2827. {
  2828. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  2829. }
  2830. throw;
  2831. }
  2832. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  2833. }
  2834. else
  2835. {
  2836. DebugAssert(!FStandaloneEditing);
  2837. TForm * Editor =
  2838. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  2839. SaveAllInternalEditors, AnyInternalEditorModified,
  2840. L"", false, SessionColor, -1);
  2841. FLocalEditors->Add(Editor);
  2842. }
  2843. }
  2844. else
  2845. {
  2846. HANDLE Process;
  2847. if (ExecuteFileBy == efExternalEditor)
  2848. {
  2849. UnicodeString Program, Params, Dir;
  2850. Data->Command = ExternalEditor->ExternalEditor;
  2851. ReformatFileNameCommand(Data->Command);
  2852. SplitCommand(Data->Command, Program, Params, Dir);
  2853. Params = ExpandFileNameCommand(Params, FileName);
  2854. Program = ExpandEnvironmentVariables(Program);
  2855. if (!ExecuteShell(Program, Params, Process))
  2856. {
  2857. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  2858. }
  2859. }
  2860. else
  2861. {
  2862. DebugAssert(Side == osRemote);
  2863. if (!ExecuteShell(FileName, L"", Process))
  2864. {
  2865. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  2866. }
  2867. }
  2868. if ((Side == osLocal) ||
  2869. ((ExecuteFileBy == efShell) &&
  2870. !WinConfiguration->Editor.SDIShellEditor) ||
  2871. ((ExecuteFileBy == efExternalEditor) &&
  2872. !ExternalEditor->SDIExternalEditor))
  2873. {
  2874. // no need for handle
  2875. if (Process != NULL)
  2876. {
  2877. DebugCheck(CloseHandle(Process));
  2878. }
  2879. Process = INVALID_HANDLE_VALUE;
  2880. }
  2881. else
  2882. {
  2883. if (Process == NULL)
  2884. {
  2885. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  2886. }
  2887. }
  2888. if (Side == osRemote)
  2889. {
  2890. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  2891. }
  2892. }
  2893. }
  2894. //---------------------------------------------------------------------------
  2895. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  2896. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2897. void * /*Arg*/)
  2898. {
  2899. if (Token != NULL)
  2900. {
  2901. EditorFormFileSave(static_cast<TForm *>(Token));
  2902. }
  2903. }
  2904. //---------------------------------------------------------------------------
  2905. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  2906. {
  2907. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  2908. {
  2909. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  2910. }
  2911. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  2912. }
  2913. //---------------------------------------------------------------------------
  2914. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  2915. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2916. void * Arg)
  2917. {
  2918. if ((Token != NULL) &&
  2919. IsEditorFormModified(static_cast<TForm *>(Token)))
  2920. {
  2921. *static_cast<bool *>(Arg) = true;
  2922. }
  2923. }
  2924. //---------------------------------------------------------------------------
  2925. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  2926. TObject * /*Sender*/, bool & Modified)
  2927. {
  2928. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  2929. {
  2930. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  2931. {
  2932. Modified = true;
  2933. }
  2934. }
  2935. if (!Modified)
  2936. {
  2937. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  2938. }
  2939. }
  2940. //---------------------------------------------------------------------------
  2941. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  2942. {
  2943. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  2944. }
  2945. //---------------------------------------------------------------------------
  2946. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  2947. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  2948. UnicodeString & Result, UnicodeString & RootDirectory)
  2949. {
  2950. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  2951. {
  2952. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  2953. Result = RootDirectory;
  2954. }
  2955. else
  2956. {
  2957. RootDirectory = L"";
  2958. Result = WinConfiguration->ExpandedTemporaryDirectory();
  2959. Result = IncludeTrailingBackslash(Result);
  2960. }
  2961. if (WinConfiguration->TemporaryDirectoryAppendSession)
  2962. {
  2963. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  2964. }
  2965. if (WinConfiguration->TemporaryDirectoryAppendPath)
  2966. {
  2967. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  2968. {
  2969. RemoteDirectory.Delete(1, 1);
  2970. }
  2971. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  2972. }
  2973. if (!ForceDirectories(ApiPath(Result)))
  2974. {
  2975. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  2976. }
  2977. }
  2978. //---------------------------------------------------------------------------
  2979. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  2980. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  2981. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  2982. {
  2983. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2984. if (ForceText)
  2985. {
  2986. CopyParam.TransferMode = tmAscii;
  2987. }
  2988. TemporaryFileCopyParam(CopyParam);
  2989. if (AllFiles)
  2990. {
  2991. CopyParam.IncludeFileMask = TFileMasks();
  2992. }
  2993. if (TempDir.IsEmpty())
  2994. {
  2995. TemporaryDirectoryForRemoteFiles(FTerminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  2996. }
  2997. DebugAssert(!FAutoOperation);
  2998. FAutoOperation = AutoOperation;
  2999. Terminal->ExceptionOnFail = true;
  3000. try
  3001. {
  3002. try
  3003. {
  3004. // turn off confirmations, as for MDI editors we may possibly download
  3005. // the same file over
  3006. Terminal->CopyToLocal(
  3007. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3008. if (GetTargetNames)
  3009. {
  3010. for (int i = 0; i < FileList->Count; i++)
  3011. {
  3012. FileList->Strings[i] =
  3013. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3014. }
  3015. }
  3016. }
  3017. catch(...)
  3018. {
  3019. if (!RootTempDir.IsEmpty())
  3020. {
  3021. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3022. }
  3023. throw;
  3024. }
  3025. }
  3026. __finally
  3027. {
  3028. FAutoOperation = false;
  3029. Terminal->ExceptionOnFail = false;
  3030. }
  3031. }
  3032. //---------------------------------------------------------------------------
  3033. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3034. {
  3035. // Do not waste time checking for default editor list the next time,
  3036. // or testing if default editor is not notepad.
  3037. bool TryNextTime = false;
  3038. UnicodeString UsageState;
  3039. if (!WinConfiguration->EditorList->IsDefaultList())
  3040. {
  3041. UsageState = "H";
  3042. }
  3043. else
  3044. {
  3045. UnicodeString Executable;
  3046. UnicodeString ExecutableDescription;
  3047. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3048. {
  3049. UnicodeString Message =
  3050. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3051. unsigned int Answer =
  3052. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3053. if (Answer != qaOK)
  3054. {
  3055. UsageState = "R";
  3056. }
  3057. else
  3058. {
  3059. UsageState = "A";
  3060. TEditorData EditorData;
  3061. EditorData.Editor = edExternal;
  3062. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3063. EditorData.DecideExternalEditorText();
  3064. TEditorList EditorList;
  3065. EditorList = *WinConfiguration->EditorList;
  3066. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3067. WinConfiguration->EditorList = &EditorList;
  3068. }
  3069. }
  3070. }
  3071. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3072. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3073. }
  3074. //---------------------------------------------------------------------------
  3075. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3076. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3077. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3078. {
  3079. if (ExecuteFileBy == efDefaultEditor)
  3080. {
  3081. if (!WinConfiguration->OfferedEditorAutoConfig)
  3082. {
  3083. EditorAutoConfig();
  3084. }
  3085. const TEditorPreferences * Editor =
  3086. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3087. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3088. {
  3089. ExecuteFileBy = efInternalEditor;
  3090. ExternalEditor = NULL;
  3091. }
  3092. else if (Editor->Data->Editor == edOpen)
  3093. {
  3094. ExecuteFileBy = efShell;
  3095. ExternalEditor = NULL;
  3096. }
  3097. else
  3098. {
  3099. ExecuteFileBy = efExternalEditor;
  3100. ExternalEditor = Editor->Data;
  3101. }
  3102. }
  3103. }
  3104. //---------------------------------------------------------------------------
  3105. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3106. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3107. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3108. {
  3109. UnicodeString OriginalFileName;
  3110. UnicodeString LocalRootDirectory;
  3111. UnicodeString RemoteDirectory;
  3112. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3113. (Side == osLocal), MaskParams);
  3114. UnicodeString Counter;
  3115. UnicodeString LocalFileName;
  3116. bool Handled = false;
  3117. if (Side == osRemote)
  3118. {
  3119. // We need to trim VMS version here, so that we use name without version
  3120. // when uploading back to create a new version of the file
  3121. OriginalFileName = FTerminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3122. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3123. TObject * Token = NULL;
  3124. UnicodeString LocalDirectory;
  3125. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3126. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3127. LocalDirectory))
  3128. {
  3129. if (Token != NULL)
  3130. {
  3131. TForm * Form = dynamic_cast<TForm *>(Token);
  3132. if (Form->WindowState == wsMinimized)
  3133. {
  3134. ShowWindow(Form->Handle, SW_RESTORE);
  3135. }
  3136. else
  3137. {
  3138. Form->SetFocus();
  3139. }
  3140. Handled = true;
  3141. }
  3142. else
  3143. {
  3144. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3145. }
  3146. }
  3147. if (!Handled)
  3148. {
  3149. TStringList * FileList1 = new TStringList();
  3150. try
  3151. {
  3152. FileList1->AddObject(FullFileName, Object);
  3153. TemporarilyDownloadFiles(FileList1,
  3154. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3155. LocalRootDirectory, LocalDirectory, true, true, true);
  3156. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3157. }
  3158. __finally
  3159. {
  3160. delete FileList1;
  3161. }
  3162. switch (ExecuteFileBy)
  3163. {
  3164. case efShell:
  3165. Counter = "RemoteFilesExecuted";
  3166. break;
  3167. case efInternalEditor:
  3168. Counter = "RemoteFilesOpenedInInternalEditor";
  3169. break;
  3170. case efExternalEditor:
  3171. Counter = "RemoteFilesOpenedInExternalEditor";
  3172. break;
  3173. default:
  3174. DebugFail();
  3175. }
  3176. }
  3177. }
  3178. else
  3179. {
  3180. LocalFileName = FullFileName;
  3181. OriginalFileName = ExtractFileName(FullFileName);
  3182. switch (ExecuteFileBy)
  3183. {
  3184. case efShell:
  3185. Counter = "LocalFilesExecuted";
  3186. break;
  3187. case efInternalEditor:
  3188. Counter = "LocalFilesOpenedInInternalEditor";
  3189. break;
  3190. case efExternalEditor:
  3191. Counter = "LocalFilesOpenedInExternalEditor";
  3192. break;
  3193. default:
  3194. DebugFail();
  3195. }
  3196. }
  3197. if (!Handled)
  3198. {
  3199. Configuration->Usage->Inc(Counter);
  3200. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3201. ExternalEditor, LocalRootDirectory, RemoteDirectory);
  3202. }
  3203. }
  3204. //---------------------------------------------------------------------------
  3205. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3206. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3207. bool AllSelected, bool OnFocused)
  3208. {
  3209. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3210. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3211. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3212. Side = GetSide(Side);
  3213. TCustomDirView * DView = DirView(Side);
  3214. TStrings * FileList = AllSelected ?
  3215. DView->CreateFileList(OnFocused, Side == osLocal) :
  3216. DView->CreateFocusedFileList(Side == osLocal);
  3217. try
  3218. {
  3219. DebugAssert(AllSelected || (FileList->Count == 1));
  3220. for (int i = 0; i < FileList->Count; i++)
  3221. {
  3222. UnicodeString ListFileName = FileList->Strings[i];
  3223. UnicodeString FileNameOnly = (Side == osRemote) ?
  3224. UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3225. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3226. if (!DView->ItemIsDirectory(Item))
  3227. {
  3228. UnicodeString FullFileName;
  3229. if (Side == osRemote)
  3230. {
  3231. FullFileName = RemoteDirView->Path + ListFileName;
  3232. }
  3233. else
  3234. {
  3235. FullFileName = ListFileName;
  3236. }
  3237. TFileMasks::TParams MaskParams;
  3238. MaskParams.Size = DView->ItemFileSize(Item);
  3239. TDateTimePrecision Precision;
  3240. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3241. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3242. FileList->Objects[i], MaskParams);
  3243. }
  3244. }
  3245. }
  3246. __finally
  3247. {
  3248. delete FileList;
  3249. }
  3250. }
  3251. //---------------------------------------------------------------------------
  3252. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3253. {
  3254. // do not forget to add additional options to TemporarilyDownloadFiles, and AS
  3255. CopyParam.FileNameCase = ncNoChange;
  3256. CopyParam.PreserveRights = false;
  3257. CopyParam.PreserveReadOnly = false;
  3258. CopyParam.ReplaceInvalidChars = true;
  3259. CopyParam.IncludeFileMask = TFileMasks();
  3260. CopyParam.NewerOnly = false;
  3261. CopyParam.FileMask = L"";
  3262. }
  3263. //---------------------------------------------------------------------------
  3264. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3265. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3266. {
  3267. TTerminalManager * Manager = TTerminalManager::Instance();
  3268. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3269. {
  3270. if (!NonVisualDataModule->Busy)
  3271. {
  3272. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3273. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3274. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3275. if (Data->Terminal == NULL)
  3276. {
  3277. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3278. if (SameSiteTerminal != NULL)
  3279. {
  3280. UnicodeString Message =
  3281. FMTLOAD(EDIT_SESSION_REATTACH,
  3282. (FileNameOnly, Data->SessionName, FileNameOnly));
  3283. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3284. {
  3285. Data->Terminal = Terminal;
  3286. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3287. Data->SessionName = Terminal->SessionData->SessionName;
  3288. // We might also overwrite session data
  3289. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3290. }
  3291. else
  3292. {
  3293. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3294. Abort();
  3295. }
  3296. }
  3297. }
  3298. // foreground session should reconnect itself
  3299. else if (Terminal != Data->Terminal)
  3300. {
  3301. UnicodeString Message =
  3302. MainInstructions(
  3303. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3304. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3305. {
  3306. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3307. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3308. }
  3309. else
  3310. {
  3311. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3312. Abort();
  3313. }
  3314. }
  3315. }
  3316. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3317. {
  3318. Configuration->Usage->Inc(L"EditInactiveSession");
  3319. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3320. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3321. (ExtractFileName(FileName), Data->SessionName)));
  3322. }
  3323. }
  3324. TStrings * FileList = new TStringList();
  3325. try
  3326. {
  3327. FileList->Add(FileName);
  3328. // consider using the same settings (preset) as when the file was downloaded
  3329. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3330. TemporaryFileCopyParam(CopyParam);
  3331. if (Data->ForceText)
  3332. {
  3333. CopyParam.TransferMode = tmAscii;
  3334. }
  3335. // so i do not need to worry if masking algorithm works in all cases
  3336. // ("" means "copy file name", no masking is actually done)
  3337. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3338. {
  3339. CopyParam.FileMask = L"";
  3340. }
  3341. else
  3342. {
  3343. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3344. }
  3345. DebugAssert(Data->Queue != NULL);
  3346. int Params = cpNoConfirmation | cpTemporary;
  3347. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3348. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3349. QueueItem->CompleteEvent = UploadCompleteEvent;
  3350. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3351. }
  3352. __finally
  3353. {
  3354. delete FileList;
  3355. }
  3356. }
  3357. //---------------------------------------------------------------------------
  3358. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3359. const UnicodeString FileName, const TEditedFileData * Data)
  3360. {
  3361. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3362. DebugAssert(!NonVisualDataModule->Busy);
  3363. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3364. {
  3365. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3366. (ExtractFileName(FileName), Data->SessionName)));
  3367. }
  3368. TTerminal * PrevTerminal = TTerminalManager::Instance()->ActiveTerminal;
  3369. TTerminalManager::Instance()->ActiveTerminal = Data->Terminal;
  3370. NonVisualDataModule->StartBusy();
  3371. try
  3372. {
  3373. std::unique_ptr<TRemoteFile> File;
  3374. UnicodeString RemoteFileName =
  3375. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3376. FTerminal->ExceptionOnFail = true;
  3377. try
  3378. {
  3379. TRemoteFile * AFile = NULL;
  3380. FTerminal->ReadFile(RemoteFileName, AFile);
  3381. File.reset(AFile);
  3382. if (!File->HaveFullFileName)
  3383. {
  3384. File->FullFileName = RemoteFileName;
  3385. }
  3386. }
  3387. __finally
  3388. {
  3389. FTerminal->ExceptionOnFail = false;
  3390. }
  3391. std::unique_ptr<TStrings> FileList(new TStringList());
  3392. FileList->AddObject(RemoteFileName, File.get());
  3393. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3394. UnicodeString TempDir = ExtractFilePath(FileName);
  3395. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3396. TempDir, true, true, true);
  3397. // sanity check, the target file name should be still the same
  3398. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3399. }
  3400. __finally
  3401. {
  3402. NonVisualDataModule->EndBusy();
  3403. // it actually may not exist anymore...
  3404. TTerminalManager::Instance()->ActiveTerminal = PrevTerminal;
  3405. }
  3406. }
  3407. //---------------------------------------------------------------------------
  3408. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3409. const TEditedFileData * Data, bool & KeepOpen)
  3410. {
  3411. // Command is set for external editors only (not for "shell" open).
  3412. if (!Data->Command.IsEmpty())
  3413. {
  3414. bool AnyFound = false;
  3415. bool AnyMDI = false;
  3416. bool AnyNonMDI = false;
  3417. bool AnyDetect = false;
  3418. TEditorList * EditorList = new TEditorList();
  3419. try
  3420. {
  3421. *EditorList = *WinConfiguration->EditorList;
  3422. for (int i = 0; i < EditorList->Count; i++)
  3423. {
  3424. const TEditorPreferences * Editor = EditorList->Editors[i];
  3425. if ((Editor->Data->Editor == edExternal) &&
  3426. (Editor->Data->ExternalEditor == Data->Command))
  3427. {
  3428. AnyFound = true;
  3429. if (Editor->Data->SDIExternalEditor)
  3430. {
  3431. AnyNonMDI = true;
  3432. if (Editor->Data->DetectMDIExternalEditor)
  3433. {
  3434. AnyDetect = true;
  3435. }
  3436. }
  3437. else
  3438. {
  3439. AnyMDI = true;
  3440. }
  3441. }
  3442. }
  3443. bool EnableMDI = false;
  3444. bool DisableDetect = false;
  3445. if (AnyMDI)
  3446. {
  3447. KeepOpen = true;
  3448. if (AnyNonMDI)
  3449. {
  3450. // there is at least one instance of the editor with MDI support enabled,
  3451. // and one with disabled, enable it for all instances
  3452. EnableMDI = true;
  3453. }
  3454. }
  3455. else if (AnyFound && !AnyDetect)
  3456. {
  3457. // at least once instance found but all have MDI autodetection disabled
  3458. // => close the file (default action)
  3459. }
  3460. else
  3461. {
  3462. // no instance of the editor has MDI support enabled
  3463. TMessageParams Params;
  3464. if (AnyFound)
  3465. {
  3466. // there is at least one instance of the editor
  3467. Params.Params |= mpNeverAskAgainCheck;
  3468. }
  3469. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3470. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3471. switch (Answer)
  3472. {
  3473. case qaNeverAskAgain:
  3474. DisableDetect = true;
  3475. break;
  3476. case qaNo:
  3477. EnableMDI = true;
  3478. KeepOpen = true;
  3479. break;
  3480. }
  3481. }
  3482. if (AnyFound && (EnableMDI || DisableDetect))
  3483. {
  3484. bool Changed = false;
  3485. for (int i = 0; i < EditorList->Count; i++)
  3486. {
  3487. const TEditorPreferences * Editor = EditorList->Editors[i];
  3488. if ((Editor->Data->Editor == edExternal) &&
  3489. (Editor->Data->ExternalEditor == Data->Command) &&
  3490. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3491. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3492. {
  3493. Changed = true;
  3494. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3495. if (EnableMDI)
  3496. {
  3497. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3498. }
  3499. if (DisableDetect)
  3500. {
  3501. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3502. }
  3503. EditorList->Change(i, UpdatedEditor);
  3504. }
  3505. }
  3506. if (Changed)
  3507. {
  3508. WinConfiguration->EditorList = EditorList;
  3509. }
  3510. }
  3511. }
  3512. __finally
  3513. {
  3514. delete EditorList;
  3515. }
  3516. }
  3517. else
  3518. {
  3519. // "open" case
  3520. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3521. qaOK, HELP_APP_EARLY_CLOSED);
  3522. }
  3523. }
  3524. //---------------------------------------------------------------------------
  3525. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3526. {
  3527. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3528. }
  3529. //---------------------------------------------------------------------------
  3530. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3531. {
  3532. SideEnter(osRemote);
  3533. }
  3534. //---------------------------------------------------------------------------
  3535. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3536. {
  3537. MakeNextInTabOrder(RemoteDirView, RemoteDriveView);
  3538. SideEnter(osRemote);
  3539. }
  3540. //---------------------------------------------------------------------------
  3541. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3542. {
  3543. FCurrentSide = Side;
  3544. if (Visible)
  3545. {
  3546. UpdateControls();
  3547. }
  3548. }
  3549. //---------------------------------------------------------------------------
  3550. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3551. TStrings * FileList, bool Alternative)
  3552. {
  3553. TCustomDirView * DView = DirView(Side);
  3554. DView->SaveSelection();
  3555. DView->SaveSelectedNames();
  3556. DebugAssert(!FAlternativeDelete);
  3557. FAlternativeDelete = Alternative;
  3558. try
  3559. {
  3560. if (Side == osRemote)
  3561. {
  3562. DebugAssert(Terminal != NULL);
  3563. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3564. }
  3565. else
  3566. {
  3567. try
  3568. {
  3569. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3570. }
  3571. __finally
  3572. {
  3573. ReloadLocalDirectory();
  3574. }
  3575. }
  3576. FAlternativeDelete = false;
  3577. }
  3578. catch(...)
  3579. {
  3580. FAlternativeDelete = false;
  3581. DView->DiscardSavedSelection();
  3582. throw;
  3583. }
  3584. DView->RestoreSelection();
  3585. }
  3586. //---------------------------------------------------------------------------
  3587. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3588. {
  3589. DebugAssert(Terminal);
  3590. RemoteDirView->SaveSelection();
  3591. RemoteDirView->SaveSelectedNames();
  3592. try
  3593. {
  3594. if (Lock)
  3595. {
  3596. Terminal->LockFiles(FileList);
  3597. }
  3598. else
  3599. {
  3600. Terminal->UnlockFiles(FileList);
  3601. }
  3602. }
  3603. catch(...)
  3604. {
  3605. RemoteDirView->DiscardSavedSelection();
  3606. throw;
  3607. }
  3608. RemoteDirView->RestoreSelection();
  3609. }
  3610. //---------------------------------------------------------------------------
  3611. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TTerminal *& Session,
  3612. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3613. bool NoConfirmation, bool Move)
  3614. {
  3615. DebugAssert(Terminal != NULL);
  3616. // update Terminal->StateData->RemoteDirectory
  3617. UpdateTerminal(Terminal);
  3618. if (Session == NULL)
  3619. {
  3620. Session = TTerminalManager::Instance()->ActiveTerminal;
  3621. }
  3622. if (Session == TTerminalManager::Instance()->ActiveTerminal)
  3623. {
  3624. if (RemoteDriveView->DropTarget != NULL)
  3625. {
  3626. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3627. }
  3628. else if (RemoteDirView->DropTarget != NULL)
  3629. {
  3630. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3631. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3632. }
  3633. else
  3634. {
  3635. Target = RemoteDirView->Path;
  3636. }
  3637. }
  3638. else
  3639. {
  3640. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(Session));
  3641. Target = ManagedTerminal->StateData->RemoteDirectory;
  3642. }
  3643. Target = UnixIncludeTrailingBackslash(Target);
  3644. if (FileList->Count == 1)
  3645. {
  3646. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3647. }
  3648. else
  3649. {
  3650. FileMask = AnyMask;
  3651. }
  3652. DirectCopy = FTerminal->IsCapable[fcRemoteCopy] || FTerminal->IsCapable[fcSecondaryShell];
  3653. bool Result = true;
  3654. if (!NoConfirmation)
  3655. {
  3656. bool Multi = (FileList->Count > 1);
  3657. if (Move)
  3658. {
  3659. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3660. }
  3661. else
  3662. {
  3663. TStrings * Sessions = TTerminalManager::Instance()->TerminalList;
  3664. TStrings * Directories = new TStringList;
  3665. try
  3666. {
  3667. for (int Index = 0; Index < Sessions->Count; Index++)
  3668. {
  3669. TManagedTerminal * Terminal =
  3670. dynamic_cast<TManagedTerminal *>(Sessions->Objects[Index]);
  3671. Directories->Add(Terminal->StateData->RemoteDirectory);
  3672. }
  3673. TDirectRemoteCopy AllowDirectCopy;
  3674. if (FTerminal->IsCapable[fcRemoteCopy] || FTerminal->CommandSessionOpened)
  3675. {
  3676. DebugAssert(DirectCopy);
  3677. AllowDirectCopy = drcAllow;
  3678. }
  3679. else if (FTerminal->IsCapable[fcSecondaryShell])
  3680. {
  3681. DebugAssert(DirectCopy);
  3682. AllowDirectCopy = drcConfirmCommandSession;
  3683. }
  3684. else
  3685. {
  3686. DebugAssert(!DirectCopy);
  3687. AllowDirectCopy = drcDisallow;
  3688. }
  3689. void * ASession = Session;
  3690. Result = DoRemoteCopyDialog(Sessions, Directories, AllowDirectCopy,
  3691. Multi, ASession, Target, FileMask, DirectCopy, TTerminalManager::Instance()->ActiveTerminal);
  3692. Session = static_cast<TTerminal *>(ASession);
  3693. }
  3694. __finally
  3695. {
  3696. delete Directories;
  3697. }
  3698. }
  3699. }
  3700. return Result;
  3701. }
  3702. //---------------------------------------------------------------------------
  3703. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3704. TStrings * FileList, bool NoConfirmation, bool Move, TTerminal * Session)
  3705. {
  3706. bool DirectCopy;
  3707. UnicodeString Target, FileMask;
  3708. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3709. if (Result)
  3710. {
  3711. if (!Move && !DirectCopy)
  3712. {
  3713. Configuration->Usage->Inc("RemoteCopyTemp");
  3714. UnicodeString RootTempDir;
  3715. UnicodeString TempDir;
  3716. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3717. TStrings * TemporaryFilesList = new TStringList();
  3718. try
  3719. {
  3720. TMakeLocalFileListParams MakeFileListParam;
  3721. MakeFileListParam.FileList = TemporaryFilesList;
  3722. MakeFileListParam.FileTimes = NULL;
  3723. MakeFileListParam.IncludeDirs = true;
  3724. MakeFileListParam.Recursive = false;
  3725. ProcessLocalDirectory(TempDir, FTerminal->MakeLocalFileList, &MakeFileListParam);
  3726. TTerminalManager::Instance()->ActiveTerminal = Session;
  3727. if (TemporaryFilesList->Count > 0)
  3728. {
  3729. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3730. CopyParam.FileMask = FileMask;
  3731. DebugAssert(!FAutoOperation);
  3732. FAutoOperation = true;
  3733. FTerminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3734. }
  3735. }
  3736. __finally
  3737. {
  3738. delete TemporaryFilesList;
  3739. FAutoOperation = false;
  3740. if (!RootTempDir.IsEmpty())
  3741. {
  3742. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3743. }
  3744. }
  3745. }
  3746. else
  3747. {
  3748. RemoteDirView->SaveSelection();
  3749. RemoteDirView->SaveSelectedNames();
  3750. try
  3751. {
  3752. if (Move)
  3753. {
  3754. Configuration->Usage->Inc("RemoteMove");
  3755. Terminal->MoveFiles(FileList, Target, FileMask);
  3756. }
  3757. else
  3758. {
  3759. Configuration->Usage->Inc("RemoteCopyDirect");
  3760. DebugAssert(DirectCopy);
  3761. DebugAssert(Session == FTerminal);
  3762. if (FTerminal->IsCapable[fcRemoteCopy] ||
  3763. FTerminal->CommandSessionOpened ||
  3764. CommandSessionFallback())
  3765. {
  3766. Terminal->CopyFiles(FileList, Target, FileMask);
  3767. }
  3768. }
  3769. }
  3770. catch(...)
  3771. {
  3772. RemoteDirView->DiscardSavedSelection();
  3773. throw;
  3774. }
  3775. RemoteDirView->RestoreSelection();
  3776. }
  3777. }
  3778. return Result;
  3779. }
  3780. //---------------------------------------------------------------------------
  3781. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3782. const UnicodeString & Path, TRemoteProperties & Properties)
  3783. {
  3784. Properties.Valid = Properties.Valid << vpEncrypt;
  3785. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  3786. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  3787. }
  3788. //---------------------------------------------------------------------------
  3789. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3790. {
  3791. Side = GetSide(Side);
  3792. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3793. TRemoteProperties * AProperties = (Side == osRemote ? &Properties : NULL);
  3794. UnicodeString Name = LoadStr(NEW_FOLDER);
  3795. int AllowedChanges =
  3796. FLAGMASK((Side == osRemote) && Terminal->IsCapable[fcModeChanging], cpMode);
  3797. bool SaveSettings = false;
  3798. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  3799. {
  3800. TWindowLock Lock(this);
  3801. if (Side == osRemote)
  3802. {
  3803. if (SaveSettings)
  3804. {
  3805. GUIConfiguration->NewDirectoryProperties = Properties;
  3806. }
  3807. CreateRemoteDirectory(Name, Properties);
  3808. }
  3809. else
  3810. {
  3811. DirView(Side)->CreateDirectory(Name);
  3812. }
  3813. }
  3814. }
  3815. //---------------------------------------------------------------------------
  3816. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  3817. {
  3818. TWindowLock Lock(this);
  3819. DirView(Side)->ExecuteHomeDirectory();
  3820. }
  3821. //---------------------------------------------------------------------------
  3822. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  3823. {
  3824. DoOpenDirectoryDialog(odBrowse, Side);
  3825. }
  3826. //---------------------------------------------------------------------------
  3827. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  3828. {
  3829. UnicodeString Path = Bookmark->GetSideDirectory(Side);
  3830. bool Result = !Path.IsEmpty();
  3831. if (Result)
  3832. {
  3833. // While we might get here when the session is closed (from location profiles),
  3834. // it's not a problem as the Path setter is noop then
  3835. DirView(Side)->Path = Path;
  3836. }
  3837. return Result;
  3838. }
  3839. //---------------------------------------------------------------------------
  3840. void __fastcall TCustomScpExplorerForm::CalculateSize(
  3841. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  3842. bool & Close)
  3843. {
  3844. // terminal can be already closed (e.g. dropped connection)
  3845. if (Terminal != NULL)
  3846. {
  3847. try
  3848. {
  3849. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  3850. }
  3851. catch(...)
  3852. {
  3853. if (!Terminal->Active)
  3854. {
  3855. Close = true;
  3856. }
  3857. throw;
  3858. }
  3859. }
  3860. }
  3861. //---------------------------------------------------------------------------
  3862. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  3863. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  3864. bool & Close)
  3865. {
  3866. // terminal can be already closed (e.g. dropped connection)
  3867. if (Terminal != NULL)
  3868. {
  3869. Configuration->Usage->Inc(L"ChecksumCalculated");
  3870. try
  3871. {
  3872. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  3873. }
  3874. catch(...)
  3875. {
  3876. if (!Terminal->Active)
  3877. {
  3878. Close = true;
  3879. }
  3880. throw;
  3881. }
  3882. }
  3883. }
  3884. //---------------------------------------------------------------------------
  3885. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  3886. {
  3887. bool Result;
  3888. if (Side == osRemote)
  3889. {
  3890. TRemoteTokenList * GroupList = NULL;
  3891. TRemoteTokenList * UserList = NULL;
  3892. try
  3893. {
  3894. TRemoteProperties CurrentProperties;
  3895. if (Terminal->LoadFilesProperties(FileList))
  3896. {
  3897. RemoteDirView->Invalidate();
  3898. }
  3899. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  3900. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  3901. if (CapableGroupChanging || CapableOwnerChanging)
  3902. {
  3903. if (CapableGroupChanging)
  3904. {
  3905. GroupList = Terminal->Groups->Duplicate();
  3906. }
  3907. if (CapableOwnerChanging)
  3908. {
  3909. UserList = Terminal->Users->Duplicate();
  3910. }
  3911. TRemoteDirectory * Files = Terminal->Files;
  3912. int Count = Files->Count;
  3913. if (Count > 100)
  3914. {
  3915. Count = 100;
  3916. }
  3917. for (int Index = 0; Index < Count; Index++)
  3918. {
  3919. TRemoteFile * File = Files->Files[Index];
  3920. if (CapableGroupChanging)
  3921. {
  3922. GroupList->AddUnique(File->Group);
  3923. }
  3924. if (CapableOwnerChanging)
  3925. {
  3926. UserList->AddUnique(File->Owner);
  3927. }
  3928. }
  3929. // if we haven't collected tokens for all files in current directory,
  3930. // make sure we collect them at least for all selected files.
  3931. // (note that so far the files in FileList has to be from current direcotry)
  3932. if (Count < Files->Count)
  3933. {
  3934. for (int Index = 0; Index < FileList->Count; Index++)
  3935. {
  3936. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  3937. if (CapableGroupChanging)
  3938. {
  3939. GroupList->AddUnique(File->Group);
  3940. }
  3941. if (CapableOwnerChanging)
  3942. {
  3943. UserList->AddUnique(File->Owner);
  3944. }
  3945. }
  3946. }
  3947. }
  3948. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  3949. int Flags = 0;
  3950. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  3951. if (CapableOwnerChanging) Flags |= cpOwner;
  3952. if (CapableGroupChanging) Flags |= cpGroup;
  3953. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  3954. if (Terminal->IsCapable[fcCalculatingChecksum])
  3955. {
  3956. CalculateChecksumEvent = CalculateChecksum;
  3957. }
  3958. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  3959. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  3960. TRemoteProperties NewProperties = CurrentProperties;
  3961. Result =
  3962. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  3963. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  3964. Terminal->IsCapable[fcGroupOwnerChangingByID],
  3965. CalculateSize, CalculateChecksumEvent);
  3966. if (Result)
  3967. {
  3968. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  3969. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  3970. }
  3971. }
  3972. __finally
  3973. {
  3974. delete GroupList;
  3975. delete UserList;
  3976. }
  3977. }
  3978. else
  3979. {
  3980. DirView(Side)->DisplayPropertiesMenu();
  3981. Result = true;
  3982. }
  3983. return Result;
  3984. }
  3985. //---------------------------------------------------------------------------
  3986. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  3987. {
  3988. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  3989. {
  3990. FIgnoreNextDialogChar = Key;
  3991. }
  3992. Key = 0;
  3993. }
  3994. //---------------------------------------------------------------------------
  3995. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  3996. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  3997. {
  3998. const TCustomCommandType * Command = List->Find(KeyShortCut);
  3999. if (Command != NULL)
  4000. {
  4001. KeyProcessed(Key, Shift);
  4002. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4003. {
  4004. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4005. false, false, const_cast<TCustomCommandType *>(Command));
  4006. }
  4007. }
  4008. }
  4009. //---------------------------------------------------------------------------
  4010. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4011. {
  4012. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4013. {
  4014. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4015. }
  4016. if (!DirView(osCurrent)->IsEditing())
  4017. {
  4018. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4019. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4020. {
  4021. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4022. if (((Action->ShortCut == KeyShortCut) ||
  4023. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4024. AllowedAction(Action, aaShortCut))
  4025. {
  4026. // Has to be called before the action as the dialog char is already in queue.
  4027. // So when the action consumes message queue, we already need to have the
  4028. // FIgnoreNextDialogChar set
  4029. KeyProcessed(Key, Shift);
  4030. // Reset reference to previous component (when menu/toolbar was clicked).
  4031. // Needed to detect that action was invoked by keyboard shortcut
  4032. // in TNonVisualDataModule::ExplorerActionsExecute
  4033. Action->ActionComponent = NULL;
  4034. Action->Execute();
  4035. return;
  4036. }
  4037. }
  4038. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4039. {
  4040. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4041. {
  4042. KeyProcessed(Key, Shift);
  4043. TTerminalManager::Instance()->ActiveTerminalIndex = i;
  4044. return;
  4045. }
  4046. }
  4047. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4048. {
  4049. KeyProcessed(Key, Shift);
  4050. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4051. }
  4052. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4053. if (IsCustomShortCut(CustomShortCut))
  4054. {
  4055. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4056. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4057. if (WinConfiguration->SharedBookmarks != NULL)
  4058. {
  4059. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4060. if ((Bookmark != NULL) &&
  4061. OpenBookmark(FCurrentSide, Bookmark))
  4062. {
  4063. KeyProcessed(Key, Shift);
  4064. }
  4065. }
  4066. }
  4067. }
  4068. TForm::KeyDown(Key, Shift);
  4069. }
  4070. //---------------------------------------------------------------------------
  4071. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4072. {
  4073. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4074. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  4075. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4076. DebugAssert(Terminal);
  4077. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4078. bool SecurityEnabled = !SessionInfo.SecurityProtocolName.IsEmpty();
  4079. SessionStatusBar->Panels->Items[Offset + 0]->Enabled = SecurityEnabled;
  4080. // expanded from ?: to avoid memory leaks
  4081. if (SecurityEnabled)
  4082. {
  4083. SessionStatusBar->Panels->Items[Offset + 0]->Hint =
  4084. FMTLOAD(STATUS_SECURE, (SessionInfo.SecurityProtocolName));
  4085. }
  4086. else
  4087. {
  4088. SessionStatusBar->Panels->Items[Offset + 0]->Hint = LoadStr(STATUS_INSECURE);
  4089. }
  4090. if (FileSystemInfo.ProtocolName.IsEmpty())
  4091. {
  4092. SessionStatusBar->Panels->Items[Offset + 1]->Caption = SessionInfo.ProtocolName;
  4093. }
  4094. else
  4095. {
  4096. SessionStatusBar->Panels->Items[Offset + 1]->Caption = FileSystemInfo.ProtocolName;
  4097. }
  4098. SessionStatusBar->Panels->Items[Offset + 1]->Hint = LoadStr(STATUS_PROTOCOL_HINT);
  4099. SessionStatusBar->Panels->Items[Offset + 2]->Enabled =
  4100. (!SessionInfo.CSCompression.IsEmpty() || !SessionInfo.SCCompression.IsEmpty());
  4101. UnicodeString CSCompressionStr = DefaultStr(SessionInfo.CSCompression, LoadStr(NO_STR));
  4102. UnicodeString SCCompressionStr = DefaultStr(SessionInfo.SCCompression, LoadStr(NO_STR));
  4103. UnicodeString CompressionStr = CSCompressionStr;
  4104. if (CSCompressionStr != SCCompressionStr)
  4105. {
  4106. CompressionStr += UnicodeString(L"/") + SCCompressionStr;
  4107. }
  4108. SessionStatusBar->Panels->Items[Offset + 2]->Hint = FMTLOAD(STATUS_COMPRESSION_HINT, (CompressionStr));
  4109. SessionStatusBar->Panels->Items[Offset + 3]->Hint = LoadStr(STATUS_DURATION_HINT);
  4110. UpdateStatusBar();
  4111. }
  4112. //---------------------------------------------------------------------------
  4113. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4114. {
  4115. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4116. DebugAssert(SessionStatusBar != NULL);
  4117. if (!Terminal || !Terminal->Active || Terminal->Status < ssOpened)
  4118. {
  4119. // note: (Terminal->Status < sshReady) currently never happens here,
  4120. // so STATUS_CONNECTING is never used
  4121. SessionStatusBar->SimplePanel = true;
  4122. SessionStatusBar->SimpleText = LoadStr(
  4123. !Terminal || !Terminal->Active ? STATUS_NOT_CONNECTED : STATUS_CONNECTING);
  4124. }
  4125. else
  4126. {
  4127. DebugAssert(Terminal);
  4128. SessionStatusBar->SimplePanel = false;
  4129. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4130. if (!FNote.IsEmpty())
  4131. {
  4132. SessionStatusBar->Panels->Items[0]->Caption = FNote;
  4133. }
  4134. else
  4135. {
  4136. UpdateStatusPanelText(SessionStatusBar->Panels->Items[0]);
  4137. }
  4138. SessionStatusBar->Panels->Items[0]->Hint = FNoteHints;
  4139. SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1]->Caption =
  4140. FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4141. }
  4142. }
  4143. //---------------------------------------------------------------------------
  4144. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4145. {
  4146. Panel->Caption = L"";
  4147. }
  4148. //---------------------------------------------------------------------------
  4149. void __fastcall TCustomScpExplorerForm::Idle()
  4150. {
  4151. if (FShowing || FStandaloneEditing)
  4152. {
  4153. FEditorManager->Check();
  4154. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4155. // that needs to know if queue view is visible (and it may be closed after queue update)
  4156. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4157. }
  4158. if (FShowing)
  4159. {
  4160. if (!NonVisualDataModule->Busy &&
  4161. // Menu is opened or drag&drop is going on
  4162. (Mouse->Capture == NULL))
  4163. {
  4164. if (FRefreshRemoteDirectory)
  4165. {
  4166. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4167. {
  4168. Terminal->RefreshDirectory();
  4169. }
  4170. FRefreshRemoteDirectory = false;
  4171. }
  4172. if (FRefreshLocalDirectory)
  4173. {
  4174. ReloadLocalDirectory();
  4175. FRefreshLocalDirectory = false;
  4176. }
  4177. if (WinConfiguration->RefreshRemotePanel)
  4178. {
  4179. TManagedTerminal * ManagedTerminal =
  4180. dynamic_cast<TManagedTerminal *>(Terminal);
  4181. if ((ManagedTerminal != NULL) && (Terminal->Status == ssOpened) &&
  4182. (Now() - ManagedTerminal->DirectoryLoaded >
  4183. WinConfiguration->RefreshRemotePanelInterval))
  4184. {
  4185. RemoteDirView->ReloadDirectory();
  4186. }
  4187. }
  4188. }
  4189. }
  4190. if (FShowing || FStandaloneEditing)
  4191. {
  4192. if (FQueueStatusInvalidated)
  4193. {
  4194. UpdateQueueStatus(false);
  4195. }
  4196. RefreshQueueItems();
  4197. }
  4198. if (FShowing)
  4199. {
  4200. UpdateStatusBar();
  4201. }
  4202. FIgnoreNextDialogChar = 0;
  4203. }
  4204. //---------------------------------------------------------------------------
  4205. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4206. {
  4207. try
  4208. {
  4209. FUserActionTimer->Enabled = false;
  4210. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4211. {
  4212. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4213. {
  4214. FPendingQueueActionItem->ProcessUserAction();
  4215. }
  4216. }
  4217. }
  4218. __finally
  4219. {
  4220. FPendingQueueActionItem = NULL;
  4221. }
  4222. }
  4223. //---------------------------------------------------------------------------
  4224. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4225. {
  4226. if (WinConfiguration->MinimizeToTray)
  4227. {
  4228. UpdateTrayIcon();
  4229. FTrayIcon->Visible = true;
  4230. if (Visible)
  4231. {
  4232. ShowWindow(Handle, SW_HIDE);
  4233. }
  4234. }
  4235. }
  4236. //---------------------------------------------------------------------------
  4237. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4238. {
  4239. // WORKAROUND
  4240. // When restoring maximized window from minimization,
  4241. // rarely some controls do not align properly.
  4242. // Two instances seen (both for Commander):
  4243. // - When restoring, window is temporarily narrower (not maximized),
  4244. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4245. // Once maximized already, top dock shinks back, but the session PageControl,
  4246. // do not align up, leaving space between TopDock and PageControl.
  4247. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4248. for (int Index = 0; Index < ControlCount; Index++)
  4249. {
  4250. RealignControl(Controls[Index]);
  4251. }
  4252. if (FTrayIcon->Visible)
  4253. {
  4254. FTrayIcon->Visible = false;
  4255. if (Visible)
  4256. {
  4257. ShowWindow(Handle, SW_SHOW);
  4258. }
  4259. }
  4260. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4261. {
  4262. FNeedSession = false;
  4263. NonVisualDataModule->StartBusy();
  4264. try
  4265. {
  4266. NeedSession(false);
  4267. }
  4268. __finally
  4269. {
  4270. NonVisualDataModule->EndBusy();
  4271. }
  4272. }
  4273. }
  4274. //---------------------------------------------------------------------------
  4275. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4276. {
  4277. FTrayIcon->Hint = GetMainForm()->Caption;
  4278. }
  4279. //---------------------------------------------------------------------------
  4280. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4281. {
  4282. UpdateTrayIcon();
  4283. }
  4284. //---------------------------------------------------------------------------
  4285. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4286. {
  4287. // workaround
  4288. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4289. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4290. // But WM_ACTIVATEAPP message can be received even when
  4291. // the main window is minimized to the tray and internal editor window is focused
  4292. // (after another application was previously active)
  4293. if (::IsIconic(Handle))
  4294. {
  4295. if (!IsAppIconic())
  4296. {
  4297. SetAppIconic(true);
  4298. }
  4299. }
  4300. ::ApplicationRestore();
  4301. Application->BringToFront();
  4302. }
  4303. //---------------------------------------------------------------------------
  4304. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4305. {
  4306. RestoreApp();
  4307. }
  4308. //---------------------------------------------------------------------------
  4309. void __fastcall TCustomScpExplorerForm::NewSession(bool FromSite, const UnicodeString & SessionUrl)
  4310. {
  4311. if (OpenInNewWindow())
  4312. {
  4313. // todo: Pass FromSite
  4314. ExecuteNewInstance(SessionUrl);
  4315. }
  4316. else
  4317. {
  4318. TTerminalManager::Instance()->NewSession(FromSite, SessionUrl);
  4319. }
  4320. }
  4321. //---------------------------------------------------------------------------
  4322. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4323. {
  4324. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->SessionName;
  4325. // current working directories become defaults here, what is not right
  4326. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4327. StoredSessions->NewSession(SessionName, SessionData.get());
  4328. // modified only, explicit
  4329. StoredSessions->Save(false, true);
  4330. // encode session name because of slashes in hierarchical sessions
  4331. return EncodeUrlString(SessionName);
  4332. }
  4333. //---------------------------------------------------------------------------
  4334. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4335. {
  4336. if (OpenInNewWindow())
  4337. {
  4338. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4339. }
  4340. else
  4341. {
  4342. // current working directories become defaults here, what is not right
  4343. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4344. TTerminalManager * Manager = TTerminalManager::Instance();
  4345. TTerminal * Terminal = Manager->NewTerminal(SessionData.get());
  4346. Manager->ActiveTerminal = Terminal;
  4347. }
  4348. }
  4349. //---------------------------------------------------------------------------
  4350. void __fastcall TCustomScpExplorerForm::RenameSession()
  4351. {
  4352. UnicodeString Name = Terminal->SessionData->Name;
  4353. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME))
  4354. {
  4355. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4356. TSessionData::ValidateName(Name);
  4357. // This is inconsistent with how color (for example) is handled.
  4358. Terminal->SessionData->Name = Name;
  4359. UpdateControls();
  4360. // Add/Remove distinguishing paths from sessions of the same name.
  4361. DoTerminalListChanged();
  4362. }
  4363. }
  4364. //---------------------------------------------------------------------------
  4365. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4366. {
  4367. DebugAssert(Queue != NULL);
  4368. bool Result = Queue->IsEmpty;
  4369. if (!Result)
  4370. {
  4371. SetFocus();
  4372. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4373. }
  4374. return Result;
  4375. }
  4376. //---------------------------------------------------------------------------
  4377. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4378. {
  4379. return CanCloseQueue(FQueue);
  4380. }
  4381. //---------------------------------------------------------------------------
  4382. void __fastcall TCustomScpExplorerForm::CloseSession()
  4383. {
  4384. if (CanCloseQueue())
  4385. {
  4386. TTerminalManager::Instance()->FreeActiveTerminal();
  4387. }
  4388. }
  4389. //---------------------------------------------------------------------------
  4390. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4391. {
  4392. if (OpenInNewWindow())
  4393. {
  4394. // encode session name because of slashes in hierarchical sessions
  4395. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4396. }
  4397. else
  4398. {
  4399. TTerminalManager * Manager = TTerminalManager::Instance();
  4400. TTerminal * Terminal = Manager->NewTerminal(Data);
  4401. Manager->ActiveTerminal = Terminal;
  4402. }
  4403. }
  4404. //---------------------------------------------------------------------------
  4405. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4406. {
  4407. if (OpenInNewWindow())
  4408. {
  4409. ExecuteNewInstance(Name);
  4410. }
  4411. else
  4412. {
  4413. TTerminalManager * Manager = TTerminalManager::Instance();
  4414. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4415. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4416. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  4417. }
  4418. }
  4419. //---------------------------------------------------------------------------
  4420. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4421. bool &CanClose)
  4422. {
  4423. if (Terminal != NULL)
  4424. {
  4425. if (Terminal->Active && WinConfiguration->ConfirmClosingSession)
  4426. {
  4427. unsigned int Result;
  4428. TMessageParams Params(mpNeverAskAgainCheck);
  4429. UnicodeString Message;
  4430. int Answers = qaOK | qaCancel;
  4431. if (TTerminalManager::Instance()->Count > 1)
  4432. {
  4433. if (!WinConfiguration->AutoSaveWorkspace)
  4434. {
  4435. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE2);
  4436. Answers = qaYes | qaNo | qaCancel;
  4437. }
  4438. else
  4439. {
  4440. Message = MainInstructions(LoadStr(CLOSE_SESSIONS));
  4441. }
  4442. }
  4443. else
  4444. {
  4445. Message = MainInstructions(FMTLOAD(CLOSE_SESSION, (Terminal->SessionData->SessionName)));
  4446. }
  4447. if (WinConfiguration->AutoSaveWorkspace)
  4448. {
  4449. Message =
  4450. FORMAT("%s\n\n%s", (Message,
  4451. FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()))));
  4452. }
  4453. SetFocus();
  4454. Result = MessageDialog(Message, qtConfirmation,
  4455. Answers, HELP_NONE, &Params);
  4456. if (Result == qaNeverAskAgain)
  4457. {
  4458. WinConfiguration->ConfirmClosingSession = false;
  4459. }
  4460. if (Result == qaNo)
  4461. {
  4462. CanClose = SaveWorkspace(true);
  4463. // note that the workspace will be saved redundatly again from FormClose
  4464. }
  4465. else
  4466. {
  4467. CanClose =
  4468. (Result == qaOK) ||
  4469. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4470. (Result == qaNeverAskAgain);
  4471. }
  4472. }
  4473. if (CanClose)
  4474. {
  4475. CanClose = CanCloseQueue();
  4476. }
  4477. }
  4478. if (CanClose)
  4479. {
  4480. CanClose =
  4481. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4482. FEditorManager->CloseExternalFilesWithoutProcess();
  4483. if (CanClose)
  4484. {
  4485. while (CanClose && (FLocalEditors->Count > 0))
  4486. {
  4487. int PrevCount = FLocalEditors->Count;
  4488. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4489. CanClose = (FLocalEditors->Count < PrevCount);
  4490. }
  4491. if (CanClose)
  4492. {
  4493. CanClose = FEditorManager->Empty(true);
  4494. if (!CanClose)
  4495. {
  4496. SetFocus();
  4497. CanClose =
  4498. (MessageDialog(
  4499. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4500. }
  4501. }
  4502. }
  4503. }
  4504. }
  4505. //---------------------------------------------------------------------------
  4506. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4507. {
  4508. TForm * Form = dynamic_cast<TForm *>(Sender);
  4509. DebugAssert(Form != NULL);
  4510. Form->Close();
  4511. }
  4512. //---------------------------------------------------------------------------
  4513. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4514. {
  4515. TForm * Form = dynamic_cast<TForm *>(Sender);
  4516. delete Form;
  4517. }
  4518. //---------------------------------------------------------------------------
  4519. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4520. {
  4521. while (FLocalEditors->Count > 0)
  4522. {
  4523. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4524. }
  4525. }
  4526. //---------------------------------------------------------------------------
  4527. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4528. TObject *Sender)
  4529. {
  4530. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4531. try
  4532. {
  4533. SetProperties(osRemote, FileList);
  4534. }
  4535. __finally
  4536. {
  4537. delete FileList;
  4538. }
  4539. }
  4540. //---------------------------------------------------------------------------
  4541. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4542. {
  4543. if (value)
  4544. {
  4545. if (Component == fcCustomCommandsBand)
  4546. {
  4547. UpdateCustomCommandsToolbar();
  4548. }
  4549. }
  4550. }
  4551. //---------------------------------------------------------------------------
  4552. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4553. {
  4554. TControl * Control = GetComponent(Component);
  4555. DebugAssert(Control);
  4556. bool Changed = (Control->Visible != value);
  4557. if (Changed)
  4558. {
  4559. ComponentShowing(Component, value);
  4560. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4561. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4562. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4563. if (value)
  4564. {
  4565. int RemainingHeight = Control->Parent->ClientHeight;
  4566. int RemainingWidth = Control->Parent->ClientWidth;
  4567. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4568. {
  4569. TControl * ChildControl = Control->Parent->Controls[i];
  4570. if (ChildControl->Visible)
  4571. {
  4572. switch (ChildControl->Align)
  4573. {
  4574. case alTop:
  4575. case alBottom:
  4576. RemainingHeight -= ChildControl->Height;
  4577. break;
  4578. case alLeft:
  4579. case alRight:
  4580. RemainingWidth -= ChildControl->Width;
  4581. break;
  4582. }
  4583. }
  4584. }
  4585. int Reserve = ScaleByTextHeight(this, 32);
  4586. // queue in explorer, trees in commander
  4587. if (Control->Height > RemainingHeight - Reserve)
  4588. {
  4589. Control->Height = RemainingHeight / 2;
  4590. }
  4591. if (Control->Width > RemainingWidth - Reserve)
  4592. {
  4593. Control->Width = RemainingWidth / 2;
  4594. }
  4595. }
  4596. Control->Visible = value;
  4597. if (WasFocused && Visible)
  4598. {
  4599. DirView(osCurrent)->SetFocus();
  4600. }
  4601. FixControlsPlacement();
  4602. }
  4603. }
  4604. //---------------------------------------------------------------------------
  4605. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4606. {
  4607. TControl * Control = GetComponent(Component);
  4608. if (Control == NULL)
  4609. {
  4610. return false;
  4611. }
  4612. else
  4613. {
  4614. return Control->Visible;
  4615. }
  4616. }
  4617. //---------------------------------------------------------------------------
  4618. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4619. {
  4620. bool Result;
  4621. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4622. {
  4623. Result = !IsUWP();
  4624. }
  4625. else
  4626. {
  4627. Result = true;
  4628. }
  4629. return Result;
  4630. }
  4631. //---------------------------------------------------------------------------
  4632. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4633. {
  4634. if (RemoteDirView->ItemFocused != NULL)
  4635. {
  4636. RemoteDirView->ItemFocused->MakeVisible(false);
  4637. }
  4638. QueueSplitter->Visible = QueuePanel->Visible;
  4639. RemotePanelSplitter->Visible = RemoteDriveView->Visible;
  4640. }
  4641. //---------------------------------------------------------------------------
  4642. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4643. {
  4644. switch (Component) {
  4645. case fcStatusBar: return RemoteStatusBar;
  4646. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4647. case fcQueueView: return QueuePanel;
  4648. case fcQueueToolbar: return QueueDock;
  4649. case fcRemoteTree: return RemoteDriveView;
  4650. case fcSessionsTabs: return SessionsPageControl;
  4651. default: return NULL;
  4652. }
  4653. }
  4654. //---------------------------------------------------------------------------
  4655. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4656. TObject *Sender, TListColumn *Column, TPoint &Point)
  4657. {
  4658. DebugAssert(NonVisualDataModule && Column && Sender);
  4659. NonVisualDataModule->ListColumn = Column;
  4660. TPopupMenu * DirViewColumnMenu;
  4661. if (Sender == RemoteDirView)
  4662. {
  4663. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4664. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4665. (Column->Index == uvName);
  4666. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4667. (Column->Index == uvSize);
  4668. }
  4669. else
  4670. {
  4671. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4672. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4673. (Column->Index == dvName);
  4674. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4675. (Column->Index == dvSize);
  4676. }
  4677. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  4678. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  4679. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  4680. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4681. }
  4682. //---------------------------------------------------------------------------
  4683. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4684. TObject *Sender, TListItem *Item, bool &AllowExec)
  4685. {
  4686. DoDirViewExecFile(Sender, Item, AllowExec);
  4687. }
  4688. //---------------------------------------------------------------------------
  4689. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4690. TListItem * Item, bool & AllowExec)
  4691. {
  4692. DebugAssert(Sender && Item && Configuration);
  4693. DebugAssert(AllowExec);
  4694. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  4695. bool Remote = (ADirView == DirView(osRemote));
  4696. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks;
  4697. TOperationSide Side = (Remote ? osRemote : osLocal);
  4698. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  4699. // on files only (not directories)
  4700. // and only when symlinks are resolved (apply to remote panel only)
  4701. if (!ADirView->ItemIsDirectory(Item) &&
  4702. (ResolvedSymlinks || FForceExecution || Terminal->IsEncryptingFiles()))
  4703. {
  4704. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  4705. !FForceExecution &&
  4706. (ResolvedSymlinks || Terminal->IsEncryptingFiles()))
  4707. {
  4708. if (WinConfiguration->DoubleClickAction == dcaCopy)
  4709. {
  4710. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  4711. UnlockWindow();
  4712. try
  4713. {
  4714. ExecuteFileOperation(
  4715. foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  4716. }
  4717. __finally
  4718. {
  4719. LockWindow();
  4720. }
  4721. AllowExec = false;
  4722. }
  4723. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  4724. {
  4725. if (!Remote || !WinConfiguration->DisableOpenEdit)
  4726. {
  4727. ExecuteFile(Side, efDefaultEditor);
  4728. AllowExec = false;
  4729. }
  4730. }
  4731. else
  4732. {
  4733. DebugFail();
  4734. }
  4735. }
  4736. // if we have not done anything special, fall back to default behavior
  4737. if (AllowExec)
  4738. {
  4739. if (Remote && !WinConfiguration->DisableOpenEdit)
  4740. {
  4741. ExecuteFile(osRemote, efShell);
  4742. AllowExec = false;
  4743. }
  4744. }
  4745. }
  4746. }
  4747. //---------------------------------------------------------------------------
  4748. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  4749. {
  4750. return ((Side == osRemote) || (Side == osCurrent));
  4751. }
  4752. //---------------------------------------------------------------------------
  4753. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  4754. {
  4755. DebugFail();
  4756. }
  4757. //---------------------------------------------------------------------------
  4758. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  4759. {
  4760. DebugFail();
  4761. }
  4762. //---------------------------------------------------------------------------
  4763. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  4764. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, bool UseDefaults)
  4765. {
  4766. TSynchronizeParamType Params;
  4767. Params.LocalDirectory = LocalDirectory;
  4768. Params.RemoteDirectory = RemoteDirectory;
  4769. int UnusedParams =
  4770. (GUIConfiguration->SynchronizeParams &
  4771. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize));
  4772. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  4773. Params.Options = GUIConfiguration->SynchronizeOptions;
  4774. bool SaveSettings = false;
  4775. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  4776. &DoSynchronizeTooManyDirectories);
  4777. DebugAssert(FSynchronizeController == NULL);
  4778. FSynchronizeController = &Controller;
  4779. bool Result;
  4780. try
  4781. {
  4782. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4783. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  4784. int Options =
  4785. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  4786. DebugAssert(FOnFeedSynchronizeError == NULL);
  4787. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  4788. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  4789. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  4790. if (Result)
  4791. {
  4792. if (SaveSettings)
  4793. {
  4794. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  4795. GUIConfiguration->SynchronizeOptions = Params.Options;
  4796. }
  4797. else
  4798. {
  4799. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  4800. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  4801. FLAGSET(Params.Options, soSynchronize))
  4802. {
  4803. GUIConfiguration->SynchronizeOptions =
  4804. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  4805. soSynchronize;
  4806. }
  4807. }
  4808. LocalDirectory = Params.LocalDirectory;
  4809. RemoteDirectory = Params.RemoteDirectory;
  4810. }
  4811. }
  4812. __finally
  4813. {
  4814. FSynchronizeController = NULL;
  4815. DebugAssert(FOnFeedSynchronizeError == NULL);
  4816. FOnFeedSynchronizeError = NULL;
  4817. }
  4818. return Result;
  4819. }
  4820. //---------------------------------------------------------------------------
  4821. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  4822. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  4823. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  4824. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  4825. TSynchronizeOptions * Options, bool Full)
  4826. {
  4827. // Keep-up-to-date mode
  4828. if (Terminal->Status == ssOpened)
  4829. {
  4830. try
  4831. {
  4832. int PParams = Params.Params;
  4833. if (!Full)
  4834. {
  4835. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  4836. if (FLAGSET(Params.Options, soRecurse))
  4837. {
  4838. PParams |= TTerminal::spSubDirs;
  4839. }
  4840. }
  4841. else
  4842. {
  4843. // if keepuptodate is non-recursive,
  4844. // full sync before has to be non-recursive as well
  4845. if (FLAGCLEAR(Params.Options, soRecurse))
  4846. {
  4847. PParams |= TTerminal::spNoRecurse;
  4848. }
  4849. }
  4850. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  4851. PParams, Checklist, Options);
  4852. }
  4853. catch (Exception & E)
  4854. {
  4855. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  4856. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  4857. {
  4858. // noop, already logged in MoreMessageDialog
  4859. }
  4860. else
  4861. {
  4862. ShowExtendedExceptionEx(Terminal, &E);
  4863. throw;
  4864. }
  4865. }
  4866. }
  4867. }
  4868. //---------------------------------------------------------------------------
  4869. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  4870. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  4871. const UnicodeString ErrorStr)
  4872. {
  4873. if (!Directory.IsEmpty())
  4874. {
  4875. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  4876. }
  4877. else
  4878. {
  4879. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  4880. }
  4881. }
  4882. //---------------------------------------------------------------------------
  4883. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  4884. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  4885. {
  4886. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  4887. {
  4888. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  4889. }
  4890. else
  4891. {
  4892. TMessageParams Params(mpNeverAskAgainCheck);
  4893. unsigned int Result = MessageDialog(
  4894. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  4895. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  4896. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  4897. {
  4898. MaxDirectories *= 2;
  4899. if (Result == qaNeverAskAgain)
  4900. {
  4901. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  4902. }
  4903. }
  4904. else
  4905. {
  4906. Abort();
  4907. }
  4908. }
  4909. }
  4910. //---------------------------------------------------------------------------
  4911. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  4912. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  4913. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  4914. TSynchronizeOptions * Options)
  4915. {
  4916. // Keep-up-to-date mode
  4917. DebugAssert(!FAutoOperation);
  4918. void * BatchStorage;
  4919. BatchStart(BatchStorage);
  4920. FAutoOperation = true;
  4921. bool AnyOperation = false;
  4922. TDateTime StartTime = Now();
  4923. TSynchronizeChecklist * AChecklist = NULL;
  4924. try
  4925. {
  4926. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  4927. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  4928. {
  4929. FSynchronizeProgressForm->Start();
  4930. }
  4931. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  4932. static_cast<TTerminal::TSynchronizeMode>(Mode),
  4933. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  4934. Options);
  4935. SAFE_DESTROY(FSynchronizeProgressForm);
  4936. AnyOperation = (AChecklist->CheckedCount > 0);
  4937. if (AnyOperation)
  4938. {
  4939. TSynchronizeProgress SynchronizeProgress(AChecklist);
  4940. CreateProgressForm(&SynchronizeProgress);
  4941. try
  4942. {
  4943. Terminal->SynchronizeApply(
  4944. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  4945. SynchronizeProcessedItem, NULL, NULL, NULL);
  4946. }
  4947. __finally
  4948. {
  4949. DestroyProgressForm();
  4950. }
  4951. }
  4952. }
  4953. __finally
  4954. {
  4955. if (Checklist == NULL)
  4956. {
  4957. delete AChecklist;
  4958. }
  4959. else
  4960. {
  4961. *Checklist = AChecklist;
  4962. }
  4963. FAutoOperation = false;
  4964. SAFE_DESTROY(FSynchronizeProgressForm);
  4965. BatchEnd(BatchStorage);
  4966. ReloadLocalDirectory();
  4967. if (AnyOperation)
  4968. {
  4969. OperationComplete(StartTime);
  4970. }
  4971. }
  4972. }
  4973. //---------------------------------------------------------------------------
  4974. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  4975. {
  4976. // can be called from command line
  4977. return Visible &&
  4978. ((DirView(osRemote)->SelCount > 0) ||
  4979. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  4980. }
  4981. //---------------------------------------------------------------------------
  4982. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  4983. {
  4984. LogSynchronizeEvent(Terminal, Message);
  4985. }
  4986. //---------------------------------------------------------------------------
  4987. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  4988. int Params, TSynchronizeOptions & Options)
  4989. {
  4990. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  4991. {
  4992. Options.Filter = new TStringList();
  4993. Options.Filter->CaseSensitive = false;
  4994. Options.Filter->Duplicates = Types::dupAccept;
  4995. if (DirView(osRemote)->SelCount > 0)
  4996. {
  4997. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  4998. }
  4999. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5000. {
  5001. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5002. }
  5003. Options.Filter->Sort();
  5004. }
  5005. }
  5006. //---------------------------------------------------------------------------
  5007. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5008. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5009. {
  5010. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5011. UnicodeString AdditionalParams =
  5012. FORMAT(L"%s \"%s\" \"%s\" %d %d %s", (
  5013. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5014. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options,
  5015. TProgramParams::FormatSwitch(DEFAULTS_SWITCH)));
  5016. TCopyParamType Defaults;
  5017. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5018. ConfigStorage->AccessMode = smRead;
  5019. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false, false))
  5020. {
  5021. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5022. }
  5023. ConfigStorage.reset(NULL);
  5024. std::unique_ptr<TStringList> Options(new TStringList());
  5025. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5026. GUIConfiguration->SaveCopyParam(OptionsStorage.get(), CopyParams, &Defaults);
  5027. if (Options->Count > 0)
  5028. {
  5029. AdditionalParams +=
  5030. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAW_CONFIG_SWITCH), StringsToParams(Options.get())));
  5031. }
  5032. ExecuteNewInstance(SessionName, AdditionalParams);
  5033. }
  5034. //---------------------------------------------------------------------------
  5035. struct TSynchronizeParams
  5036. {
  5037. UnicodeString LocalDirectory;
  5038. UnicodeString RemoteDirectory;
  5039. TSynchronizeMode Mode;
  5040. TSynchronizeChecklist * Checklist;
  5041. int Params;
  5042. TCopyParamType * CopyParam;
  5043. TDateTime CollectElapsed;
  5044. TDateTime * StartTime;
  5045. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5046. };
  5047. //---------------------------------------------------------------------------
  5048. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5049. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5050. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5051. {
  5052. DebugAssert(!FAutoOperation);
  5053. void * BatchStorage;
  5054. BatchStart(BatchStorage);
  5055. FAutoOperation = true;
  5056. TFileOperationStatistics Statistics;
  5057. TDateTime Start = Now();
  5058. try
  5059. {
  5060. Params.OnProcessedItem = OnProcessedItem;
  5061. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5062. CreateProgressForm(&SynchronizeProgress);
  5063. Terminal->SynchronizeApply(
  5064. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5065. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5066. &Statistics);
  5067. }
  5068. __finally
  5069. {
  5070. Params.OnProcessedItem = NULL;
  5071. FAutoOperation = false;
  5072. DestroyProgressForm();
  5073. BatchEnd(BatchStorage);
  5074. ReloadLocalDirectory();
  5075. }
  5076. if (WinConfiguration->SynchronizeSummary)
  5077. {
  5078. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5079. // The statistics should be 0 anyway in this case
  5080. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5081. {
  5082. Message += L"\n";
  5083. if (Statistics.FilesUploaded > 0)
  5084. {
  5085. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5086. }
  5087. if (Statistics.FilesDownloaded > 0)
  5088. {
  5089. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5090. }
  5091. if (Statistics.FilesDeletedLocal > 0)
  5092. {
  5093. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5094. }
  5095. if (Statistics.FilesDeletedRemote > 0)
  5096. {
  5097. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5098. }
  5099. }
  5100. TDateTime Elapsed = (Now() - Start);
  5101. Message +=
  5102. L"\n" +
  5103. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5104. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5105. TMessageParams Params(mpNeverAskAgainCheck);
  5106. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5107. if (Result == qaNeverAskAgain)
  5108. {
  5109. WinConfiguration->SynchronizeSummary = false;
  5110. }
  5111. }
  5112. }
  5113. //---------------------------------------------------------------------------
  5114. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5115. {
  5116. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5117. {
  5118. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5119. }
  5120. // Not set in keep-up-to-date mode - Synchronize() method
  5121. if (Token != NULL)
  5122. {
  5123. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5124. if (Params.OnProcessedItem != NULL)
  5125. {
  5126. Params.OnProcessedItem(NULL, ChecklistItem);
  5127. }
  5128. }
  5129. }
  5130. //---------------------------------------------------------------------------
  5131. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5132. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5133. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5134. {
  5135. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5136. *Params.StartTime = Now();
  5137. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5138. }
  5139. //---------------------------------------------------------------------------
  5140. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5141. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5142. {
  5143. // terminal can be already closed (e.g. dropped connection)
  5144. if (Terminal != NULL)
  5145. {
  5146. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5147. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5148. }
  5149. }
  5150. //---------------------------------------------------------------------------
  5151. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5152. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5153. TSynchronizeMode & Mode, bool & SaveMode, bool UseDefaults)
  5154. {
  5155. int Result;
  5156. int Params = GUIConfiguration->SynchronizeParams;
  5157. bool SaveSettings = false;
  5158. int Options =
  5159. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5160. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5161. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5162. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5163. bool Continue =
  5164. UseDefaults ||
  5165. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5166. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs);
  5167. if (Continue)
  5168. {
  5169. Configuration->Usage->Inc(L"Synchronizations");
  5170. UpdateCopyParamCounters(CopyParam);
  5171. TSynchronizeOptions SynchronizeOptions;
  5172. GetSynchronizeOptions(Params, SynchronizeOptions);
  5173. if (SaveSettings)
  5174. {
  5175. GUIConfiguration->SynchronizeParams = Params;
  5176. }
  5177. else
  5178. {
  5179. SaveMode = false;
  5180. }
  5181. TDateTime StartTime = Now();
  5182. TSynchronizeChecklist * Checklist = NULL;
  5183. try
  5184. {
  5185. DebugAssert(!FAutoOperation);
  5186. FAutoOperation = true;
  5187. try
  5188. {
  5189. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  5190. FSynchronizeProgressForm->Start();
  5191. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5192. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5193. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5194. &SynchronizeOptions);
  5195. }
  5196. __finally
  5197. {
  5198. FAutoOperation = false;
  5199. SAFE_DESTROY(FSynchronizeProgressForm);
  5200. }
  5201. TSynchronizeParams SynchronizeParams;
  5202. SynchronizeParams.LocalDirectory = LocalDirectory;
  5203. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5204. SynchronizeParams.Mode = Mode;
  5205. SynchronizeParams.CopyParam = &CopyParam;
  5206. SynchronizeParams.Params = Params;
  5207. SynchronizeParams.Checklist = Checklist;
  5208. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5209. SynchronizeParams.StartTime = &StartTime;
  5210. SynchronizeParams.OnProcessedItem = NULL;
  5211. Result = Checklist->Count;
  5212. if (Checklist->Count > 0)
  5213. {
  5214. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5215. {
  5216. if (!DoSynchronizeChecklistDialog(
  5217. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5218. DoSynchronizeChecklistCalculateSize, &SynchronizeParams))
  5219. {
  5220. Result = -1;
  5221. }
  5222. }
  5223. else
  5224. {
  5225. FullSynchronize(SynchronizeParams, NULL, NULL);
  5226. }
  5227. }
  5228. else
  5229. {
  5230. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5231. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5232. }
  5233. }
  5234. __finally
  5235. {
  5236. delete Checklist;
  5237. }
  5238. OperationComplete(StartTime);
  5239. }
  5240. else
  5241. {
  5242. Result = -1;
  5243. }
  5244. return Result;
  5245. }
  5246. //---------------------------------------------------------------------------
  5247. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5248. const UnicodeString LocalDirectory, const UnicodeString RemoteDirectory,
  5249. bool & Continue, bool Collect)
  5250. {
  5251. if (Collect)
  5252. {
  5253. DebugAssert(FSynchronizeProgressForm != NULL);
  5254. if (!FSynchronizeProgressForm->Started)
  5255. {
  5256. FSynchronizeProgressForm->Start();
  5257. }
  5258. FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Continue);
  5259. }
  5260. else
  5261. {
  5262. DebugAssert(FSynchronizeProgressForm == NULL);
  5263. DebugAssert(FProgressForm != NULL);
  5264. }
  5265. }
  5266. //---------------------------------------------------------------------------
  5267. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5268. {
  5269. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  5270. TRemoteFile * File = NULL;
  5271. Terminal->ReadFile(FullFileName, File);
  5272. if (File != NULL)
  5273. {
  5274. std::unique_ptr<TRemoteFile> FileOwner(File);
  5275. TAutoFlag Flag(FStandaloneEditing);
  5276. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5277. Application->ShowMainForm = false;
  5278. Application->Run();
  5279. }
  5280. }
  5281. //---------------------------------------------------------------------------
  5282. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  5283. {
  5284. DebugFail();
  5285. }
  5286. //---------------------------------------------------------------------------
  5287. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5288. {
  5289. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5290. SessionData->Assign(Terminal->SessionData);
  5291. UpdateSessionData(SessionData.get());
  5292. TTerminalManager::Instance()->UpdateSessionCredentials(SessionData.get());
  5293. if (!Terminal->SessionData->HasSessionName())
  5294. {
  5295. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5296. // once IsWorkspace is cleared
  5297. SessionData->Name = UnicodeString();
  5298. SessionData->IsWorkspace = false;
  5299. }
  5300. return SessionData.release();
  5301. }
  5302. //---------------------------------------------------------------------------
  5303. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5304. {
  5305. TSessionData * SessionData = CloneCurrentSessionData();
  5306. try
  5307. {
  5308. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5309. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5310. }
  5311. __finally
  5312. {
  5313. delete SessionData;
  5314. }
  5315. }
  5316. //---------------------------------------------------------------------------
  5317. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5318. {
  5319. TTerminalManager * Manager = TTerminalManager::Instance();
  5320. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5321. if (DebugAlwaysTrue(Terminal != NULL))
  5322. {
  5323. // Update (Managed)Terminal->StateData
  5324. UpdateTerminal(Terminal);
  5325. }
  5326. Manager->SaveWorkspace(DataList.get());
  5327. return DataList.release();
  5328. }
  5329. //---------------------------------------------------------------------------
  5330. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5331. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5332. {
  5333. WinConfiguration->LastWorkspace = Name;
  5334. if (!SavePasswords)
  5335. {
  5336. for (int Index = 0; Index < DataList->Count; Index++)
  5337. {
  5338. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5339. if (SessionData->Link.IsEmpty())
  5340. {
  5341. SessionData->ClearSessionPasswords();
  5342. }
  5343. }
  5344. }
  5345. StoredSessions->NewWorkspace(Name, DataList);
  5346. // modified only
  5347. StoredSessions->Save(false, Explicit);
  5348. }
  5349. //---------------------------------------------------------------------------
  5350. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5351. {
  5352. return
  5353. DefaultStr(WinConfiguration->LastWorkspace,
  5354. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5355. }
  5356. //---------------------------------------------------------------------------
  5357. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5358. {
  5359. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5360. bool AnyNonStoredSessionWithPassword = false;
  5361. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5362. bool AllNonStoredSessionsAnonymous = true;
  5363. TTerminalManager * Manager = TTerminalManager::Instance();
  5364. for (int Index = 0; Index < DataList->Count; Index++)
  5365. {
  5366. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5367. if (SessionData->Link.IsEmpty())
  5368. {
  5369. if (SessionData->HasAnySessionPassword())
  5370. {
  5371. AnyNonStoredSessionWithPassword = true;
  5372. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  5373. {
  5374. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5375. }
  5376. }
  5377. if (!SameText(SessionData->UserName, AnonymousUserName))
  5378. {
  5379. AllNonStoredSessionsAnonymous = false;
  5380. }
  5381. }
  5382. }
  5383. bool SavePasswords;
  5384. bool * PSavePasswords;
  5385. bool NotRecommendedSavingPasswords =
  5386. !CustomWinConfiguration->UseMasterPassword &&
  5387. !AllNonStoredSessionsAnonymous;
  5388. if (Configuration->DisablePasswordStoring ||
  5389. !AnyNonStoredSessionWithPassword)
  5390. {
  5391. PSavePasswords = NULL;
  5392. SavePasswords = false;
  5393. }
  5394. else
  5395. {
  5396. PSavePasswords = &SavePasswords;
  5397. SavePasswords =
  5398. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5399. !NotRecommendedSavingPasswords;
  5400. }
  5401. UnicodeString Name = WorkspaceName();
  5402. bool CreateShortcut = false;
  5403. bool Result =
  5404. DoSaveWorkspaceDialog(
  5405. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5406. EnableAutoSave);
  5407. if (Result)
  5408. {
  5409. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5410. if (CreateShortcut)
  5411. {
  5412. TOperationVisualizer Visualizer;
  5413. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5414. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5415. }
  5416. if (EnableAutoSave)
  5417. {
  5418. WinConfiguration->AutoSaveWorkspace = true;
  5419. WinConfiguration->AutoWorkspace = Name;
  5420. if (PSavePasswords != NULL)
  5421. {
  5422. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5423. }
  5424. }
  5425. }
  5426. return Result;
  5427. }
  5428. //---------------------------------------------------------------------------
  5429. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TTerminal * Terminal)
  5430. {
  5431. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  5432. DebugAssert(ManagedTerminal != NULL);
  5433. SAFE_DESTROY(ManagedTerminal->RemoteExplorerState);
  5434. if (WinConfiguration->PreservePanelState)
  5435. {
  5436. ManagedTerminal->RemoteExplorerState = RemoteDirView->SaveState();
  5437. }
  5438. UpdateSessionData(ManagedTerminal->StateData);
  5439. }
  5440. //---------------------------------------------------------------------------
  5441. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5442. {
  5443. // Keep in sync with TSessionData::CopyStateData
  5444. DebugAssert(Data != NULL);
  5445. // This is inconsistent with how color (for example) is handled.
  5446. Data->Name = Terminal->SessionData->Name;
  5447. // cannot use RemoteDirView->Path, because it is empty if connection
  5448. // was already closed
  5449. // also only peek, we may not be connected at all atm
  5450. // so make sure we do not try retrieving current directory from the server
  5451. // (particularly with FTP)
  5452. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5453. if (!ACurrentDirectory.IsEmpty())
  5454. {
  5455. Data->RemoteDirectory = ACurrentDirectory;
  5456. }
  5457. Data->Color = SessionColor;
  5458. }
  5459. //---------------------------------------------------------------------------
  5460. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5461. {
  5462. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5463. DebugAssert(Toolbar != NULL);
  5464. for (int i = 0; i < Toolbar->Items->Count; i++)
  5465. {
  5466. TTBXCustomDropDownItem * DropDownItem;
  5467. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5468. if (DropDownItem != NULL)
  5469. {
  5470. ToolbarItemResize(DropDownItem,
  5471. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5472. Toolbar->NonClientWidth);
  5473. break;
  5474. }
  5475. }
  5476. }
  5477. //---------------------------------------------------------------------------
  5478. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5479. {
  5480. Item->EditWidth = Width;
  5481. }
  5482. //---------------------------------------------------------------------------
  5483. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5484. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5485. {
  5486. for (int i = 0; i < Toolbar->Items->Count; i++)
  5487. {
  5488. TTBXCustomDropDownItem * DropDownItem;
  5489. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5490. if (DropDownItem != NULL)
  5491. {
  5492. ASize.x -= DropDownItem->EditWidth;
  5493. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5494. }
  5495. }
  5496. }
  5497. //---------------------------------------------------------------------------
  5498. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5499. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5500. {
  5501. if (WinConfiguration->DDWarnLackOfTempSpace)
  5502. {
  5503. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5504. if (!ADrive.IsEmpty())
  5505. {
  5506. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5507. DebugAssert(RequiredSpace >= 0);
  5508. __int64 RequiredWithReserve;
  5509. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5510. if (FreeSpace < RequiredWithReserve)
  5511. {
  5512. unsigned int Result;
  5513. TMessageParams Params(mpNeverAskAgainCheck);
  5514. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5515. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5516. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5517. if (Result == qaNeverAskAgain)
  5518. {
  5519. WinConfiguration->DDWarnLackOfTempSpace = false;
  5520. }
  5521. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5522. }
  5523. }
  5524. }
  5525. }
  5526. //---------------------------------------------------------------------------
  5527. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5528. {
  5529. DoOpenDirectoryDialog(odAddBookmark, Side);
  5530. }
  5531. //---------------------------------------------------------------------------
  5532. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5533. {
  5534. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5535. TStringList * VisitedDirectories = CreateSortedStringList((Side == osRemote));
  5536. try
  5537. {
  5538. TCustomDirView * DView = DirView(Side);
  5539. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5540. {
  5541. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5542. }
  5543. }
  5544. catch (...)
  5545. {
  5546. delete VisitedDirectories;
  5547. throw;
  5548. }
  5549. return VisitedDirectories;
  5550. }
  5551. //---------------------------------------------------------------------------
  5552. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5553. TOpenDirectoryMode Mode, TOperationSide Side)
  5554. {
  5555. bool Continue = true;
  5556. if (Mode == odAddBookmark)
  5557. {
  5558. TMessageParams Params(mpNeverAskAgainCheck);
  5559. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5560. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5561. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  5562. unsigned int Answer =
  5563. MessageDialog(Message,
  5564. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  5565. if (Answer == qaNeverAskAgain)
  5566. {
  5567. Continue = true;
  5568. WinConfiguration->UseSharedBookmarks = true;
  5569. }
  5570. else if (Answer == qaOK)
  5571. {
  5572. Continue = true;
  5573. WinConfiguration->UseSharedBookmarks = false;
  5574. }
  5575. else
  5576. {
  5577. Continue = false;
  5578. }
  5579. }
  5580. if (Continue)
  5581. {
  5582. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  5583. try
  5584. {
  5585. UnicodeString Name = DirView(Side)->PathName;
  5586. bool Repeat;
  5587. do
  5588. {
  5589. Repeat = false;
  5590. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  5591. // do not allow switching to location profiles,
  5592. // if we are not connected
  5593. HasDirView[osLocal] && (Terminal != NULL)))
  5594. {
  5595. TWindowLock Lock(this);
  5596. if (!TryOpenDirectory(Side, Name))
  5597. {
  5598. Repeat = true;
  5599. Mode = odBrowse;
  5600. }
  5601. }
  5602. }
  5603. while (Repeat);
  5604. }
  5605. __finally
  5606. {
  5607. delete VisitedDirectories;
  5608. }
  5609. }
  5610. }
  5611. //---------------------------------------------------------------------------
  5612. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  5613. {
  5614. DebugAssert(!FTerminal->CommandSessionOpened);
  5615. return TTerminalManager::Instance()->ConnectTerminal(FTerminal->CommandSession);
  5616. }
  5617. //---------------------------------------------------------------------------
  5618. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  5619. {
  5620. bool Result = FTerminal->IsCapable[Capability] ||
  5621. FTerminal->CommandSessionOpened;
  5622. if (!Result)
  5623. {
  5624. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  5625. if (!GUIConfiguration->ConfirmCommandSession)
  5626. {
  5627. Result = true;
  5628. }
  5629. else
  5630. {
  5631. TMessageParams Params(mpNeverAskAgainCheck);
  5632. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  5633. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  5634. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  5635. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  5636. if (Answer == qaNeverAskAgain)
  5637. {
  5638. GUIConfiguration->ConfirmCommandSession = false;
  5639. Result = true;
  5640. }
  5641. else if (Answer == qaOK)
  5642. {
  5643. Result = true;
  5644. }
  5645. }
  5646. if (Result)
  5647. {
  5648. Result = CommandSessionFallback();
  5649. }
  5650. }
  5651. return Result;
  5652. }
  5653. //---------------------------------------------------------------------------
  5654. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  5655. {
  5656. if (EnsureCommandSessionFallback(fcAnyCommand))
  5657. {
  5658. DoConsoleDialog(Terminal, Command);
  5659. }
  5660. }
  5661. //---------------------------------------------------------------------------
  5662. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  5663. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  5664. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  5665. {
  5666. if (IsFileControl(DropSourceControl, osRemote) &&
  5667. (FDDExtMapFile != NULL))
  5668. {
  5669. Accept = true;
  5670. }
  5671. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  5672. DebugAssert(FDDTargetControl != NULL);
  5673. }
  5674. //---------------------------------------------------------------------------
  5675. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  5676. _di_IDataObject DataObj, int KeyState,
  5677. const TPoint & Point, int & Effect, bool & Accept)
  5678. {
  5679. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  5680. }
  5681. //---------------------------------------------------------------------------
  5682. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  5683. _di_IDataObject DataObj, int KeyState,
  5684. const TPoint & Point, int & Effect, bool & Accept)
  5685. {
  5686. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  5687. }
  5688. //---------------------------------------------------------------------------
  5689. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  5690. TObject * Sender)
  5691. {
  5692. DebugUsedParam(Sender);
  5693. DebugAssert(FDDTargetControl == Sender);
  5694. FDDTargetControl = NULL;
  5695. }
  5696. //---------------------------------------------------------------------------
  5697. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  5698. {
  5699. FileControlDDDragLeave(SessionsPageControl);
  5700. }
  5701. //---------------------------------------------------------------------------
  5702. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  5703. {
  5704. FileControlDDDragLeave(QueueView3);
  5705. }
  5706. //---------------------------------------------------------------------------
  5707. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  5708. {
  5709. DebugAssert(GetSide(Side) == osRemote);
  5710. DebugUsedParam(Side);
  5711. bool Edit = false;
  5712. TRemoteFile * File = NULL;
  5713. UnicodeString FileName;
  5714. UnicodeString PointTo;
  5715. bool SymbolicLink = true;
  5716. if (RemoteDirView->ItemFocused)
  5717. {
  5718. DebugAssert(RemoteDirView->ItemFocused->Data);
  5719. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  5720. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  5721. if (Edit)
  5722. {
  5723. FileName = File->FileName;
  5724. PointTo = File->LinkTo;
  5725. }
  5726. else
  5727. {
  5728. PointTo = File->FileName;
  5729. }
  5730. }
  5731. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  5732. Terminal->IsCapable[fcHardLink]))
  5733. {
  5734. Configuration->Usage->Inc(L"RemoteLinksCreated");
  5735. if (Edit)
  5736. {
  5737. DebugAssert(File->FileName == FileName);
  5738. int Params = dfNoRecursive;
  5739. Terminal->ExceptionOnFail = true;
  5740. try
  5741. {
  5742. Terminal->DeleteFile(L"", File, &Params);
  5743. }
  5744. __finally
  5745. {
  5746. Terminal->ExceptionOnFail = false;
  5747. }
  5748. }
  5749. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  5750. }
  5751. }
  5752. //---------------------------------------------------------------------------
  5753. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  5754. {
  5755. return
  5756. (Terminal != NULL) &&
  5757. ((GetSide(Side) != osRemote) ||
  5758. (Terminal->ResolvingSymlinks &&
  5759. Terminal->IsCapable[fcSymbolicLink]));
  5760. }
  5761. //---------------------------------------------------------------------------
  5762. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  5763. {
  5764. return
  5765. (FCurrentSide == osRemote) &&
  5766. (RemoteDirView->ItemFocused != NULL) &&
  5767. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  5768. Terminal->SessionData->ResolveSymlinks;
  5769. }
  5770. //---------------------------------------------------------------------------
  5771. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  5772. {
  5773. DebugAssert(!WinConfiguration->DisableOpenEdit);
  5774. FForceExecution = true;
  5775. try
  5776. {
  5777. DirView(osCurrent)->ExecuteCurrentFile();
  5778. }
  5779. __finally
  5780. {
  5781. FForceExecution = false;
  5782. }
  5783. }
  5784. //---------------------------------------------------------------------------
  5785. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  5786. {
  5787. TEditorData ExternalEditor;
  5788. ExternalEditor.Editor = edExternal;
  5789. bool Remember = false;
  5790. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc,
  5791. (GetSide(osCurrent) == osRemote)))
  5792. {
  5793. if (Remember)
  5794. {
  5795. TEditorList * EditorList = new TEditorList;
  5796. try
  5797. {
  5798. *EditorList = *WinConfiguration->EditorList;
  5799. bool Found = false;
  5800. int i = 0;
  5801. while (!Found && (i < EditorList->Count))
  5802. {
  5803. const TEditorPreferences * Editor = EditorList->Editors[i];
  5804. if ((Editor->Data->Editor == edExternal) &&
  5805. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  5806. {
  5807. Found = true;
  5808. }
  5809. i++;
  5810. }
  5811. if (!Found)
  5812. {
  5813. EditorList->Add(new TEditorPreferences(ExternalEditor));
  5814. WinConfiguration->EditorList = EditorList;
  5815. }
  5816. }
  5817. __finally
  5818. {
  5819. delete EditorList;
  5820. }
  5821. }
  5822. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  5823. }
  5824. }
  5825. //---------------------------------------------------------------------------
  5826. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  5827. {
  5828. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  5829. if (FFileFindTerminal == Sender)
  5830. {
  5831. FFileFindTerminal = NULL;
  5832. HideFileFindDialog();
  5833. }
  5834. if (FClipboardTerminal == Sender)
  5835. {
  5836. ClipboardClear(); // implies ClipboardStop
  5837. }
  5838. }
  5839. //---------------------------------------------------------------------------
  5840. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  5841. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  5842. {
  5843. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  5844. DebugAssert(Terminal != NULL);
  5845. if (Data->Terminal == Terminal)
  5846. {
  5847. Data->Terminal = NULL;
  5848. }
  5849. }
  5850. //---------------------------------------------------------------------------
  5851. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  5852. {
  5853. UpdateControls();
  5854. SessionColor = TColor(0);
  5855. UpdateRemotePathComboBox(false);
  5856. NeedSession(true);
  5857. }
  5858. //---------------------------------------------------------------------------
  5859. void __fastcall TCustomScpExplorerForm::NeedSession(bool ReloadSessions)
  5860. {
  5861. try
  5862. {
  5863. try
  5864. {
  5865. TTerminalManager::Instance()->NewSession(false, L"", ReloadSessions, this);
  5866. }
  5867. __finally
  5868. {
  5869. if (!WinConfiguration->KeepOpenWhenNoSession &&
  5870. (!Terminal || !Terminal->Active))
  5871. {
  5872. TerminateApplication();
  5873. }
  5874. }
  5875. }
  5876. catch (EAbort &)
  5877. {
  5878. // swallow
  5879. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  5880. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  5881. }
  5882. }
  5883. //---------------------------------------------------------------------------
  5884. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged()
  5885. {
  5886. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  5887. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  5888. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  5889. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  5890. try
  5891. {
  5892. while ((SessionsPageControl->PageCount > TerminalList->Count + 1) ||
  5893. // Clear the design time unthemed tab
  5894. ((SessionsPageControl->PageCount > 0) &&
  5895. (dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[SessionsPageControl->PageCount - 1]) == NULL)))
  5896. {
  5897. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  5898. }
  5899. for (int Index = 0; Index <= TerminalList->Count; Index++)
  5900. {
  5901. TThemeTabSheet * TabSheet;
  5902. if (Index >= SessionsPageControl->PageCount)
  5903. {
  5904. TabSheet = new TThemeTabSheet(SessionsPageControl);
  5905. TabSheet->PageControl = SessionsPageControl;
  5906. }
  5907. else
  5908. {
  5909. TabSheet = DebugNotNull(dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[Index]));
  5910. }
  5911. bool IsSessionTab = (Index < TerminalList->Count);
  5912. if (IsSessionTab)
  5913. {
  5914. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  5915. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  5916. UpdateSessionTab(TabSheet);
  5917. }
  5918. else
  5919. {
  5920. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  5921. TabSheet->Tag = 0; // not really needed
  5922. // We know that we are at the last page, sotherwise we could not call this (it assumes that new session tab is the last one)
  5923. UpdateNewSessionTab();
  5924. }
  5925. TabSheet->ShowCloseButton = IsSessionTab;
  5926. }
  5927. }
  5928. __finally
  5929. {
  5930. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  5931. }
  5932. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  5933. }
  5934. //---------------------------------------------------------------------------
  5935. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  5936. {
  5937. DoTerminalListChanged();
  5938. }
  5939. //---------------------------------------------------------------------------
  5940. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  5941. {
  5942. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  5943. DebugAssert(TabSheet->ImageIndex == 0);
  5944. TabSheet->Caption =
  5945. WinConfiguration->SelectiveToolbarText ?
  5946. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  5947. UnicodeString();
  5948. }
  5949. //---------------------------------------------------------------------------
  5950. TTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  5951. {
  5952. return reinterpret_cast<TTerminal *>(TabSheet->Tag);
  5953. }
  5954. //---------------------------------------------------------------------------
  5955. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  5956. {
  5957. if (DebugAlwaysTrue(TabSheet != NULL))
  5958. {
  5959. TManagedTerminal * ManagedTerminal =
  5960. dynamic_cast<TManagedTerminal *>(GetSessionTabTerminal(TabSheet));
  5961. if (DebugAlwaysTrue(ManagedTerminal != NULL))
  5962. {
  5963. TColor Color = (ManagedTerminal == FTerminal) ? FSessionColor : ManagedTerminal->StateData->Color;
  5964. TabSheet->ImageIndex = AddSessionColor(Color);
  5965. UnicodeString TabCaption = TTerminalManager::Instance()->GetTerminalTitle(ManagedTerminal, true);
  5966. TabSheet->Caption = SessionsPageControl->FormatCaptionWithCloseButton(TabCaption);
  5967. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  5968. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  5969. {
  5970. ThemeTabSheet->Shadowed = !ManagedTerminal->Active;
  5971. }
  5972. }
  5973. }
  5974. }
  5975. //---------------------------------------------------------------------------
  5976. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  5977. {
  5978. DebugAssert(SessionsPageControl->ActivePage != NULL);
  5979. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  5980. bool Result = (Terminal != NULL);
  5981. if (Result)
  5982. {
  5983. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  5984. }
  5985. else
  5986. {
  5987. try
  5988. {
  5989. NewSession(false);
  5990. }
  5991. __finally
  5992. {
  5993. DoTerminalListChanged();
  5994. }
  5995. FSessionsPageControlNewSessionTime = Now();
  5996. }
  5997. return Result;
  5998. }
  5999. //---------------------------------------------------------------------------
  6000. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6001. {
  6002. SessionTabSwitched();
  6003. }
  6004. //---------------------------------------------------------------------------
  6005. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6006. {
  6007. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6008. DebugAssert(TransferList != NULL);
  6009. UnicodeString Name;
  6010. if (TransferList->ItemIndex <= 0)
  6011. {
  6012. Name = L"";
  6013. }
  6014. else
  6015. {
  6016. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6017. }
  6018. if (FCopyParamAutoSelected.IsEmpty())
  6019. {
  6020. // if previous preset was not autoselected, make new preset the "default"
  6021. FCopyParamDefault = Name;
  6022. }
  6023. GUIConfiguration->CopyParamCurrent = Name;
  6024. }
  6025. //---------------------------------------------------------------------------
  6026. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6027. {
  6028. // sanity check
  6029. bool ExistingPreset =
  6030. (FTransferListHoverIndex >= 0) &&
  6031. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6032. DebugAssert(ExistingPreset);
  6033. if (ExistingPreset)
  6034. {
  6035. HDC DC = GetDC(0);
  6036. TCanvas * Canvas = new TCanvas();
  6037. try
  6038. {
  6039. Canvas->Handle = DC;
  6040. Canvas->Font = GetToolbarFont(this);
  6041. UnicodeString Name;
  6042. if (FTransferListHoverIndex == 0)
  6043. {
  6044. Name = L"";
  6045. }
  6046. else
  6047. {
  6048. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6049. }
  6050. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6051. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6052. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6053. static_cast<TObject*>(GetComponent(fcTransferList)));
  6054. UnicodeString InfoStr =
  6055. GUIConfiguration->CopyParamPreset[Name].
  6056. GetInfoStr(L"; ",
  6057. FLAGMASK(Terminal != NULL, Terminal->UsableCopyParamAttrs(0).General));
  6058. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6059. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6060. {
  6061. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6062. {
  6063. InfoStr.SetLength(InfoStr.Length() - 1);
  6064. }
  6065. InfoStr += Ellipsis;
  6066. }
  6067. // UpdateCaption does not cause invalidation of whole submenu, while
  6068. // setting Caption property does.
  6069. // also it probably does not resize the label, even if necessary
  6070. // (we do not want that anyway)
  6071. TransferLabel->UpdateCaption(InfoStr);
  6072. }
  6073. __finally
  6074. {
  6075. Canvas->Handle = NULL;
  6076. ReleaseDC(0, DC);
  6077. delete Canvas;
  6078. }
  6079. }
  6080. }
  6081. //---------------------------------------------------------------------------
  6082. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6083. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6084. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6085. {
  6086. if (FTransferListHoverIndex != AHoverIndex)
  6087. {
  6088. FTransferListHoverIndex = AHoverIndex;
  6089. UpdateTransferLabel();
  6090. }
  6091. }
  6092. //---------------------------------------------------------------------------
  6093. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6094. {
  6095. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6096. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6097. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6098. {
  6099. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6100. {
  6101. OpenDirectory(GetSide(osCurrent));
  6102. Message.Result = 1;
  6103. }
  6104. else
  6105. {
  6106. TForm::Dispatch(&Message);
  6107. }
  6108. }
  6109. else
  6110. {
  6111. TForm::Dispatch(&Message);
  6112. }
  6113. }
  6114. //---------------------------------------------------------------------------
  6115. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6116. {
  6117. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6118. if ((FIgnoreNextDialogChar != 0) &&
  6119. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6120. {
  6121. Message.Result = 1;
  6122. }
  6123. else
  6124. {
  6125. TForm::Dispatch(&Message);
  6126. }
  6127. FIgnoreNextDialogChar = 0;
  6128. }
  6129. //---------------------------------------------------------------------------
  6130. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6131. {
  6132. // The four low-order bits are used internally by Windows
  6133. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6134. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6135. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6136. // 61730 - restore thru double click - undocumented
  6137. // 61490 - maximize thru double click - undocumented
  6138. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6139. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6140. {
  6141. SysResizing(Cmd);
  6142. }
  6143. TForm::Dispatch(&Message);
  6144. }
  6145. //---------------------------------------------------------------------------
  6146. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6147. {
  6148. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6149. // Also there no point returning TRUE as we are not able to
  6150. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6151. // Hence the process termination might be safer :)
  6152. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6153. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6154. {
  6155. Message.Result = FALSE;
  6156. }
  6157. else
  6158. {
  6159. Message.Result = TRUE;
  6160. }
  6161. // Do not call default handling as that triggers OnCloseQuery,
  6162. // where our implementation will popup confirmation dialogs, what we do not want,
  6163. // as per Vista guidelines:
  6164. // https://docs.microsoft.com/en-us/windows/desktop/shutdown/shutting-down
  6165. }
  6166. //---------------------------------------------------------------------------
  6167. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6168. {
  6169. if (Message.EndSession && IsApplicationMinimized())
  6170. {
  6171. // WORKAROUND
  6172. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6173. // when it receives WM_ENDSESSION,
  6174. // but that sometimes (particularly when application is minimized) cause crashes.
  6175. // Crash popups message that blocks log off.
  6176. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6177. // so we call ExitProcess() immediately, not even trying to cleanup.
  6178. // It still causes beep, so there's likely some popup, but it does not block
  6179. // log off.
  6180. ExitProcess(0);
  6181. }
  6182. TForm::Dispatch(&Message);
  6183. }
  6184. #ifdef _DEBUG
  6185. //---------------------------------------------------------------------------
  6186. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6187. {
  6188. TForm::Dispatch(&Message);
  6189. }
  6190. #endif
  6191. //---------------------------------------------------------------------------
  6192. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6193. {
  6194. }
  6195. //---------------------------------------------------------------------------
  6196. void __fastcall TCustomScpExplorerForm::DoShow()
  6197. {
  6198. // only now are the controls resized finally, so the size constraints
  6199. // will not conflict with possibly very small window size
  6200. RestoreFormParams();
  6201. FixControlsPlacement();
  6202. if (Position == poDefaultPosOnly)
  6203. {
  6204. CutFormToDesktop(this);
  6205. }
  6206. TForm::DoShow();
  6207. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6208. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6209. if (Terminal == NULL)
  6210. {
  6211. StartingDisconnected();
  6212. }
  6213. UpdatePixelsPerInchMainWindowCounter();
  6214. FShowing = true;
  6215. }
  6216. //---------------------------------------------------------------------------
  6217. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6218. {
  6219. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6220. }
  6221. //---------------------------------------------------------------------------
  6222. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  6223. {
  6224. DoTerminalListChanged();
  6225. }
  6226. //---------------------------------------------------------------------------
  6227. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6228. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6229. TNotifyEvent OnBalloonClick, TObject * UserData)
  6230. {
  6231. bool Do;
  6232. UnicodeString Message;
  6233. if (E == NULL)
  6234. {
  6235. Message = Str;
  6236. Do = true;
  6237. }
  6238. else
  6239. {
  6240. Do = ExceptionMessage(E, Message);
  6241. }
  6242. if (Do && WinConfiguration->BalloonNotifications)
  6243. {
  6244. UnicodeString Title;
  6245. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6246. {
  6247. Message = TrimLeft(Message);
  6248. }
  6249. else
  6250. {
  6251. Message = UnformatMessage(Message);
  6252. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6253. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6254. Title = LoadResourceString(Captions[Type]);
  6255. if (Terminal != NULL)
  6256. {
  6257. Title = FORMAT(L"%s - %s",
  6258. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), Title));
  6259. }
  6260. }
  6261. if (Seconds == 0)
  6262. {
  6263. Seconds = WinConfiguration->NotificationsTimeout;
  6264. }
  6265. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6266. }
  6267. }
  6268. //---------------------------------------------------------------------------
  6269. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6270. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6271. UnicodeString HelpKeyword, const TMessageParams * Params,
  6272. TTerminal * Terminal)
  6273. {
  6274. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6275. (FOnFeedSynchronizeError != NULL)) &&
  6276. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6277. {
  6278. if (FOnFeedSynchronizeError != NULL)
  6279. {
  6280. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6281. }
  6282. else
  6283. {
  6284. DebugAssert(FErrorList != NULL);
  6285. TStringList * MoreMessagesCopy = NULL;
  6286. if (MoreMessages)
  6287. {
  6288. MoreMessagesCopy = new TStringList();
  6289. MoreMessagesCopy->Assign(MoreMessages);
  6290. }
  6291. FErrorList->AddObject(Message, MoreMessagesCopy);
  6292. }
  6293. PopupTrayBalloon(Terminal, Message, Type);
  6294. return ContinueAnswer(Answers);
  6295. }
  6296. else
  6297. {
  6298. bool UseBalloon = FTrayIcon->Visible;
  6299. if (UseBalloon)
  6300. {
  6301. PopupTrayBalloon(Terminal, Message, Type);
  6302. }
  6303. unsigned int Result;
  6304. try
  6305. {
  6306. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6307. }
  6308. __finally
  6309. {
  6310. // cancel only balloon we popped up, otherwise we may cancel notification
  6311. // balloon that was there before the message dialog
  6312. // (such as "dd confirmation opt in balloon)
  6313. if (UseBalloon)
  6314. {
  6315. FTrayIcon->CancelBalloon();
  6316. }
  6317. }
  6318. return Result;
  6319. }
  6320. }
  6321. //---------------------------------------------------------------------------
  6322. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6323. TTerminal * Terminal, Exception * E)
  6324. {
  6325. if (FTrayIcon->Visible)
  6326. {
  6327. PopupTrayBalloon(Terminal, L"", qtError, E);
  6328. }
  6329. // particularly prevent opening new session from jump list,
  6330. // while exception is shown
  6331. NonVisualDataModule->StartBusy();
  6332. try
  6333. {
  6334. ShowExtendedExceptionEx(Terminal, E);
  6335. }
  6336. __finally
  6337. {
  6338. NonVisualDataModule->EndBusy();
  6339. FTrayIcon->CancelBalloon();
  6340. }
  6341. }
  6342. //---------------------------------------------------------------------------
  6343. void __fastcall TCustomScpExplorerForm::TerminalReady()
  6344. {
  6345. // cannot rely on active page being page for active terminal,
  6346. // as it can happen that active page is the "new session" page
  6347. // (e.g. when reconnecting active terminal, while login dialog
  6348. // invoked from "new session" page is modal)
  6349. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6350. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  6351. }
  6352. //---------------------------------------------------------------------------
  6353. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6354. TTerminal * Terminal, Exception * E)
  6355. {
  6356. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6357. if (!Terminal->Active)
  6358. {
  6359. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6360. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6361. {
  6362. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6363. }
  6364. }
  6365. }
  6366. //---------------------------------------------------------------------------
  6367. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6368. UnicodeString Message, TQueryType Type,
  6369. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6370. {
  6371. if ((E == NULL) ||
  6372. ExceptionMessage(E, Message))
  6373. {
  6374. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6375. if (Important)
  6376. {
  6377. Seconds *= 5;
  6378. }
  6379. UnicodeString NoteMessage(UnformatMessage(Message));
  6380. if (Terminal != NULL)
  6381. {
  6382. NoteMessage = FORMAT(L"%s: %s",
  6383. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), NoteMessage));
  6384. }
  6385. if (WinConfiguration->BalloonNotifications)
  6386. {
  6387. AddNote(NoteMessage);
  6388. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6389. }
  6390. else
  6391. {
  6392. FlashOnBackground();
  6393. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6394. }
  6395. }
  6396. }
  6397. //---------------------------------------------------------------------------
  6398. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6399. {
  6400. RestoreApp();
  6401. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6402. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6403. !NonVisualDataModule->Busy)
  6404. {
  6405. TTerminal * Terminal = TerminalNoteData->Terminal;
  6406. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6407. if (!ComponentVisible[fcQueueView])
  6408. {
  6409. ToggleQueueVisibility();
  6410. GoToQueue();
  6411. }
  6412. }
  6413. }
  6414. //---------------------------------------------------------------------------
  6415. void __fastcall TCustomScpExplorerForm::QueueEvent(TTerminal * ATerminal,
  6416. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6417. {
  6418. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  6419. UnicodeString Message;
  6420. TNotifyEvent OnClick = NULL;
  6421. TObject * UserData = NULL;
  6422. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6423. switch (Event)
  6424. {
  6425. case qeEmptyButMonitored:
  6426. if ((ATerminal != Terminal) || QueueInvisible)
  6427. {
  6428. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6429. OnClick = QueueEmptyNoteClicked;
  6430. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6431. TerminalNoteData->Terminal = ATerminal;
  6432. UserData = TerminalNoteData;
  6433. }
  6434. break;
  6435. case qeEmpty:
  6436. OperationComplete(ManagedTerminal->QueueOperationStart);
  6437. break;
  6438. case qePendingUserAction:
  6439. if ((ATerminal != Terminal) ||
  6440. (QueueInvisible && !IsQueueAutoPopup()))
  6441. {
  6442. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6443. }
  6444. break;
  6445. default:
  6446. DebugFail();
  6447. }
  6448. if (!Message.IsEmpty())
  6449. {
  6450. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6451. }
  6452. }
  6453. //---------------------------------------------------------------------------
  6454. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6455. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6456. {
  6457. if (FDDExtMapFile != NULL)
  6458. {
  6459. CloseHandle(FDDExtMapFile);
  6460. FDDExtMapFile = NULL;
  6461. }
  6462. if (WinConfiguration->DDFakeFile)
  6463. {
  6464. DDFakeFileInitDrag(FileList, Created);
  6465. }
  6466. }
  6467. //---------------------------------------------------------------------------
  6468. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  6469. {
  6470. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  6471. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  6472. {
  6473. FFakeFileDropTarget = FileName;
  6474. }
  6475. }
  6476. //---------------------------------------------------------------------------
  6477. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  6478. {
  6479. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6480. if (!ForceDirectories(ApiPath(Result)))
  6481. {
  6482. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  6483. }
  6484. FileSetAttr(ApiPath(Result), faHidden);
  6485. return Result;
  6486. }
  6487. //---------------------------------------------------------------------------
  6488. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  6489. bool & Created)
  6490. {
  6491. FFakeFileDropTarget = UnicodeString();
  6492. FDragFakeDirectory = CreateFakeTransferDirectory();
  6493. FileList->AddItem(NULL, FDragFakeDirectory);
  6494. Created = true;
  6495. if (!WinConfiguration->IsDDExtRunning() || (GetWindowsBuild() >= 17134))
  6496. {
  6497. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  6498. }
  6499. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6500. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6501. {
  6502. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6503. TDragExtCommStruct* CommStruct;
  6504. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6505. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6506. DebugAssert(CommStruct != NULL);
  6507. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  6508. CommStruct->Dragging = true;
  6509. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  6510. LENOF(CommStruct->DropDest));
  6511. NULL_TERMINATE(CommStruct->DropDest);
  6512. UnmapViewOfFile(CommStruct);
  6513. }
  6514. FDDMoveSlipped = false;
  6515. }
  6516. //---------------------------------------------------------------------------
  6517. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  6518. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  6519. {
  6520. bool Result;
  6521. if (Sender == RemoteDirView)
  6522. {
  6523. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  6524. }
  6525. else
  6526. {
  6527. DebugAssert(Sender == RemoteDriveView);
  6528. TStrings * FileList = RemoteDriveView->DragFileList();
  6529. try
  6530. {
  6531. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  6532. }
  6533. __finally
  6534. {
  6535. delete FileList;
  6536. }
  6537. }
  6538. if (FDDTargetControl == RemoteDriveView)
  6539. {
  6540. RemoteDriveView->UpdateDropTarget();
  6541. }
  6542. // foRemoteMove happens when file/dir is dragged within the remote tree view
  6543. // or from tree view to dir view.
  6544. // foMove happens when file/dir is dragged from remote tree view outside of
  6545. // application via dragex
  6546. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  6547. (DropSourceControl == RemoteDriveView))
  6548. {
  6549. RemoteDriveView->UpdateDropSource();
  6550. }
  6551. return Result;
  6552. }
  6553. //---------------------------------------------------------------------------
  6554. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  6555. {
  6556. // This also handles drops of remote files to queue.
  6557. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  6558. SAFE_DESTROY(FDDFileList);
  6559. if (!FFakeFileDropTarget.IsEmpty())
  6560. {
  6561. RemoveDir(ApiPath(FFakeFileDropTarget));
  6562. }
  6563. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  6564. {
  6565. try
  6566. {
  6567. TDragResult DDResult = (Sender == RemoteDirView) ?
  6568. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  6569. // Focus is moved to the target application,
  6570. // but as we are going to present the UI, we need to steal the focus back.
  6571. // This most likely won't work though (windows does not allow application
  6572. // to steal focus most of the time)
  6573. Application->BringToFront();
  6574. // On older version of Windows we never got drMove here, see also comment below.
  6575. // On Windows 10, we get the "move".
  6576. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  6577. {
  6578. UnicodeString TargetDirectory;
  6579. TFileOperation Operation;
  6580. // drInvalid may mean drMove, see comment below
  6581. switch (DDResult)
  6582. {
  6583. case drCopy:
  6584. Operation = foCopy;
  6585. break;
  6586. case drMove:
  6587. Operation = foMove;
  6588. break;
  6589. default:
  6590. DebugFail();
  6591. case drInvalid:
  6592. // prefer "copy" for safety
  6593. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  6594. break;
  6595. }
  6596. if (FDDMoveSlipped)
  6597. {
  6598. Operation = foMove;
  6599. }
  6600. TTransferOperationParam Param;
  6601. UnicodeString CounterName;
  6602. bool ForceQueue;
  6603. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  6604. {
  6605. // we get drInvalid both if d&d was intercepted by ddext,
  6606. // and when users drops on no-drop location.
  6607. // we tell the difference by existence of response from ddext,
  6608. // so we ignore absence of response in this case
  6609. if (DDResult != drInvalid)
  6610. {
  6611. // here we know that the extension is installed,
  6612. // as it is checked as soon as drag&drop starts from
  6613. // RemoteFileControlDDCreateDragFileList
  6614. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  6615. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  6616. }
  6617. }
  6618. else
  6619. {
  6620. // download using fake file
  6621. Param.Temp = false;
  6622. Param.DragDrop = true;
  6623. if (ForceQueue)
  6624. {
  6625. Param.Queue = asOn;
  6626. }
  6627. if (Sender == RemoteDirView)
  6628. {
  6629. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  6630. }
  6631. if (RemoteFileControlFileOperation(Sender, Operation,
  6632. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  6633. {
  6634. Configuration->Usage->Inc(CounterName);
  6635. }
  6636. }
  6637. }
  6638. }
  6639. __finally
  6640. {
  6641. CloseHandle(FDDExtMapFile);
  6642. FDDExtMapFile = NULL;
  6643. delete FDragFakeMonitors;
  6644. FDragFakeMonitors = NULL;
  6645. RemoveDir(ApiPath(FDragFakeDirectory));
  6646. FDragFakeDirectory = L"";
  6647. FFakeFileDropTarget = UnicodeString();
  6648. }
  6649. }
  6650. if (FDragDropSshTerminate.get() != NULL)
  6651. {
  6652. FDragDropSshTerminate->Rethrow();
  6653. }
  6654. }
  6655. //---------------------------------------------------------------------------
  6656. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  6657. TObject * Sender, int dwEffect, HRESULT & /*Result*/)
  6658. {
  6659. HCURSOR SlippedCopyCursor;
  6660. FDDMoveSlipped =
  6661. (FDragMoveCursor != NULL) &&
  6662. (!WinConfiguration->DDAllowMoveInit) && (dwEffect == DROPEFFECT_Copy) &&
  6663. ((IsFileControl(FDDTargetControl, osRemote) && (GetKeyState(VK_CONTROL) >= 0) &&
  6664. FTerminal->IsCapable[fcRemoteMove]) ||
  6665. (IsFileControl(FDDTargetControl, osLocal) && (GetKeyState(VK_SHIFT) < 0)));
  6666. SlippedCopyCursor = FDDMoveSlipped ? FDragMoveCursor : Dragdrop::DefaultCursor;
  6667. DragDropFiles(Sender)->CHCopy = SlippedCopyCursor;
  6668. DragDropFiles(Sender)->CHScrollCopy = SlippedCopyCursor;
  6669. // Remember drop effect so we know (when user drops files), if we copy or move
  6670. FLastDropEffect = dwEffect;
  6671. }
  6672. //---------------------------------------------------------------------------
  6673. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  6674. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  6675. {
  6676. bool Result;
  6677. if (FDDTargetControl == QueueView3)
  6678. {
  6679. Directory = DefaultDownloadTargetDirectory();
  6680. Result = true;
  6681. CounterName = L"DownloadsDragDropQueue";
  6682. ForceQueue = true;
  6683. }
  6684. else if (!FFakeFileDropTarget.IsEmpty())
  6685. {
  6686. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  6687. Result = true;
  6688. ForceQueue = false;
  6689. CounterName = L"DownloadsDragDropFakeFile";
  6690. }
  6691. else
  6692. {
  6693. ForceQueue = false;
  6694. Enabled = false;
  6695. try
  6696. {
  6697. int Timer = 0;
  6698. Result = false;
  6699. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  6700. {
  6701. {
  6702. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6703. TDragExtCommStruct* CommStruct;
  6704. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6705. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6706. DebugAssert(CommStruct != NULL);
  6707. Result = !CommStruct->Dragging;
  6708. if (Result)
  6709. {
  6710. Directory = ExtractFilePath(CommStruct->DropDest);
  6711. CounterName = L"DownloadsDragDropExternalExt";
  6712. }
  6713. UnmapViewOfFile(CommStruct);
  6714. }
  6715. if (!Result)
  6716. {
  6717. Sleep(50);
  6718. Timer += 50;
  6719. Application->ProcessMessages();
  6720. }
  6721. }
  6722. }
  6723. __finally
  6724. {
  6725. Enabled = true;
  6726. }
  6727. }
  6728. return Result;
  6729. }
  6730. //---------------------------------------------------------------------------
  6731. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  6732. const UnicodeString TempDir, int SecDelay)
  6733. {
  6734. TDateTime Alarm = Now() + (double)((double)SecDelay*OneMillisecond);
  6735. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  6736. if (FDelayedDeletionTimer == NULL)
  6737. {
  6738. DebugAssert(HandleAllocated());
  6739. FDelayedDeletionTimer = new TTimer(this);
  6740. FDelayedDeletionTimer->Interval = 10000;
  6741. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  6742. }
  6743. else
  6744. {
  6745. FDelayedDeletionTimer->Enabled = true;
  6746. }
  6747. }
  6748. //---------------------------------------------------------------------------
  6749. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  6750. {
  6751. DebugAssert(FDelayedDeletionList != NULL);
  6752. TDateTime N = Now();
  6753. TDateTime Alert;
  6754. UnicodeString Directory;
  6755. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  6756. {
  6757. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  6758. if ((N >= Alert) || (Sender == NULL))
  6759. {
  6760. Directory = FDelayedDeletionList->Strings[Index];
  6761. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  6762. {
  6763. FDelayedDeletionList->Delete(Index);
  6764. }
  6765. }
  6766. }
  6767. if (FDelayedDeletionList->Count == 0)
  6768. {
  6769. FDelayedDeletionTimer->Enabled = false;
  6770. }
  6771. }
  6772. //---------------------------------------------------------------------------
  6773. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  6774. {
  6775. if (IsFileControl(FDDTargetControl, osRemote) ||
  6776. (FDDTargetControl == SessionsPageControl))
  6777. {
  6778. TTerminal * TargetTerminal = NULL;
  6779. TFileOperation Operation = foNone;
  6780. if (FDDTargetControl == SessionsPageControl)
  6781. {
  6782. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  6783. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  6784. // do not allow dropping on the "+" tab
  6785. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  6786. if (TargetTerminal != NULL)
  6787. {
  6788. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  6789. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  6790. {
  6791. Operation = foRemoteMove;
  6792. }
  6793. else
  6794. {
  6795. Operation = foRemoteCopy;
  6796. }
  6797. }
  6798. }
  6799. else
  6800. {
  6801. // when move from remote side is disabled, we allow copying inside the remote
  6802. // panel, but we interpret is as moving (we also slip in the move cursor)
  6803. if ((FLastDropEffect == DROPEFFECT_MOVE) ||
  6804. (!WinConfiguration->DDAllowMoveInit && (FLastDropEffect == DROPEFFECT_COPY) &&
  6805. FDDMoveSlipped))
  6806. {
  6807. Operation = foRemoteMove;
  6808. }
  6809. else if (FLastDropEffect == DROPEFFECT_COPY)
  6810. {
  6811. Operation = foRemoteCopy;
  6812. }
  6813. }
  6814. if (Operation != foNone)
  6815. {
  6816. RemoteFileControlFileOperation(DropSourceControl,
  6817. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  6818. }
  6819. // abort drag&drop
  6820. Abort();
  6821. }
  6822. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  6823. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  6824. (FDDTargetControl != QueueView3))
  6825. {
  6826. DebugAssert(!FDragTempDir.IsEmpty());
  6827. TTransferType Type;
  6828. UnicodeString TempDir = FDragTempDir;
  6829. // We clear FDragTempDir before calling
  6830. // just in case it fail (raises exception)
  6831. FDragTempDir = L"";
  6832. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  6833. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  6834. // empty directory parameter means temp directory -> don't display it!
  6835. UnicodeString TargetDir = L"";
  6836. int Options = 0;
  6837. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  6838. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  6839. {
  6840. Abort();
  6841. }
  6842. // TargetDir is set when dropped on local file control
  6843. // (this was workaround for legacy dirview event handling, now it should be
  6844. // made prettier)
  6845. if (TargetDir.IsEmpty())
  6846. {
  6847. TargetDir = TempDir;
  6848. if (ForceDirectories(ApiPath(TargetDir)))
  6849. {
  6850. DebugAssert(Terminal && !TargetDir.IsEmpty());
  6851. FPendingTempSpaceWarn = true;
  6852. try
  6853. {
  6854. FDragDropOperation = true;
  6855. int Params = cpTemporary |
  6856. (Type == ttMove ? cpDelete : 0);
  6857. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  6858. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  6859. }
  6860. __finally
  6861. {
  6862. FDragDropOperation = false;
  6863. FPendingTempSpaceWarn = false;
  6864. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  6865. }
  6866. }
  6867. else
  6868. {
  6869. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  6870. }
  6871. }
  6872. }
  6873. }
  6874. //---------------------------------------------------------------------------
  6875. void __fastcall TCustomScpExplorerForm::DDDownload(
  6876. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  6877. {
  6878. void * BatchStorage;
  6879. BatchStart(BatchStorage);
  6880. try
  6881. {
  6882. UpdateCopyParamCounters(*CopyParam);
  6883. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  6884. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  6885. {
  6886. RemoteDriveView->UpdateDropSource();
  6887. }
  6888. }
  6889. __finally
  6890. {
  6891. BatchEnd(BatchStorage);
  6892. }
  6893. }
  6894. //---------------------------------------------------------------------------
  6895. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  6896. {
  6897. public:
  6898. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  6899. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename)
  6900. {
  6901. }
  6902. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  6903. {
  6904. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  6905. TDataObjectFilesEx::AllowData(FormatEtc);
  6906. }
  6907. };
  6908. //---------------------------------------------------------------------------
  6909. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  6910. TObject * Sender, TDataObject *& DataObject)
  6911. {
  6912. if (FDDExtMapFile != NULL)
  6913. {
  6914. TFileList * FileList = DragDropFiles(Sender)->FileList;
  6915. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  6916. {
  6917. Abort();
  6918. }
  6919. if (FileList->Count > 0)
  6920. {
  6921. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  6922. if (!FilesObject->IsValid(true, true))
  6923. {
  6924. FilesObject->_Release();
  6925. }
  6926. else
  6927. {
  6928. DataObject = FilesObject;
  6929. }
  6930. }
  6931. }
  6932. }
  6933. //---------------------------------------------------------------------------
  6934. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  6935. {
  6936. DebugFail();
  6937. }
  6938. //---------------------------------------------------------------------------
  6939. void __fastcall TCustomScpExplorerForm::GoToTree()
  6940. {
  6941. ComponentVisible[fcRemoteTree] = true;
  6942. RemoteDriveView->SetFocus();
  6943. }
  6944. //---------------------------------------------------------------------------
  6945. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  6946. TPanelExport Export)
  6947. {
  6948. TCustomDirView * DirView = this->DirView(Side);
  6949. std::unique_ptr<TStrings> ExportData(new TStringList());
  6950. switch (Export)
  6951. {
  6952. case pePath:
  6953. ExportData->Add(DirView->PathName);
  6954. break;
  6955. case peFileList:
  6956. case peFullFileList:
  6957. {
  6958. bool FullPath = (Export == peFullFileList);
  6959. DirView->CreateFileList(false, FullPath, ExportData.get());
  6960. for (int Index = 0; Index < ExportData->Count; Index++)
  6961. {
  6962. if (ExportData->Strings[Index].Pos(L" ") > 0)
  6963. {
  6964. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  6965. }
  6966. }
  6967. }
  6968. break;
  6969. default:
  6970. DebugFail();
  6971. }
  6972. return ExportData.release();
  6973. }
  6974. //---------------------------------------------------------------------------
  6975. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  6976. TPanelExport Export, TPanelExportDestination Destination)
  6977. {
  6978. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  6979. PanelExportStore(Side, Export, Destination, ExportData.get());
  6980. }
  6981. //---------------------------------------------------------------------------
  6982. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  6983. TPanelExport /*Export*/, TPanelExportDestination Destination,
  6984. TStrings * ExportData)
  6985. {
  6986. if (Destination == pedClipboard)
  6987. {
  6988. TInstantOperationVisualizer Visualizer;
  6989. CopyToClipboard(ExportData);
  6990. }
  6991. else
  6992. {
  6993. DebugFail();
  6994. }
  6995. }
  6996. //---------------------------------------------------------------------------
  6997. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  6998. {
  6999. TCustomDirView * DirView = this->DirView(Side);
  7000. TFileFilter Filter;
  7001. DefaultFileFilter(Filter);
  7002. Filter.Masks = DirView->Mask;
  7003. if (DoFilterMaskDialog(DirView, &Filter))
  7004. {
  7005. DirView->Mask = TFileMasks::NormalizeMask(Filter.Masks);
  7006. Configuration->Usage->Inc(L"Filters");
  7007. }
  7008. }
  7009. //---------------------------------------------------------------------------
  7010. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7011. {
  7012. return FQueueController->DefaultOperation();
  7013. }
  7014. //---------------------------------------------------------------------------
  7015. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7016. TQueueOperation Operation, void ** Param)
  7017. {
  7018. switch (Operation)
  7019. {
  7020. case qoPreferences:
  7021. return true;
  7022. case qoGoTo:
  7023. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7024. case qoOnceEmpty:
  7025. return !FQueueController->Empty;
  7026. default:
  7027. return FQueueController->AllowOperation(Operation, Param);
  7028. }
  7029. }
  7030. //---------------------------------------------------------------------------
  7031. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7032. {
  7033. if (DebugAlwaysTrue(QueueView3->Visible))
  7034. {
  7035. QueueView3->SetFocus();
  7036. }
  7037. }
  7038. //---------------------------------------------------------------------------
  7039. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7040. TQueueOperation Operation, void * Param)
  7041. {
  7042. if (Operation == qoGoTo)
  7043. {
  7044. GoToQueue();
  7045. }
  7046. else if (Operation == qoPreferences)
  7047. {
  7048. PreferencesDialog(pmQueue);
  7049. }
  7050. else
  7051. {
  7052. FQueueController->ExecuteOperation(Operation, Param);
  7053. }
  7054. }
  7055. //---------------------------------------------------------------------------
  7056. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7057. {
  7058. return (Queue != NULL) && Queue->Enabled;
  7059. }
  7060. //---------------------------------------------------------------------------
  7061. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7062. {
  7063. DebugAssert(Queue != NULL);
  7064. if (Queue != NULL)
  7065. {
  7066. Queue->Enabled = !Queue->Enabled;
  7067. }
  7068. }
  7069. //---------------------------------------------------------------------------
  7070. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7071. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7072. {
  7073. FQueueActedItem = QueueView3->ItemFocused;
  7074. }
  7075. //---------------------------------------------------------------------------
  7076. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7077. {
  7078. return TopDock->Height + QueueSplitter->Height;
  7079. }
  7080. //---------------------------------------------------------------------------
  7081. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7082. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7083. {
  7084. // when queue is hidden by double-clicking splitter, stray attempt to
  7085. // resize the panel with strange value arrives, make sure it is ignored
  7086. if (ComponentVisible[fcQueueView])
  7087. {
  7088. int HeightLimit = ClientHeight - GetStaticComponentsHeight() -
  7089. RemotePanel->Constraints->MinHeight;
  7090. if (NewSize > HeightLimit)
  7091. {
  7092. NewSize = HeightLimit;
  7093. }
  7094. }
  7095. else
  7096. {
  7097. Accept = false;
  7098. }
  7099. }
  7100. //---------------------------------------------------------------------------
  7101. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7102. TDragObject *& /*DragObject*/)
  7103. {
  7104. FQueueActedItem = QueueView3->ItemFocused;
  7105. }
  7106. //---------------------------------------------------------------------------
  7107. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7108. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7109. {
  7110. Accept = true;
  7111. if (Source == QueueView3)
  7112. {
  7113. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7114. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7115. if (Accept)
  7116. {
  7117. TQueueItemProxy * QueueItem;
  7118. TQueueItemProxy * DestQueueItem;
  7119. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7120. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7121. Accept = (QueueItem != DestQueueItem) &&
  7122. (QueueItem->Status == TQueueItem::qsPending) &&
  7123. (DestQueueItem->Status == TQueueItem::qsPending);
  7124. }
  7125. }
  7126. }
  7127. //---------------------------------------------------------------------------
  7128. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7129. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7130. {
  7131. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7132. {
  7133. TQueueItemProxy * QueueItem;
  7134. TQueueItemProxy * DestQueueItem;
  7135. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7136. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7137. QueueItem->Move(DestQueueItem);
  7138. }
  7139. }
  7140. //---------------------------------------------------------------------------
  7141. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7142. {
  7143. if ((QueueView3->ItemFocused == NULL) &&
  7144. (QueueView3->Items->Count > 0))
  7145. {
  7146. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7147. }
  7148. QueueLabelUpdateStatus();
  7149. }
  7150. //---------------------------------------------------------------------------
  7151. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7152. {
  7153. QueueLabelUpdateStatus();
  7154. }
  7155. //---------------------------------------------------------------------------
  7156. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7157. {
  7158. QueueLabel->UpdateStatus();
  7159. }
  7160. //---------------------------------------------------------------------------
  7161. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7162. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7163. {
  7164. if (Selected)
  7165. {
  7166. NonVisualDataModule->UpdateNonVisibleActions();
  7167. }
  7168. }
  7169. //---------------------------------------------------------------------------
  7170. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7171. {
  7172. TDragDropFilesEx * Result = NULL;
  7173. if (Sender == SessionsPageControl)
  7174. {
  7175. Result = FSessionsDragDropFilesEx;
  7176. }
  7177. else if (Sender == QueueView3)
  7178. {
  7179. Result = FQueueDragDropFilesEx;
  7180. }
  7181. else
  7182. {
  7183. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7184. if (DirView != NULL)
  7185. {
  7186. Result = DirView->DragDropFilesEx;
  7187. }
  7188. else
  7189. {
  7190. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7191. if (DriveView != NULL)
  7192. {
  7193. Result = DriveView->DragDropFilesEx;
  7194. }
  7195. }
  7196. }
  7197. DebugAssert(Result != NULL);
  7198. return Result;
  7199. }
  7200. //---------------------------------------------------------------------------
  7201. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7202. {
  7203. return (DView->SelCount == DView->FilesCount);
  7204. }
  7205. //---------------------------------------------------------------------------
  7206. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7207. {
  7208. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7209. }
  7210. //---------------------------------------------------------------------------
  7211. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7212. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue)
  7213. {
  7214. TFileOperation Operation;
  7215. switch (Effect)
  7216. {
  7217. case DROPEFFECT_MOVE:
  7218. Operation = foMove;
  7219. break;
  7220. case DROPEFFECT_COPY:
  7221. // occures on WinXP (reported by user)
  7222. default:
  7223. Operation = foCopy;
  7224. break;
  7225. };
  7226. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7227. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7228. if (DragDropFilesEx->FileList->Count > 0)
  7229. {
  7230. TStrings * FileList = new TStringList();
  7231. try
  7232. {
  7233. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7234. {
  7235. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7236. }
  7237. FDragDropOperation = true;
  7238. TTransferOperationParam Param;
  7239. Param.TargetDirectory = TargetPath;
  7240. // upload, no temp dirs
  7241. Param.Temp = false;
  7242. Param.DragDrop = true;
  7243. Param.Options =
  7244. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7245. if (ForceQueue)
  7246. {
  7247. Param.Queue = asOn;
  7248. }
  7249. if (ExecuteFileOperation(Operation, osLocal, FileList,
  7250. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7251. {
  7252. if (IsFileControl(DropSourceControl, osLocal))
  7253. {
  7254. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7255. }
  7256. else
  7257. {
  7258. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7259. }
  7260. }
  7261. }
  7262. __finally
  7263. {
  7264. FDragDropOperation = false;
  7265. delete FileList;
  7266. }
  7267. }
  7268. }
  7269. //---------------------------------------------------------------------------
  7270. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7271. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7272. UnicodeString TargetPath, bool & /*DoOperation*/)
  7273. {
  7274. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false);
  7275. }
  7276. //---------------------------------------------------------------------------
  7277. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7278. TObject * Sender, int grfKeyState, int & dwEffect)
  7279. {
  7280. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7281. if ((dwEffect != DROPEFFECT_None) &&
  7282. IsFileControl(DropSourceControl, osRemote))
  7283. {
  7284. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7285. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7286. (RemoteDirView->DropTarget == NULL))
  7287. {
  7288. dwEffect = DROPEFFECT_None;
  7289. }
  7290. else
  7291. {
  7292. if (dwEffect == DROPEFFECT_Copy)
  7293. {
  7294. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  7295. // currently we support copying always (at least via temporary directory);
  7296. // remove associated checks once this all proves stable and working
  7297. bool CopyCapable = true;
  7298. // if we do not support neither of operations, there's no discussion
  7299. if (!MoveCapable && !CopyCapable)
  7300. {
  7301. dwEffect = DROPEFFECT_None;
  7302. }
  7303. // when moving is disabled, we need to keep effect to "copy",
  7304. // which will be later interpretted as move (with slipped-in cursor)
  7305. else if (!WinConfiguration->DDAllowMoveInit && FLAGCLEAR(grfKeyState, MK_CONTROL))
  7306. {
  7307. // no-op, keep copy
  7308. }
  7309. else
  7310. {
  7311. // The default effect inside remote panel is move,
  7312. // unless we do not support it, but support copy
  7313. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7314. {
  7315. dwEffect = MoveCapable ? DROPEFFECT_Move : DROPEFFECT_Copy;
  7316. }
  7317. else
  7318. {
  7319. // with ctrl-down, we want copy unless it is not supported
  7320. dwEffect = CopyCapable ? DROPEFFECT_Copy : DROPEFFECT_None;
  7321. }
  7322. }
  7323. }
  7324. }
  7325. }
  7326. }
  7327. //---------------------------------------------------------------------------
  7328. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7329. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7330. {
  7331. if (FDDTotalSize >= 0)
  7332. {
  7333. if (File->IsDirectory)
  7334. {
  7335. FDDTotalSize = -1;
  7336. }
  7337. else
  7338. {
  7339. FDDTotalSize += File->Size;
  7340. }
  7341. }
  7342. DebugAssert(!FDragTempDir.IsEmpty());
  7343. // TODO: this is quite ineffective
  7344. // TODO: what if invalid character replacement is disabled?
  7345. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7346. UnicodeString TransferFileName;
  7347. if (Sender == RemoteDriveView)
  7348. {
  7349. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7350. }
  7351. else
  7352. {
  7353. TransferFileName = File->FileName;
  7354. }
  7355. FDDFileList->AddObject(TransferFileName, File);
  7356. }
  7357. //---------------------------------------------------------------------------
  7358. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7359. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7360. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7361. {
  7362. // sometimes we do not get DDEnd so the list is not released
  7363. SAFE_DESTROY(FDDFileList);
  7364. FDDFileList = new TStringList();
  7365. FDragTempDir = WinConfiguration->TemporaryDir();
  7366. FDDTotalSize = 0;
  7367. FDragDropSshTerminate.reset(NULL);
  7368. }
  7369. //---------------------------------------------------------------------------
  7370. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7371. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7372. HRESULT & Result)
  7373. {
  7374. if (Result == DRAGDROP_S_DROP)
  7375. {
  7376. try
  7377. {
  7378. GlobalDragImageList->HideDragImage();
  7379. try
  7380. {
  7381. RemoteFileControlDDTargetDrop();
  7382. }
  7383. catch(ESshTerminate & E)
  7384. {
  7385. FDragDropSshTerminate.reset(E.Clone());
  7386. }
  7387. }
  7388. catch (Exception &E)
  7389. {
  7390. // If downloading fails we need to cancel drag&drop, otherwise
  7391. // Explorer shows error
  7392. // But by the way exception probably never reach this point as
  7393. // it's catched on way.
  7394. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7395. Result = DRAGDROP_S_CANCEL;
  7396. ShowExtendedException(Terminal, &E);
  7397. }
  7398. }
  7399. }
  7400. //---------------------------------------------------------------------------
  7401. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7402. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7403. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7404. {
  7405. TFileMasks::TParams MaskParams;
  7406. MaskParams.Size = Size;
  7407. MaskParams.Modification = Modification;
  7408. // this does not re-parse the mask if it is the same as the last time
  7409. FDirViewMatchMask = Masks;
  7410. bool ImplicitMatch;
  7411. Matches =
  7412. // RecurseInclude parameter has no effect as the Path is empty
  7413. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7414. (AllowImplicitMatches || !ImplicitMatch);
  7415. }
  7416. //---------------------------------------------------------------------------
  7417. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7418. TObject * Sender, TListItem * Item, WORD & Indexes)
  7419. {
  7420. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7421. UnicodeString Ext;
  7422. if (DirView == RemoteDirView)
  7423. {
  7424. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7425. }
  7426. else
  7427. {
  7428. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7429. }
  7430. if (SameText(Ext, Configuration->PartialExt))
  7431. {
  7432. Indexes |= oiPartial;
  7433. }
  7434. }
  7435. //---------------------------------------------------------------------------
  7436. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7437. {
  7438. return
  7439. DirViewEnabled(osCurrent) &&
  7440. DirView(osCurrent)->CanPasteFromClipBoard();
  7441. }
  7442. //---------------------------------------------------------------------------
  7443. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7444. {
  7445. bool Result = false;
  7446. if (CanPasteToDirViewFromClipBoard())
  7447. {
  7448. Result = true;
  7449. }
  7450. else
  7451. {
  7452. UnicodeString ClipboardText;
  7453. if (NonEmptyTextFromClipboard(ClipboardText))
  7454. {
  7455. if (StoredSessions->IsUrl(ClipboardText))
  7456. {
  7457. Result = true;
  7458. }
  7459. else
  7460. {
  7461. Result = DirViewEnabled(osCurrent);
  7462. }
  7463. }
  7464. }
  7465. return Result;
  7466. }
  7467. //---------------------------------------------------------------------------
  7468. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7469. {
  7470. if (DoesClipboardContainOurFiles())
  7471. {
  7472. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  7473. {
  7474. TTerminalManager * Manager = TTerminalManager::Instance();
  7475. TTerminal * TergetTerminal = Manager->ActiveTerminal;
  7476. Manager->ActiveTerminal = FClipboardTerminal;
  7477. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TergetTerminal);
  7478. }
  7479. }
  7480. else if (CanPasteToDirViewFromClipBoard())
  7481. {
  7482. DirView(osCurrent)->PasteFromClipBoard();
  7483. }
  7484. else
  7485. {
  7486. UnicodeString ClipboardText;
  7487. if (NonEmptyTextFromClipboard(ClipboardText))
  7488. {
  7489. if (StoredSessions->IsUrl(ClipboardText))
  7490. {
  7491. NewSession(false, ClipboardText);
  7492. }
  7493. else
  7494. {
  7495. DirView(osCurrent)->Path = ClipboardText;
  7496. }
  7497. }
  7498. }
  7499. }
  7500. //---------------------------------------------------------------------------
  7501. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  7502. {
  7503. // TBD
  7504. }
  7505. //---------------------------------------------------------------------------
  7506. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  7507. {
  7508. TCustomDirView * ADirView = DirView(Side);
  7509. ADirView->SelectAll(Mode);
  7510. ADirView->SetFocus();
  7511. }
  7512. //---------------------------------------------------------------------------
  7513. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  7514. {
  7515. TCustomDirView * ADirView = DirView(Side);
  7516. TFileFilter Filter;
  7517. DefaultFileFilter(Filter);
  7518. if (DoSelectMaskDialog(ADirView, Select, &Filter, Configuration))
  7519. {
  7520. Configuration->Usage->Inc(L"MaskSelections");
  7521. ADirView->SelectFiles(Filter, Select);
  7522. ADirView->SetFocus();
  7523. }
  7524. }
  7525. //---------------------------------------------------------------------------
  7526. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  7527. {
  7528. TCustomDirView * ADirView = DirView(Side);
  7529. ADirView->RestoreSelectedNames();
  7530. ADirView->SetFocus();
  7531. }
  7532. //---------------------------------------------------------------------------
  7533. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  7534. {
  7535. TCustomDirView * CurrentDirView = DirView(osCurrent);
  7536. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  7537. {
  7538. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  7539. UnicodeString Ext;
  7540. if (GetSide(osCurrent) == osRemote)
  7541. {
  7542. Ext = UnixExtractFileExt(FileName);
  7543. }
  7544. else
  7545. {
  7546. Ext = ExtractFileExt(FileName);
  7547. }
  7548. if (Ext.IsEmpty())
  7549. {
  7550. Ext = L".";
  7551. }
  7552. TFileFilter Filter;
  7553. Filter.Masks = FORMAT(L"*%s", (Ext));
  7554. Filter.Directories = false;
  7555. CurrentDirView->SelectFiles(Filter, Select);
  7556. }
  7557. }
  7558. //---------------------------------------------------------------------------
  7559. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  7560. const TStatusFileInfo & FileInfo, TOperationSide Side)
  7561. {
  7562. UnicodeString Result;
  7563. if ((Side == osRemote) && (Terminal == NULL))
  7564. {
  7565. // noop
  7566. }
  7567. else
  7568. {
  7569. Result =
  7570. FMTLOAD(FILE_INFO_FORMAT,
  7571. (FormatBytes(FileInfo.SelectedSize),
  7572. FormatBytes(FileInfo.FilesSize),
  7573. FormatNumber(FileInfo.SelectedCount),
  7574. FormatNumber(FileInfo.FilesCount)));
  7575. }
  7576. return Result;
  7577. }
  7578. //---------------------------------------------------------------------------
  7579. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  7580. TTBXStatusPanel * Panel, TOperationSide Side)
  7581. {
  7582. if (Panel->Index == 1)
  7583. {
  7584. ToggleShowHiddenFiles();
  7585. }
  7586. else if (Panel->Index == 2)
  7587. {
  7588. Filter(Side);
  7589. }
  7590. }
  7591. //---------------------------------------------------------------------------
  7592. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  7593. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  7594. {
  7595. DebugAssert(!StatusBar->SimplePanel);
  7596. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  7597. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  7598. }
  7599. //---------------------------------------------------------------------------
  7600. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  7601. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  7602. {
  7603. DebugAssert(StatusBar->Panels->Count >= 3);
  7604. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  7605. if (FileInfo.HiddenCount > 0)
  7606. {
  7607. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  7608. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7609. }
  7610. else
  7611. {
  7612. HiddenFilesPanel->ViewPriority = 0;
  7613. // not really necessary, just to cleanup no-longer-valid data
  7614. HiddenFilesPanel->Caption = L"";
  7615. }
  7616. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  7617. if (FileInfo.FilteredCount > 0)
  7618. {
  7619. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  7620. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7621. }
  7622. else
  7623. {
  7624. FilteredFilesPanel->ViewPriority = 0;
  7625. // not really necessary, just to cleanup no-longer-valid data
  7626. FilteredFilesPanel->Caption = L"";
  7627. }
  7628. }
  7629. //---------------------------------------------------------------------------
  7630. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  7631. TObject * /*Sender*/)
  7632. {
  7633. if (RemoteDirView->Enabled)
  7634. {
  7635. RemoteDirView->SetFocus();
  7636. }
  7637. }
  7638. //---------------------------------------------------------------------------
  7639. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  7640. {
  7641. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  7642. switch (Config.Show)
  7643. {
  7644. case qvShow:
  7645. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  7646. {
  7647. Config.Show = qvHide;
  7648. }
  7649. else
  7650. {
  7651. Config.Show = Config.LastHideShow;
  7652. }
  7653. break;
  7654. case qvHideWhenEmpty:
  7655. if (ComponentVisible[fcQueueView])
  7656. {
  7657. Config.Show = qvHide;
  7658. }
  7659. else
  7660. {
  7661. Config.LastHideShow = Config.Show;
  7662. Config.Show = qvShow;
  7663. }
  7664. break;
  7665. case qvHide:
  7666. Config.LastHideShow = Config.Show;
  7667. Config.Show = qvShow;
  7668. break;
  7669. }
  7670. WinConfiguration->QueueView = Config;
  7671. }
  7672. //---------------------------------------------------------------------------
  7673. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  7674. {
  7675. UnicodeString Result;
  7676. if (FTerminal != NULL)
  7677. {
  7678. switch (WinConfiguration->PathInCaption)
  7679. {
  7680. case picShort:
  7681. Result = TTerminalManager::Instance()->GetTerminalShortPath(FTerminal);
  7682. break;
  7683. case picFull:
  7684. Result = FTerminal->CurrentDirectory;
  7685. break;
  7686. }
  7687. }
  7688. return Result;
  7689. }
  7690. //---------------------------------------------------------------------------
  7691. void __fastcall TCustomScpExplorerForm::UpdateControls()
  7692. {
  7693. TTerminalManager::Instance()->UpdateAppTitle();
  7694. // WORAKRDOUND: Disabling list view when it is not showing yet does not set its
  7695. // background to gray on Windows 7 (works on Windows 10).
  7696. // See also EnableControl
  7697. if (Showing)
  7698. {
  7699. bool HasTerminal = (Terminal != NULL);
  7700. if (HasTerminal)
  7701. {
  7702. // Update path when it changes
  7703. if ((SessionsPageControl->ActivePage != NULL) && (GetSessionTabTerminal(SessionsPageControl->ActivePage) == Terminal))
  7704. {
  7705. UpdateSessionTab(SessionsPageControl->ActivePage);
  7706. }
  7707. if (!RemoteDirView->Enabled)
  7708. {
  7709. RemoteDirView->Enabled = true;
  7710. if (FRemoteDirViewWasFocused)
  7711. {
  7712. ActiveControl = RemoteDirView;
  7713. }
  7714. }
  7715. RemoteDriveView->Enabled = true;
  7716. RemoteDirView->Color = (FSessionColor != 0 ? FSessionColor : clWindow);
  7717. RemoteDriveView->Color = RemoteDirView->Color;
  7718. }
  7719. else
  7720. {
  7721. if (RemoteDirView->Enabled)
  7722. {
  7723. // This is first called when the form is being constructed
  7724. // (not anymore due to Showing test above)
  7725. // but the false is overriden in the constructor later.
  7726. // An even later in TScpCommanderForm::DoShow()
  7727. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  7728. EnableControl(RemoteDirView, false);
  7729. }
  7730. EnableControl(RemoteDriveView, false);
  7731. }
  7732. EnableControl(QueueView3, HasTerminal && Terminal->IsCapable[fsBackgroundTransfers]);
  7733. QueueLabelUpdateStatus();
  7734. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  7735. }
  7736. }
  7737. //---------------------------------------------------------------------------
  7738. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  7739. {
  7740. UpdateControls();
  7741. }
  7742. //---------------------------------------------------------------------------
  7743. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  7744. TObject * Sender)
  7745. {
  7746. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7747. DebugAssert(DirView != NULL);
  7748. DoDirViewLoaded(DirView);
  7749. TransferPresetAutoSelect();
  7750. }
  7751. //---------------------------------------------------------------------------
  7752. void __fastcall TCustomScpExplorerForm::StartUpdates()
  7753. {
  7754. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  7755. // first run after installation
  7756. if (double(Updates.LastCheck) == 0)
  7757. {
  7758. // make sure next time there will be an update (if enabled)
  7759. Updates.LastCheck = TDateTime(1);
  7760. WinConfiguration->Updates = Updates;
  7761. }
  7762. else if ((double(Updates.Period) > 0) &&
  7763. (Now() - Updates.LastCheck >= Updates.Period))
  7764. {
  7765. TThreadMethod OnUpdatesChecked = NULL;
  7766. if (!IsUWP())
  7767. {
  7768. OnUpdatesChecked = UpdatesChecked;
  7769. }
  7770. StartUpdateThread(OnUpdatesChecked);
  7771. }
  7772. }
  7773. //---------------------------------------------------------------------------
  7774. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  7775. {
  7776. UnicodeString Message;
  7777. bool New;
  7778. TQueryType Type;
  7779. GetUpdatesMessage(Message, New, Type, false);
  7780. if (!Message.IsEmpty())
  7781. {
  7782. if (New)
  7783. {
  7784. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  7785. }
  7786. if (!New && (Type != qtWarning))
  7787. {
  7788. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  7789. }
  7790. else
  7791. {
  7792. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  7793. }
  7794. if (New)
  7795. {
  7796. Configuration->Usage->Inc(L"UpdateNotifications");
  7797. }
  7798. }
  7799. }
  7800. //---------------------------------------------------------------------------
  7801. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  7802. {
  7803. RestoreApp();
  7804. bool CanDisplay = !NonVisualDataModule->Busy;
  7805. if (!CanDisplay && (Screen->ActiveForm != NULL))
  7806. {
  7807. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  7808. }
  7809. if (CanDisplay)
  7810. {
  7811. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  7812. CheckForUpdates(true);
  7813. }
  7814. else
  7815. {
  7816. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  7817. }
  7818. }
  7819. //---------------------------------------------------------------------------
  7820. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  7821. TCopyParamRuleData & Data)
  7822. {
  7823. DebugAssert(Terminal != NULL);
  7824. Data.HostName = Terminal->SessionData->HostNameExpanded;
  7825. Data.UserName = Terminal->SessionData->UserNameExpanded;
  7826. Data.RemoteDirectory = RemoteDirView->PathName;
  7827. }
  7828. //---------------------------------------------------------------------------
  7829. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  7830. {
  7831. // Terminal can be null when we are changing local directory implicitly,
  7832. // because it has been deleted, while no session is connected
  7833. // (Login dialog is open)
  7834. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  7835. {
  7836. TCopyParamRuleData Data;
  7837. GetTransferPresetAutoSelectData(Data);
  7838. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  7839. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  7840. if (CopyParamIndex < 0)
  7841. {
  7842. // there is no preset that matches autoselection
  7843. // set preset that we consider "default"
  7844. FCopyParamAutoSelected = L""; // forget last autoselected preset
  7845. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  7846. }
  7847. else
  7848. {
  7849. // there is preset matching autoselection
  7850. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  7851. if (CopyParamName == FCopyParamAutoSelected)
  7852. {
  7853. // autoselected the same preset as the last time
  7854. // make no change (i.e. preserve custom user preset, if any)
  7855. }
  7856. else
  7857. {
  7858. // autoselected the different preset then the last time (or there
  7859. // was default preset set)
  7860. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  7861. GUIConfiguration->CopyParamCurrent = CopyParamName;
  7862. }
  7863. }
  7864. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  7865. {
  7866. if (CopyParamIndex >= 0)
  7867. {
  7868. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  7869. }
  7870. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  7871. try
  7872. {
  7873. int Fmt =
  7874. (CopyParamIndex < 0) ?
  7875. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  7876. COPY_PARAM_AUTOSELECTED;
  7877. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  7878. Data->Message = MainInstructions(Message);
  7879. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  7880. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  7881. CopyParamAttrs);
  7882. if (CopyParamIndex >= 0)
  7883. {
  7884. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  7885. Info = FORMAT(L"%s\n \n%s", (Info,
  7886. FMTLOAD(COPY_PARAM_RULE,
  7887. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  7888. }
  7889. Data->Message += L"\n\n" + Info;
  7890. if (WinConfiguration->CopyParamAutoSelectNotice)
  7891. {
  7892. TransferPresetNoteMessage(Data, true);
  7893. }
  7894. else
  7895. {
  7896. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  7897. Data = NULL; // ownership passed
  7898. }
  7899. }
  7900. __finally
  7901. {
  7902. delete Data;
  7903. }
  7904. }
  7905. }
  7906. }
  7907. //---------------------------------------------------------------------------
  7908. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  7909. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  7910. {
  7911. DebugAssert(NoteData != NULL);
  7912. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  7913. TQueryButtonAlias Aliases[1];
  7914. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  7915. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  7916. Params.Aliases = Aliases;
  7917. Params.AliasesCount = LENOF(Aliases);
  7918. unsigned int Result =
  7919. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  7920. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  7921. switch (Result)
  7922. {
  7923. case qaNeverAskAgain:
  7924. DebugAssert(AllowNeverAskAgain);
  7925. WinConfiguration->CopyParamAutoSelectNotice = false;
  7926. break;
  7927. case qaIgnore:
  7928. PreferencesDialog(pmPresets);
  7929. break;
  7930. }
  7931. }
  7932. //---------------------------------------------------------------------------
  7933. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  7934. {
  7935. // as of now useless, as this is used for notes only, never for balloons, ...
  7936. RestoreApp();
  7937. // .. and we should never be busy here
  7938. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  7939. {
  7940. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  7941. }
  7942. }
  7943. //---------------------------------------------------------------------------
  7944. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  7945. TPreferencesMode APreferencesMode)
  7946. {
  7947. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  7948. TCopyParamRuleData Data;
  7949. if (Terminal != NULL)
  7950. {
  7951. PreferencesData.reset(new TPreferencesDialogData());
  7952. GetTransferPresetAutoSelectData(Data);
  7953. PreferencesData->CopyParamRuleData = &Data;
  7954. }
  7955. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  7956. }
  7957. //---------------------------------------------------------------------------
  7958. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  7959. const TCustomCommandType & Command)
  7960. {
  7961. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  7962. {
  7963. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (Command.Command)));
  7964. }
  7965. }
  7966. //---------------------------------------------------------------------------
  7967. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  7968. {
  7969. bool RemoteAllowed = CustomCommandRemoteAllowed();
  7970. TCustomCommandType Command;
  7971. // make sure we use local custom command when remote are not supported
  7972. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  7973. {
  7974. Command = FLastCustomCommand;
  7975. }
  7976. else
  7977. {
  7978. Command.Params = Command.Params | ccLocal;
  7979. }
  7980. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  7981. FEditingFocusedAdHocCommand = OnFocused;
  7982. bool LocalSide = (FCurrentSide == osLocal);
  7983. int Options =
  7984. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  7985. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  7986. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  7987. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  7988. {
  7989. FLastCustomCommand = Command;
  7990. UpdateCustomCommandsToolbar();
  7991. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  7992. }
  7993. }
  7994. //---------------------------------------------------------------------------
  7995. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  7996. {
  7997. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  7998. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  7999. DebugAssert(State > 0);
  8000. if (State <= 0)
  8001. {
  8002. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (FLastCustomCommand.Command)));
  8003. }
  8004. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8005. }
  8006. //---------------------------------------------------------------------------
  8007. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8008. TCustomCommandType & Command, int & State)
  8009. {
  8010. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8011. if (Result)
  8012. {
  8013. Command = FLastCustomCommand;
  8014. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8015. }
  8016. return Result;
  8017. }
  8018. //---------------------------------------------------------------------------
  8019. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8020. {
  8021. if (RemoteDirView->ItemFocused != NULL)
  8022. {
  8023. RemoteDirView->ItemFocused->CancelEdit();
  8024. }
  8025. }
  8026. //---------------------------------------------------------------------------
  8027. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8028. {
  8029. DebugAssert(ComponentVisible[Component]);
  8030. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8031. }
  8032. //---------------------------------------------------------------------------
  8033. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8034. {
  8035. // when queue panel is resized here directly, the status bar is stretched
  8036. // over whole space the panel occupied
  8037. PostComponentHide(fcQueueView);
  8038. }
  8039. //---------------------------------------------------------------------------
  8040. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8041. {
  8042. TMessage * M = static_cast<TMessage*>(Message);
  8043. switch (M->Msg)
  8044. {
  8045. case CM_DIALOGCHAR:
  8046. CMDialogChar(*M);
  8047. break;
  8048. case WM_APPCOMMAND:
  8049. WMAppCommand(*M);
  8050. break;
  8051. case WM_SYSCOMMAND:
  8052. WMSysCommand(*M);
  8053. break;
  8054. case WM_QUERYENDSESSION:
  8055. WMQueryEndSession(*M);
  8056. break;
  8057. case WM_ENDSESSION:
  8058. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8059. break;
  8060. #ifdef _DEBUG
  8061. case WM_WINDOWPOSCHANGED:
  8062. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8063. break;
  8064. case CM_CUSTOMSTYLECHANGED:
  8065. TForm::Dispatch(Message);
  8066. break;
  8067. #endif
  8068. case WM_COMPONENT_HIDE:
  8069. {
  8070. Byte Component = static_cast<Byte>(M->WParam);
  8071. // sanity check
  8072. if (ComponentVisible[Component])
  8073. {
  8074. // special treatment
  8075. if (Component == fcQueueView)
  8076. {
  8077. ToggleQueueVisibility();
  8078. DebugAssert(!ComponentVisible[fcQueueView]);
  8079. }
  8080. else
  8081. {
  8082. ComponentVisible[Component] = false;
  8083. }
  8084. }
  8085. }
  8086. break;
  8087. case WM_COPYDATA:
  8088. WMCopyData(*M);
  8089. break;
  8090. case WM_MANAGES_CAPTION:
  8091. // caption managed in TTerminalManager::UpdateAppTitle()
  8092. M->Result = 1;
  8093. break;
  8094. case WM_WANTS_MOUSEWHEEL:
  8095. M->Result = 1;
  8096. break;
  8097. case CM_SHOWINGCHANGED:
  8098. CMShowingChanged(*M);
  8099. break;
  8100. case WM_CLOSE:
  8101. WMClose(*M);
  8102. break;
  8103. case WM_DPICHANGED:
  8104. WMDpiChanged(*M);
  8105. break;
  8106. case CM_DPICHANGED:
  8107. CMDpiChanged(*M);
  8108. break;
  8109. default:
  8110. TForm::Dispatch(Message);
  8111. break;
  8112. }
  8113. }
  8114. //---------------------------------------------------------------------------
  8115. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8116. {
  8117. // noop
  8118. }
  8119. //---------------------------------------------------------------------------
  8120. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8121. {
  8122. TForm::Dispatch(&Message);
  8123. FSessionColors->Clear();
  8124. AddFixedSessionImages();
  8125. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8126. UpdateImages();
  8127. }
  8128. //---------------------------------------------------------------------------
  8129. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8130. {
  8131. TForm::Dispatch(&Message);
  8132. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8133. UpdatePixelsPerInchMainWindowCounter();
  8134. }
  8135. //---------------------------------------------------------------------------
  8136. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8137. {
  8138. // Cannot close window while we are busy.
  8139. // We cannot test this in FormCloseQuery as that is called also from
  8140. // Close(), which is called by CloseApplicationAction. So we can be busy
  8141. // there already even, when it is legitimate to close the application.
  8142. // Possibly a better place to handle this would be WMSysCommand.
  8143. if (NonVisualDataModule->Busy)
  8144. {
  8145. Message.Result = 1;
  8146. }
  8147. else
  8148. {
  8149. TForm::Dispatch(&Message);
  8150. }
  8151. }
  8152. //---------------------------------------------------------------------------
  8153. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8154. {
  8155. TForm::Dispatch(&Message);
  8156. if (Showing && (Terminal == NULL))
  8157. {
  8158. // When we are starting minimized (i.e. from an installer),
  8159. // postpone showing Login dialog until we get restored.
  8160. // Otherwise the Login dialog (and Authentication window) show restored
  8161. // over invidible (minimized) main window.
  8162. if (WindowState == wsMinimized)
  8163. {
  8164. FNeedSession = true;
  8165. }
  8166. else
  8167. {
  8168. // This happens before application ever goes idle, so the toolbars would
  8169. // stay enabled (initial state) until the Login dialog is dismissed.
  8170. UpdateActions();
  8171. NonVisualDataModule->StartBusy();
  8172. try
  8173. {
  8174. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8175. // otherwise the dialog does not receive focus.
  8176. // With Commander interface the ProcessMessages is called already
  8177. // by TDriveView, but with Explorer interface, we need to call it explicily
  8178. Application->ProcessMessages();
  8179. // do not reload sessions, they have been loaded just now (optimization)
  8180. NeedSession(false);
  8181. }
  8182. __finally
  8183. {
  8184. NonVisualDataModule->EndBusy();
  8185. }
  8186. }
  8187. }
  8188. }
  8189. //---------------------------------------------------------------------------
  8190. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8191. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8192. int & MaxHeight)
  8193. {
  8194. // workaround for bug in TWinControl.CalcConstraints
  8195. // Check for empty rect (restore from iconic state) is done there only after
  8196. // call to AdjustClientRect, which enlarges the rect (for forms).
  8197. TRect R = GetClientRect();
  8198. // when restoring from iconic state, place no restrictions
  8199. if (IsRectEmpty(R))
  8200. {
  8201. MinWidth = 0;
  8202. MinHeight = 0;
  8203. MaxWidth = 32000;
  8204. MaxHeight = 32000;
  8205. }
  8206. }
  8207. //---------------------------------------------------------------------------
  8208. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8209. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8210. {
  8211. // terminal can be already closed (e.g. dropped connection)
  8212. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8213. {
  8214. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8215. try
  8216. {
  8217. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8218. }
  8219. catch(...)
  8220. {
  8221. if (!Terminal->Active)
  8222. {
  8223. Close = true;
  8224. }
  8225. throw;
  8226. }
  8227. }
  8228. }
  8229. //---------------------------------------------------------------------------
  8230. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8231. {
  8232. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8233. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8234. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8235. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8236. {
  8237. OnGetSpaceAvailable = GetSpaceAvailable;
  8238. }
  8239. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8240. OnGetSpaceAvailable);
  8241. }
  8242. //---------------------------------------------------------------------------
  8243. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8244. {
  8245. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8246. Terminal->FillSessionDataForCode(Data.get());
  8247. return Data.release();
  8248. }
  8249. //---------------------------------------------------------------------------
  8250. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8251. {
  8252. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8253. DoGenerateUrlDialog(Data.get(), Paths);
  8254. }
  8255. //---------------------------------------------------------------------------
  8256. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8257. {
  8258. GenerateUrl(NULL);
  8259. }
  8260. //---------------------------------------------------------------------------
  8261. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8262. {
  8263. std::unique_ptr<TStrings> Paths(new TStringList());
  8264. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8265. GenerateUrl(Paths.get());
  8266. }
  8267. //---------------------------------------------------------------------------
  8268. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8269. {
  8270. if (value != FSessionColor)
  8271. {
  8272. FSessionColor = value;
  8273. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8274. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8275. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8276. DebugAssert(ColorItem != NULL);
  8277. ColorItem->Color = C;
  8278. NonVisualDataModule->ColorMenuItem->Color = C;
  8279. // Is null when called from LastTerminalClosed
  8280. if (Terminal != NULL)
  8281. {
  8282. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8283. }
  8284. UpdateControls();
  8285. }
  8286. }
  8287. //---------------------------------------------------------------------------
  8288. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8289. {
  8290. bool Result = FNoteTimer->Enabled;
  8291. if (Result)
  8292. {
  8293. // cannot cancel note too early
  8294. bool NotEarly =
  8295. (Now() - FNoteShown >
  8296. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8297. if (Force || NotEarly)
  8298. {
  8299. FNoteTimer->Enabled = false;
  8300. FNote = L"";
  8301. SAFE_DESTROY(FNoteData);
  8302. FOnNoteClick = NULL;
  8303. FNoteHints = FNotes->Text;
  8304. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8305. UpdateStatusBar();
  8306. }
  8307. }
  8308. return Result;
  8309. }
  8310. //---------------------------------------------------------------------------
  8311. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8312. {
  8313. DebugAssert(FNoteTimer->Enabled);
  8314. CancelNote(true);
  8315. }
  8316. //---------------------------------------------------------------------------
  8317. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8318. {
  8319. int P = Note.Pos(L"\n");
  8320. if (P > 0)
  8321. {
  8322. Note.SetLength(P - 1);
  8323. }
  8324. FNotes->Add(FORMAT(L"[%s] %s",
  8325. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8326. while (FNotes->Count > 10)
  8327. {
  8328. FNotes->Delete(0);
  8329. }
  8330. if (UpdateNow)
  8331. {
  8332. FNoteHints = FNotes->Text;
  8333. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8334. UpdateStatusBar();
  8335. }
  8336. }
  8337. //---------------------------------------------------------------------------
  8338. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8339. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8340. {
  8341. int P = Note.Pos(L"\n");
  8342. if (P > 0)
  8343. {
  8344. Note.SetLength(P - 1);
  8345. }
  8346. FNoteHints = FNotes->Text;
  8347. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8348. FNote = Note;
  8349. SAFE_DESTROY(FNoteData);
  8350. FNoteData = NoteData;
  8351. FOnNoteClick = OnNoteClick;
  8352. AddNote(Note, false);
  8353. UpdateStatusBar();
  8354. FNoteShown = Now();
  8355. FNoteTimer->Enabled = false;
  8356. if (Seconds == 0)
  8357. {
  8358. Seconds = WinConfiguration->NotificationsTimeout;
  8359. }
  8360. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8361. FNoteTimer->Enabled = true;
  8362. }
  8363. //---------------------------------------------------------------------------
  8364. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8365. {
  8366. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8367. }
  8368. //---------------------------------------------------------------------------
  8369. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8370. {
  8371. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  8372. {
  8373. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8374. }
  8375. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  8376. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  8377. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  8378. 0, NULL, NULL);
  8379. }
  8380. //---------------------------------------------------------------------------
  8381. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  8382. {
  8383. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  8384. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  8385. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  8386. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  8387. }
  8388. //---------------------------------------------------------------------------
  8389. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  8390. {
  8391. WinConfiguration->FormatSizeBytes = Style;
  8392. }
  8393. //---------------------------------------------------------------------------
  8394. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  8395. {
  8396. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  8397. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  8398. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  8399. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  8400. }
  8401. //---------------------------------------------------------------------------
  8402. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  8403. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  8404. {
  8405. if (Panel->Index == 0)
  8406. {
  8407. if (FOnNoteClick != NULL)
  8408. {
  8409. // prevent the user data from being freed by possible call
  8410. // to CancelNote or PostNote during call to OnNoteClick
  8411. std::unique_ptr<TObject> NoteData(FNoteData);
  8412. TNotifyEvent OnNoteClick = FOnNoteClick;
  8413. FNoteData = NULL;
  8414. // need to cancel the note as we are going to delete its user data
  8415. CancelNote(true);
  8416. OnNoteClick(NoteData.get());
  8417. }
  8418. }
  8419. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  8420. {
  8421. FileSystemInfo();
  8422. }
  8423. }
  8424. //---------------------------------------------------------------------------
  8425. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  8426. {
  8427. // workaround:
  8428. // 1) for unknown reason, disabling window, while minimized,
  8429. // prevents it from restoring, even if it was enabled again meanwhile
  8430. // 2) when disabling the main window, while another has focus
  8431. // minimize is no longer possible
  8432. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  8433. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  8434. // Shouldn't we use IsApplicationMinimized() here?
  8435. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  8436. {
  8437. Enabled = false;
  8438. }
  8439. FLockLevel++;
  8440. }
  8441. //---------------------------------------------------------------------------
  8442. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  8443. {
  8444. DebugAssert(FLockLevel > 0);
  8445. FLockLevel--;
  8446. if (FLockLevel == 0)
  8447. {
  8448. DebugAssert(FLockSuspendLevel == 0);
  8449. Enabled = true;
  8450. }
  8451. }
  8452. //---------------------------------------------------------------------------
  8453. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  8454. {
  8455. // We need to make sure that window is enabled when the last modal window closes
  8456. // otherwise focus is not restored correctly.
  8457. // So we re-enable the window when modal window opens and
  8458. // disable it back after is closes
  8459. if (FLockLevel > 0)
  8460. {
  8461. // while we have nesting counter, we know that we never be called
  8462. // recursivelly as Application->OnModalBegin is called only
  8463. // for the top-level modal window
  8464. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8465. {
  8466. // won't be disabled when conditions in LockWindow() were not satisfied
  8467. FDisabledOnLockSuspend = !Enabled;
  8468. // When minimized to tray (or actually when set to SW_HIDE),
  8469. // setting Enabled makes the window focusable even when there's
  8470. // modal window over it
  8471. if (!FTrayIcon->Visible)
  8472. {
  8473. Enabled = true;
  8474. }
  8475. }
  8476. FLockSuspendLevel++;
  8477. }
  8478. }
  8479. //---------------------------------------------------------------------------
  8480. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  8481. {
  8482. if (FLockSuspendLevel > 0)
  8483. {
  8484. DebugAssert(FLockLevel > 0);
  8485. FLockSuspendLevel--;
  8486. // see comment in SuspendWindowLock
  8487. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8488. {
  8489. // Note that window can be enabled here, when we were minized to tray when
  8490. // was SuspendWindowLock() called.
  8491. // We should possibly do the same check as in LockWindow(),
  8492. // if it is ever possible that the consitions change between
  8493. // SuspendWindowLock() and ResumeWindowLock()
  8494. Enabled = !FDisabledOnLockSuspend;
  8495. }
  8496. }
  8497. }
  8498. //---------------------------------------------------------------------------
  8499. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  8500. {
  8501. if (!TextOnly)
  8502. {
  8503. TTBXComboBoxItem * RemotePathComboBox =
  8504. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  8505. TStrings * Items = RemotePathComboBox->Strings;
  8506. Items->BeginUpdate();
  8507. try
  8508. {
  8509. Items->Clear();
  8510. if (Terminal != NULL)
  8511. {
  8512. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8513. while (!IsUnixRootPath(APath))
  8514. {
  8515. int P = APath.LastDelimiter(L'/');
  8516. DebugAssert(P >= 0);
  8517. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  8518. APath.SetLength(P - 1);
  8519. }
  8520. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  8521. }
  8522. }
  8523. __finally
  8524. {
  8525. RemotePathComboBox->ItemIndex = Items->Count - 1;
  8526. // Setting ItemIndex to -1 does not reset its text
  8527. if (Items->Count == 0)
  8528. {
  8529. RemotePathComboBox->Text = L"";
  8530. }
  8531. Items->EndUpdate();
  8532. }
  8533. }
  8534. }
  8535. //---------------------------------------------------------------------------
  8536. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  8537. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  8538. int & ImageIndex)
  8539. {
  8540. if (AIndex < 0)
  8541. {
  8542. AIndex = Sender->ItemIndex;
  8543. }
  8544. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  8545. }
  8546. //---------------------------------------------------------------------------
  8547. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  8548. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  8549. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  8550. {
  8551. ARect.Left += (10 * AIndex);
  8552. }
  8553. //---------------------------------------------------------------------------
  8554. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  8555. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  8556. {
  8557. AWidth += (10 * AIndex);
  8558. }
  8559. //---------------------------------------------------------------------------
  8560. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  8561. TObject * Sender)
  8562. {
  8563. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  8564. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8565. int Index = RemotePathComboBox->ItemIndex;
  8566. while (Index < RemotePathComboBox->Strings->Count - 1)
  8567. {
  8568. APath = UnixExtractFileDir(APath);
  8569. Index++;
  8570. }
  8571. // VanDyke style paths
  8572. if (APath.IsEmpty())
  8573. {
  8574. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  8575. APath = ROOTDIRECTORY;
  8576. }
  8577. if (RemoteDirView->Path != APath)
  8578. {
  8579. RemoteDirView->Path = APath;
  8580. }
  8581. }
  8582. //---------------------------------------------------------------------------
  8583. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  8584. {
  8585. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  8586. DebugUsedParam(Sender);
  8587. UpdateRemotePathComboBox(true);
  8588. }
  8589. //---------------------------------------------------------------------------
  8590. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  8591. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  8592. {
  8593. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8594. DebugAssert(DirView != NULL);
  8595. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  8596. {
  8597. HWND Edit = ListView_GetEditControl(DirView->Handle);
  8598. // OnEditing is called also from TCustomListView::CanEdit
  8599. if (Edit != NULL)
  8600. {
  8601. EditSelectBaseName(Edit);
  8602. }
  8603. }
  8604. }
  8605. //---------------------------------------------------------------------------
  8606. //---------------------------------------------------------------------------
  8607. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  8608. {
  8609. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  8610. Result->AutoDetectDnD = false;
  8611. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  8612. Result->RenderDataOn = rdoEnterAndDropSync;
  8613. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  8614. return Result;
  8615. }
  8616. //---------------------------------------------------------------------------
  8617. void __fastcall TCustomScpExplorerForm::CreateWnd()
  8618. {
  8619. TForm::CreateWnd();
  8620. if (FSessionsDragDropFilesEx == NULL)
  8621. {
  8622. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  8623. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  8624. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  8625. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  8626. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  8627. }
  8628. if (FQueueDragDropFilesEx == NULL)
  8629. {
  8630. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  8631. // No need to set OnDragOver as we do not have any restrictions
  8632. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  8633. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  8634. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  8635. }
  8636. }
  8637. //---------------------------------------------------------------------------
  8638. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  8639. {
  8640. TForm::DestroyWnd();
  8641. FSessionsDragDropFilesEx->DragDropControl = NULL;
  8642. FQueueDragDropFilesEx->DragDropControl = NULL;
  8643. }
  8644. //---------------------------------------------------------------------------
  8645. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  8646. {
  8647. SideEnter(FCurrentSide);
  8648. FEverShown = true;
  8649. }
  8650. //---------------------------------------------------------------------------
  8651. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  8652. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  8653. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  8654. {
  8655. if (!NonVisualDataModule->Busy)
  8656. {
  8657. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8658. Configuration->Usage->Inc(L"FileFinds");
  8659. LockWindow(true);
  8660. NonVisualDataModule->StartBusy();
  8661. WinConfiguration->LockedInterface = true;
  8662. try
  8663. {
  8664. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  8665. }
  8666. __finally
  8667. {
  8668. WinConfiguration->LockedInterface = false;
  8669. NonVisualDataModule->EndBusy();
  8670. UnlockWindow();
  8671. }
  8672. }
  8673. }
  8674. //---------------------------------------------------------------------------
  8675. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  8676. {
  8677. if (!NonVisualDataModule->Busy)
  8678. {
  8679. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8680. SetFocus();
  8681. RemoteDirView->Path = UnixExtractFilePath(Path);
  8682. TListItem * Item = RemoteDirView->FindFileItem(UnixExtractFileName(Path));
  8683. if (Item != NULL)
  8684. {
  8685. RemoteDirView->ItemFocused = Item;
  8686. RemoteDirView->ItemFocused->MakeVisible(false);
  8687. RemoteDirView->SetFocus();
  8688. }
  8689. }
  8690. }
  8691. //---------------------------------------------------------------------------
  8692. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  8693. {
  8694. bool Result = !NonVisualDataModule->Busy;
  8695. if (Result)
  8696. {
  8697. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8698. }
  8699. return Result;
  8700. }
  8701. //---------------------------------------------------------------------------
  8702. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  8703. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8704. {
  8705. if (CanOperateOnFoundFiles(ATerminal))
  8706. {
  8707. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  8708. FOnFileOperationFinished = OnFileOperationFinished;
  8709. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  8710. }
  8711. }
  8712. //---------------------------------------------------------------------------
  8713. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  8714. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8715. {
  8716. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  8717. }
  8718. //---------------------------------------------------------------------------
  8719. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  8720. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8721. {
  8722. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  8723. }
  8724. //---------------------------------------------------------------------------
  8725. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  8726. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8727. {
  8728. if (CanOperateOnFoundFiles(ATerminal))
  8729. {
  8730. for (int Index = 0; Index < FileList->Count; Index++)
  8731. {
  8732. UnicodeString FileName = FileList->Strings[Index];
  8733. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  8734. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  8735. OnFileOperationFinished(FileName, true);
  8736. }
  8737. }
  8738. }
  8739. //---------------------------------------------------------------------------
  8740. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  8741. {
  8742. FFileFindTerminal = Terminal;
  8743. ShowFileFindDialog(
  8744. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  8745. DoDownloadFoundFiles, DoEditFoundFiles);
  8746. }
  8747. //---------------------------------------------------------------------------
  8748. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  8749. {
  8750. FTaskbarList = TaskbarList;
  8751. }
  8752. //---------------------------------------------------------------------------
  8753. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  8754. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  8755. {
  8756. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8757. if (Index >= 0)
  8758. {
  8759. if (Button == mbLeft)
  8760. {
  8761. // "Mouse down" is raised only after tab is switched.
  8762. // If switching tab (switching session) takes long enough for user
  8763. // to actually release the button, "mouse down" is still raised,
  8764. // but we do not get "mouse up" event, so dragging is not cancelled,
  8765. // prevent that by not beginning dragging in the first place.
  8766. if (IsKeyPressed(VK_LBUTTON))
  8767. {
  8768. // when user clicks the "+", we get mouse down only after the session
  8769. // is closed, when new session tab is already on X:Y, so dragging
  8770. // starts, prevent that
  8771. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  8772. {
  8773. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8774. if (Terminal != NULL)
  8775. {
  8776. SessionsPageControl->BeginDrag(false);
  8777. }
  8778. }
  8779. }
  8780. }
  8781. else if (Button == mbMiddle)
  8782. {
  8783. // ignore middle-click for "New session tab"
  8784. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8785. if (Terminal != NULL)
  8786. {
  8787. SessionsPageControl->ActivePageIndex = Index;
  8788. // Switch to session tab (has to be session tab, due to previous check)
  8789. if (DebugAlwaysTrue(SessionTabSwitched()))
  8790. {
  8791. CloseSession();
  8792. }
  8793. }
  8794. }
  8795. }
  8796. }
  8797. //---------------------------------------------------------------------------
  8798. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  8799. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  8800. {
  8801. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8802. // do not allow dropping on the "+" tab
  8803. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8804. if ((TargetTerminal != NULL) &&
  8805. (SessionsPageControl->ActivePage->PageIndex != Index))
  8806. {
  8807. Configuration->Usage->Inc(L"SessionTabMoves");
  8808. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  8809. // but we want to actually prevent that to avoid flicker
  8810. SessionsPageControl->ActivePage->PageIndex = Index;
  8811. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  8812. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  8813. }
  8814. }
  8815. //---------------------------------------------------------------------------
  8816. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  8817. TObject * Sender, TObject * Source, int X, int Y,
  8818. TDragState /*State*/, bool & Accept)
  8819. {
  8820. Accept = (Sender == Source);
  8821. if (Accept)
  8822. {
  8823. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8824. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8825. // do not allow dragging to the "+" tab
  8826. Accept = (Terminal != NULL);
  8827. }
  8828. }
  8829. //---------------------------------------------------------------------------
  8830. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(int /*KeyState*/,
  8831. const TPoint & Point, int & Effect)
  8832. {
  8833. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  8834. if (Index < 0)
  8835. {
  8836. Effect = DROPEFFECT_None;
  8837. }
  8838. else
  8839. {
  8840. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8841. // do not allow dropping on the "+" tab
  8842. if (TargetTerminal == NULL)
  8843. {
  8844. Effect = DROPEFFECT_None;
  8845. }
  8846. }
  8847. }
  8848. //---------------------------------------------------------------------------
  8849. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  8850. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  8851. {
  8852. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  8853. // do not allow dropping on the "+" tab
  8854. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8855. if (TargetTerminal != NULL)
  8856. {
  8857. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  8858. if (!IsFileControl(DropSourceControl, osRemote))
  8859. {
  8860. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  8861. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  8862. // Why don't we use Terminal->CurrentDirectory directly?
  8863. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory, false);
  8864. }
  8865. }
  8866. }
  8867. //---------------------------------------------------------------------------
  8868. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  8869. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  8870. {
  8871. // Downloads are handled in RemoteFileControlDDEnd
  8872. if (!IsFileControl(DropSourceControl, osRemote))
  8873. {
  8874. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  8875. Terminal->CurrentDirectory,
  8876. // force queue
  8877. true);
  8878. }
  8879. }
  8880. //---------------------------------------------------------------------------
  8881. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  8882. {
  8883. FShowing = false;
  8884. // Do not save empty workspace
  8885. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  8886. {
  8887. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  8888. UnicodeString Name = WorkspaceName();
  8889. DoSaveWorkspace(
  8890. Name, DataList.get(),
  8891. !Configuration->DisablePasswordStoring &&
  8892. WinConfiguration->AutoSaveWorkspacePasswords, false);
  8893. WinConfiguration->LastStoredSession = Name;
  8894. }
  8895. }
  8896. //---------------------------------------------------------------------------
  8897. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  8898. {
  8899. TManagedTerminal * ManagedTerminal =
  8900. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  8901. if (ManagedTerminal != NULL)
  8902. {
  8903. ManagedTerminal->DirectoryLoaded = Now();
  8904. }
  8905. }
  8906. //---------------------------------------------------------------------------
  8907. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  8908. TListItem * /*Item*/, bool /*Selected*/)
  8909. {
  8910. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8911. switch (DirView->LastSelectMethod)
  8912. {
  8913. case smKeyboard:
  8914. Configuration->Usage->Inc(L"KeyboardSelections");
  8915. break;
  8916. case smMouse:
  8917. Configuration->Usage->Inc(L"MouseSelections");
  8918. break;
  8919. }
  8920. }
  8921. //---------------------------------------------------------------------------
  8922. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  8923. {
  8924. if (Color != 0)
  8925. {
  8926. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  8927. }
  8928. else
  8929. {
  8930. return FSessionTabImageIndex;
  8931. }
  8932. }
  8933. //---------------------------------------------------------------------------
  8934. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  8935. {
  8936. TPngImageCollectionItem * Item =
  8937. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  8938. FSessionColors->AddPng(Item->PngImage);
  8939. return FSessionColors->Count - 1;
  8940. }
  8941. //---------------------------------------------------------------------------
  8942. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  8943. {
  8944. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  8945. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  8946. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  8947. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  8948. }
  8949. //---------------------------------------------------------------------------
  8950. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  8951. {
  8952. for (int Index = 0; Index < Control->ControlCount; Index++)
  8953. {
  8954. TControl * AControl = Control->Controls[Index];
  8955. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  8956. if (WinControl != NULL)
  8957. {
  8958. CollectItemsWithTextDisplayMode(WinControl);
  8959. }
  8960. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  8961. if (Toolbar != NULL)
  8962. {
  8963. // we care for top-level items only
  8964. TTBCustomItem * Items = Toolbar->Items;
  8965. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  8966. {
  8967. TTBCustomItem * Item = Items->Items[ItemIndex];
  8968. if (((Item->DisplayMode == nbdmImageAndText) ||
  8969. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  8970. EligibleForImageDisplayMode(Item))
  8971. {
  8972. FItemsWithTextDisplayMode.insert(Item);
  8973. }
  8974. }
  8975. }
  8976. }
  8977. }
  8978. //---------------------------------------------------------------------------
  8979. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  8980. {
  8981. return true;
  8982. }
  8983. //---------------------------------------------------------------------------
  8984. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  8985. {
  8986. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  8987. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  8988. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  8989. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  8990. bool Result = true;
  8991. while (Result && (i != FItemsWithTextDisplayMode.end()))
  8992. {
  8993. TTBCustomItem * Item = *i;
  8994. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  8995. if (Label != NULL)
  8996. {
  8997. // optimization
  8998. if (Label->Visible == SelectiveToolbarText)
  8999. {
  9000. Result = false;
  9001. }
  9002. else
  9003. {
  9004. Label->Visible = SelectiveToolbarText;
  9005. }
  9006. }
  9007. else
  9008. {
  9009. // optimization
  9010. if (Item->DisplayMode == DisplayMode)
  9011. {
  9012. Result = false;
  9013. }
  9014. else
  9015. {
  9016. Item->DisplayMode = DisplayMode;
  9017. }
  9018. }
  9019. ++i;
  9020. }
  9021. if (Result)
  9022. {
  9023. UpdateNewSessionTab();
  9024. }
  9025. return Result;
  9026. }
  9027. //---------------------------------------------------------------------------
  9028. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9029. {
  9030. DebugFail();
  9031. }
  9032. //---------------------------------------------------------------------------
  9033. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9034. {
  9035. // Among other, a lock level is non zero, while directory is being loaded,
  9036. // even when it happens as a result of dirview navigation,
  9037. // i.e. when TNonVisualDataModule is NOT FBusy.
  9038. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9039. // Among other this prevents a panel auto update to occur while
  9040. // directory is changing.
  9041. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9042. }
  9043. //---------------------------------------------------------------------------
  9044. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  9045. {
  9046. // While the window is disabled, we still seem to process menu shortcuts at least,
  9047. // so stop it at least here.
  9048. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9049. return
  9050. (Allowed == aaUpdate) ||
  9051. !NonVisualDataModule->Busy;
  9052. }
  9053. //---------------------------------------------------------------------------
  9054. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9055. {
  9056. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9057. }
  9058. //---------------------------------------------------------------------------
  9059. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9060. {
  9061. // This is somewhat redundant to LockWindow() call from
  9062. // TTerminalManager::TerminalReadDirectoryProgress.
  9063. // But disabling window is known not to block keyboard shorcuts
  9064. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9065. if (Busy > 0)
  9066. {
  9067. if (NonVisualDataModule->Busy)
  9068. {
  9069. State = false;
  9070. }
  9071. else
  9072. {
  9073. NonVisualDataModule->StartBusy();
  9074. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9075. LockWindow();
  9076. }
  9077. }
  9078. else if (Busy < 0)
  9079. {
  9080. UnlockWindow();
  9081. NonVisualDataModule->EndBusy();
  9082. }
  9083. else
  9084. {
  9085. State = NonVisualDataModule->Busy;
  9086. }
  9087. }
  9088. //---------------------------------------------------------------------------
  9089. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9090. {
  9091. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9092. // no context menu for "New session tab"
  9093. if ((Index >= 0) && (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != NULL))
  9094. {
  9095. SessionsPageControl->ActivePageIndex = Index;
  9096. if (DebugAlwaysTrue(SessionTabSwitched()))
  9097. {
  9098. // copied from TControl.WMContextMenu
  9099. SendCancelMode(SessionsPageControl);
  9100. // explicit popup instead of using PopupMenu property
  9101. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9102. // while connecting yet not-connected session and hence
  9103. // allowing an access to commands over not-completelly connected session
  9104. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9105. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  9106. PopupMenu->PopupComponent = SessionsPageControl;
  9107. PopupMenu->Popup(Point.x, Point.y);
  9108. }
  9109. }
  9110. Handled = true;
  9111. }
  9112. //---------------------------------------------------------------------------
  9113. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9114. {
  9115. return (Terminal != NULL) && Terminal->Active && Terminal->IsCapable[fcChangePassword];
  9116. }
  9117. //---------------------------------------------------------------------------
  9118. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9119. {
  9120. Configuration->Usage->Inc(L"PasswordChanges");
  9121. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9122. ChangePasswordData->DisableAuthentationsExceptPassword();
  9123. ChangePasswordData->ChangePassword = true;
  9124. std::unique_ptr<TTerminal> ChangePasswordSession(
  9125. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9126. ChangePasswordSession->AutoReadDirectory = false;
  9127. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9128. {
  9129. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9130. }
  9131. else
  9132. {
  9133. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9134. }
  9135. }
  9136. //---------------------------------------------------------------------------
  9137. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9138. {
  9139. // No nice way to assert SSH2
  9140. return (Terminal != NULL) && Terminal->Active && (Terminal->FSProtocol == cfsSFTP);
  9141. }
  9142. //---------------------------------------------------------------------------
  9143. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9144. {
  9145. TOperationVisualizer OperationVisualizer;
  9146. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9147. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9148. }
  9149. //---------------------------------------------------------------------------
  9150. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9151. {
  9152. TForm::ChangeScale(M, D);
  9153. int APixelsPerInch = GetControlPixelsPerInch(this);
  9154. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9155. try
  9156. {
  9157. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9158. }
  9159. __finally
  9160. {
  9161. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9162. }
  9163. }
  9164. //---------------------------------------------------------------------------
  9165. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9166. {
  9167. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9168. }
  9169. //---------------------------------------------------------------------
  9170. class TPublicControl : public TControl
  9171. {
  9172. friend class TCustomScpExplorerForm;
  9173. };
  9174. //---------------------------------------------------------------------------
  9175. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9176. {
  9177. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9178. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9179. }
  9180. //---------------------------------------------------------------------------
  9181. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9182. {
  9183. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9184. OpenBookmark(Side, Bookmark);
  9185. }
  9186. //---------------------------------------------------------------------------
  9187. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9188. {
  9189. DoBookmarkClick(osLocal, Sender);
  9190. }
  9191. //---------------------------------------------------------------------------
  9192. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9193. {
  9194. DoBookmarkClick(osRemote, Sender);
  9195. }
  9196. //---------------------------------------------------------------------------
  9197. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9198. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9199. {
  9200. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9201. if (!WinConfiguration->UseLocationProfiles)
  9202. {
  9203. std::unique_ptr<TStringList> Directories(new TStringList());
  9204. Directories->CaseSensitive = (Side == osRemote);
  9205. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9206. {
  9207. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9208. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9209. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9210. {
  9211. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9212. Item->Caption = EscapeHotkey(Directory);
  9213. Item->ShortCut = Bookmark->ShortCut;
  9214. Item->OnClick = OnBookmarkClick;
  9215. Item->Tag = reinterpret_cast<int>(Bookmark);
  9216. Directories->Add(Directory);
  9217. Menu->Add(Item.release());
  9218. }
  9219. }
  9220. }
  9221. else
  9222. {
  9223. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9224. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9225. {
  9226. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9227. if (!Bookmark->Node.IsEmpty())
  9228. {
  9229. Folders->Add(Bookmark->Node);
  9230. }
  9231. }
  9232. for (int Index = 0; Index < Folders->Count; Index++)
  9233. {
  9234. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9235. Item->Caption = Folders->Strings[Index];
  9236. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction->ImageIndex;
  9237. Folders->Objects[Index] = Item.get();
  9238. Menu->Add(Item.release());
  9239. }
  9240. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9241. {
  9242. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9243. TTBCustomItem * Parent;
  9244. if (!Bookmark->Node.IsEmpty())
  9245. {
  9246. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9247. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9248. }
  9249. else
  9250. {
  9251. Parent = Menu;
  9252. }
  9253. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9254. Item->Caption = Bookmark->Name;
  9255. Item->ShortCut = Bookmark->ShortCut;
  9256. Item->OnClick = OnBookmarkClick;
  9257. Item->Tag = reinterpret_cast<int>(Bookmark);
  9258. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9259. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9260. {
  9261. // No hint for location profiles that are actually mere "bookmarks"
  9262. }
  9263. else
  9264. {
  9265. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  9266. UnicodeString LongHint;
  9267. if (!Bookmark->Local.IsEmpty())
  9268. {
  9269. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  9270. }
  9271. if (!Bookmark->Remote.IsEmpty())
  9272. {
  9273. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  9274. }
  9275. AddToList(Hint, LongHint, L"|");
  9276. Item->Hint = Hint;
  9277. }
  9278. Parent->Add(Item.release());
  9279. }
  9280. }
  9281. }
  9282. //---------------------------------------------------------------------------
  9283. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  9284. {
  9285. Menu->Clear();
  9286. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  9287. AddMenuSeparator(Menu);
  9288. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  9289. AddMenuSeparator(Menu);
  9290. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9291. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction : NonVisualDataModule->RemoteAddBookmarkAction;
  9292. Menu->Add(Item.release());
  9293. }
  9294. //---------------------------------------------------------------------------
  9295. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  9296. {
  9297. bool Result = true;
  9298. try
  9299. {
  9300. if (Side == osRemote)
  9301. {
  9302. Terminal->ExceptionOnFail = true;
  9303. }
  9304. try
  9305. {
  9306. DirView(Side)->Path = Path;
  9307. }
  9308. __finally
  9309. {
  9310. if (Side == osRemote)
  9311. {
  9312. Terminal->ExceptionOnFail = false;
  9313. }
  9314. }
  9315. }
  9316. catch (Exception & E)
  9317. {
  9318. if ((Side != osRemote) || Terminal->Active)
  9319. {
  9320. ShowExtendedExceptionEx(Terminal, &E);
  9321. Result = false;
  9322. }
  9323. else
  9324. {
  9325. throw;
  9326. }
  9327. }
  9328. return Result;
  9329. }
  9330. //---------------------------------------------------------------------------
  9331. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  9332. {
  9333. // Make sure there some feedback even when loading tine local folders
  9334. TInstantOperationVisualizer Visualizer;
  9335. DirView(Side)->ReloadDirectory();
  9336. }
  9337. //---------------------------------------------------------------------------
  9338. void __fastcall TCustomScpExplorerForm::SessionsPageControlCloseButtonClick(TPageControl * /*Sender*/, int Index)
  9339. {
  9340. if (Index == SessionsPageControl->TabIndex)
  9341. {
  9342. CloseSession();
  9343. }
  9344. else
  9345. {
  9346. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  9347. TTerminal * Terminal = GetSessionTabTerminal(TabSheet);
  9348. TTerminalManager * Manager = TTerminalManager::Instance();
  9349. if (!Terminal->Active)
  9350. {
  9351. Manager->FreeTerminal(Terminal);
  9352. }
  9353. else
  9354. {
  9355. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  9356. if (CanCloseQueue(Queue))
  9357. {
  9358. Manager->FreeTerminal(Terminal);
  9359. }
  9360. }
  9361. }
  9362. }
  9363. //---------------------------------------------------------------------------
  9364. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side)
  9365. {
  9366. if (DebugAlwaysTrue(GetSide(Side) == osRemote))
  9367. {
  9368. TInstantOperationVisualizer Visualizer;
  9369. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  9370. // before we create a new data object
  9371. ClipboardClear();
  9372. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  9373. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  9374. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  9375. FClipboardFakeMonitors.reset(StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated));
  9376. if (FClipboardDragDropFilesEx.get() == NULL)
  9377. {
  9378. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  9379. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  9380. }
  9381. FClipboardDragDropFilesEx->FileList->Clear();
  9382. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  9383. FClipboardDragDropFilesEx->CopyToClipboard();
  9384. FClipboardTerminal = FTerminal;
  9385. // Need full paths, as cwd can be different once files are pasted
  9386. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(false, true)));
  9387. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  9388. {
  9389. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  9390. }
  9391. }
  9392. }
  9393. //---------------------------------------------------------------------------
  9394. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  9395. {
  9396. if (OpenClipboard(0))
  9397. {
  9398. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  9399. CloseClipboard();
  9400. }
  9401. }
  9402. //---------------------------------------------------------------------------
  9403. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  9404. {
  9405. return !FClipboardFakeDirectory.IsEmpty();
  9406. }
  9407. //---------------------------------------------------------------------------
  9408. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  9409. {
  9410. RemoveDir(ApiPath(FClipboardFakeDirectory));
  9411. FClipboardFakeDirectory = UnicodeString();
  9412. FClipboardTerminal = NULL;
  9413. // Also called in Idle()
  9414. FClipboardFakeMonitors.reset(NULL);
  9415. }
  9416. //---------------------------------------------------------------------------
  9417. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  9418. {
  9419. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  9420. {
  9421. ClipboardStop();
  9422. }
  9423. }
  9424. //---------------------------------------------------------------------------
  9425. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  9426. {
  9427. TTerminalManager * Manager = TTerminalManager::Instance();
  9428. Manager->ActiveTerminal = FClipboardTerminal;
  9429. TTransferOperationParam Params;
  9430. Params.TargetDirectory = TargetDirectory;
  9431. Params.DragDrop = DragDrop;
  9432. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  9433. }
  9434. //---------------------------------------------------------------------------
  9435. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  9436. {
  9437. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  9438. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  9439. {
  9440. RemoveDir(ApiPath(FileName));
  9441. if (!NonVisualDataModule->Busy)
  9442. {
  9443. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  9444. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  9445. }
  9446. }
  9447. }
  9448. //---------------------------------------------------------------------------