CustomScpExplorer.cpp 328 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094
  1. //---------------------------------------------------------------------------
  2. #define NO_WIN32_LEAN_AND_MEAN
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include <Common.h>
  6. #include "CustomScpExplorer.h"
  7. #include <Bookmarks.h>
  8. #include <Interface.h>
  9. #include <Exceptions.h>
  10. #include <CoreMain.h>
  11. #include <FileSystems.h>
  12. #include <TextsCore.h>
  13. #include <TextsWin.h>
  14. #include <HelpWin.h>
  15. #include <VCLCommon.h>
  16. #include <Progress.h>
  17. #include <SynchronizeProgress.h>
  18. #include <DragExt.h>
  19. #include <WinApi.h>
  20. #include "GUITools.h"
  21. #include "NonVisual.h"
  22. #include "Glyphs.h"
  23. #include "Tools.h"
  24. #include "WinConfiguration.h"
  25. #include "TerminalManager.h"
  26. #include "EditorManager.h"
  27. #include "ProgParams.h"
  28. #include "Setup.h"
  29. #include <Consts.hpp>
  30. #include <DateUtils.hpp>
  31. #include <TB2Common.hpp>
  32. #include <DirectoryMonitor.hpp>
  33. //---------------------------------------------------------------------------
  34. #pragma package(smart_init)
  35. #pragma link "CustomDirView"
  36. #pragma link "CustomUnixDirView"
  37. #pragma link "IEListView"
  38. #pragma link "NortonLikeListView"
  39. #pragma link "UnixDirView"
  40. #pragma link "CustomDriveView"
  41. #pragma link "UnixDriveView"
  42. #pragma link "CustomDriveView"
  43. #pragma link "TB2Dock"
  44. #pragma link "TBXStatusBars"
  45. #pragma link "TB2Item"
  46. #pragma link "TB2Toolbar"
  47. #pragma link "ThemePageControl"
  48. #pragma link "PathLabel"
  49. #ifndef NO_RESOURCES
  50. #pragma resource "*.dfm"
  51. #endif
  52. //---------------------------------------------------------------------------
  53. #define SAVE_SELECTION(DIRVIEW) \
  54. UnicodeString FocusFile = L""; \
  55. UnicodeString LastFocusedFile = L""; \
  56. if (DIRVIEW->ItemFocused) LastFocusedFile = DIRVIEW->ItemFocused->Caption; \
  57. { TListItem * ClosestUnselected = DIRVIEW->ClosestUnselected(DIRVIEW->ItemFocused); \
  58. if (ClosestUnselected) FocusFile = ClosestUnselected->Caption; }
  59. #define RESTORE_SELECTION(DIRVIEW) \
  60. if (!LastFocusedFile.IsEmpty() && \
  61. (!DIRVIEW->ItemFocused || (DIRVIEW->ItemFocused->Caption != LastFocusedFile))) \
  62. { \
  63. TListItem *ItemToSelect = DIRVIEW->FindFileItem(FocusFile); \
  64. if (ItemToSelect) \
  65. { \
  66. DIRVIEW->ItemFocused = ItemToSelect; \
  67. DIRVIEW->ItemFocused->MakeVisible(False); \
  68. } \
  69. }
  70. //---------------------------------------------------------------------------
  71. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  72. static const int SessionPanelCount = 4;
  73. //---------------------------------------------------------------------------
  74. class TMutexGuard
  75. {
  76. public:
  77. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  78. int Timeout = 5000)
  79. {
  80. FMutex = NULL;
  81. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  82. if (WaitResult == WAIT_TIMEOUT)
  83. {
  84. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  85. }
  86. else
  87. {
  88. FMutex = AMutex;
  89. }
  90. }
  91. ~TMutexGuard()
  92. {
  93. if (FMutex != NULL)
  94. {
  95. ReleaseMutex(FMutex);
  96. }
  97. }
  98. private:
  99. HANDLE FMutex;
  100. };
  101. //---------------------------------------------------------------------------
  102. class TWindowLock
  103. {
  104. public:
  105. TWindowLock(TCustomScpExplorerForm * Form) :
  106. FForm(Form)
  107. {
  108. FForm->LockWindow();
  109. }
  110. ~TWindowLock()
  111. {
  112. FForm->UnlockWindow();
  113. }
  114. private:
  115. TCustomScpExplorerForm * FForm;
  116. };
  117. //---------------------------------------------------------------------------
  118. class TAutoBatch
  119. {
  120. public:
  121. TAutoBatch(TCustomScpExplorerForm * Form) :
  122. FForm(Form)
  123. {
  124. FForm->BatchStart(FBatchStorage);
  125. }
  126. ~TAutoBatch()
  127. {
  128. FForm->BatchEnd(FBatchStorage);
  129. }
  130. private:
  131. TCustomScpExplorerForm * FForm;
  132. void * FBatchStorage;
  133. };
  134. //---------------------------------------------------------------------------
  135. struct TTransferOperationParam
  136. {
  137. TTransferOperationParam();
  138. UnicodeString TargetDirectory;
  139. bool Temp;
  140. bool DragDrop;
  141. int Options;
  142. TAutoSwitch Queue;
  143. };
  144. //---------------------------------------------------------------------------
  145. TTransferOperationParam::TTransferOperationParam()
  146. {
  147. Temp = false;
  148. DragDrop = false;
  149. Options = 0;
  150. Queue = asAuto;
  151. }
  152. //---------------------------------------------------------------------------
  153. class TTransferPresetNoteData : public TObject
  154. {
  155. public:
  156. UnicodeString Message;
  157. };
  158. //---------------------------------------------------------------------------
  159. class TTerminalNoteData : public TObject
  160. {
  161. public:
  162. TTerminal * Terminal;
  163. };
  164. //---------------------------------------------------------------------------
  165. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  166. FFormRestored(false),
  167. TForm(Owner)
  168. {
  169. FCurrentSide = osRemote;
  170. FEverShown = false;
  171. FDocks = new TList();
  172. RestoreParams();
  173. ConfigurationChanged();
  174. RemoteDirView->Invalidate();
  175. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  176. NonVisualDataModule->ScpExplorer = this;
  177. FAutoOperation = false;
  178. FOnFileOperationFinished = NULL;
  179. FForceExecution = false;
  180. FIgnoreNextDialogChar = 0;
  181. FErrorList = NULL;
  182. FSynchronizeProgressForm = NULL;
  183. FProgressForm = NULL;
  184. FRefreshLocalDirectory = false;
  185. FRefreshRemoteDirectory = false;
  186. FDDExtMapFile = NULL;
  187. // CreateMutexW keeps failing with ERROR_NOACCESS
  188. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  189. DebugAssert(FDDExtMutex != NULL);
  190. FDDTargetControl = NULL;
  191. FDelayedDeletionTimer = NULL;
  192. FDelayedDeletionList = new TStringList();
  193. FDDFileList = NULL;
  194. FPendingTempSpaceWarn = false;
  195. FCapturedLog = NULL;
  196. FDragDropOperation = false;
  197. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  198. FAllowTransferPresetAutoSelect = true;
  199. FCopyParamDefault = L"";
  200. FSynchronizeController = NULL;
  201. FPendingQueueActionItem = NULL;
  202. FLockLevel = 0;
  203. FLockSuspendLevel = 0;
  204. FDisabledOnLockSuspend = false;
  205. FAlternativeDelete = false;
  206. FTrayIcon = new ::TTrayIcon(0);
  207. FTrayIcon->OnClick = TrayIconClick;
  208. FMaxQueueLength = 0;
  209. FLastContextPopupScreenPoint = TPoint(-1, -1);
  210. FTransferResumeList = NULL;
  211. FMoveToQueue = false;
  212. FStandaloneEditing = false;
  213. FOnFeedSynchronizeError = NULL;
  214. FNeedSession = false;
  215. FDoNotIdleCurrentTerminal = 0;
  216. FEditorManager = new TEditorManager();
  217. FEditorManager->OnFileChange = ExecutedFileChanged;
  218. FEditorManager->OnFileReload = ExecutedFileReload;
  219. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  220. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  221. FLocalEditors = new TList();
  222. FQueueStatus = NULL;
  223. FQueueStatusSection = new TCriticalSection();
  224. FQueueStatusInvalidated = false;
  225. FQueueItemInvalidated = false;
  226. FQueueActedItem = NULL;
  227. FQueueController = new TQueueController(QueueView3);
  228. FUserActionTimer = new TTimer(this);
  229. FUserActionTimer->Enabled = false;
  230. FUserActionTimer->Interval = 10;
  231. FUserActionTimer->OnTimer = UserActionTimer;
  232. FNotes = new TStringList();
  233. FNoteTimer = new TTimer(this);
  234. FNoteTimer->Enabled = false;
  235. FNoteTimer->OnTimer = NoteTimer;
  236. FOnNoteClick = NULL;
  237. FOle32Library = LoadLibrary(L"Ole32.dll");
  238. FDragMoveCursor = FOle32Library != NULL ?
  239. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  240. UseSystemSettings(this);
  241. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  242. static_cast<TObject*>(GetComponent(fcTransferList)));
  243. DebugAssert(TransferList != NULL);
  244. FTransferListHoverIndex = -1;
  245. TransferList->OnChange = TransferListChange;
  246. TransferList->OnDrawItem = TransferListDrawItem;
  247. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))));
  248. SetSubmenu(NonVisualDataModule->ColorMenuItem);
  249. UseDesktopFont(SessionsPageControl);
  250. UpdateSessionsPageControlHeight();
  251. UseDesktopFont(RemoteDirView);
  252. UseDesktopFont(RemoteDriveView);
  253. UseDesktopFont(QueueView3);
  254. UseDesktopFont(QueueLabel);
  255. UseDesktopFont(RemoteStatusBar);
  256. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  257. QueueSplitter->ShowHint = true;
  258. RemotePanelSplitter->ShowHint = true;
  259. UpdateImages();
  260. FCustomCommandMenu = CreateTBXPopupMenu(this);
  261. FCustomCommandLocalFileList = NULL;
  262. FCustomCommandRemoteFileList = NULL;
  263. FSessionColors = new TPngImageList(this);
  264. FSessionColors->ColorDepth = cd32Bit;
  265. AddFixedSessionImages();
  266. SessionsPageControl->Images = FSessionColors;
  267. QueueLabel->FocusControl = QueueView3;
  268. UpdateQueueLabel();
  269. FRemoteDirViewWasFocused = true;
  270. CreateHiddenWindow();
  271. StartUpdates();
  272. }
  273. //---------------------------------------------------------------------------
  274. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  275. {
  276. // this has to be one of the very first things to do
  277. StopUpdateThread();
  278. SessionsPageControl->Images = NULL;
  279. SAFE_DESTROY(FSessionColors);
  280. SAFE_DESTROY(FSessionsDragDropFilesEx);
  281. delete FCustomCommandLocalFileList;
  282. delete FCustomCommandRemoteFileList;
  283. delete FCustomCommandMenu;
  284. delete FTrayIcon;
  285. FTrayIcon = NULL;
  286. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  287. delete FEditorManager;
  288. ForceCloseLocalEditors();
  289. delete FLocalEditors;
  290. HideFileFindDialog();
  291. if (FDelayedDeletionTimer)
  292. {
  293. DoDelayedDeletion(NULL);
  294. SAFE_DESTROY(FDelayedDeletionTimer);
  295. }
  296. SAFE_DESTROY(FDelayedDeletionList);
  297. // sometimes we do not get DDEnd so the list is not released
  298. SAFE_DESTROY(FDDFileList);
  299. DebugAssert(FSynchronizeController == NULL);
  300. CloseHandle(FDDExtMutex);
  301. FDDExtMutex = NULL;
  302. FreeLibrary(FOle32Library);
  303. FOle32Library = NULL;
  304. FDragMoveCursor = NULL;
  305. DebugAssert(!FErrorList);
  306. if (FEverShown)
  307. {
  308. // when window is never shown (like when running command-line operation),
  309. // particularly window site is not restored correctly (BoundsRect value set
  310. // in RestoreForm gets lost during handle allocation), so we do not want
  311. // it to be stored
  312. StoreParams();
  313. }
  314. Terminal = NULL;
  315. Queue = NULL;
  316. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  317. NonVisualDataModule->ScpExplorer = NULL;
  318. delete FQueueController;
  319. FQueueController = NULL;
  320. delete FQueueStatusSection;
  321. FQueueStatusSection = NULL;
  322. delete FQueueStatus;
  323. FQueueStatus = NULL;
  324. delete FUserActionTimer;
  325. FUserActionTimer = NULL;
  326. delete FNoteTimer;
  327. delete FNotes;
  328. delete FNoteData;
  329. SAFE_DESTROY(FDocks);
  330. SAFE_DESTROY(FHistoryMenu[0][0]);
  331. SAFE_DESTROY(FHistoryMenu[0][1]);
  332. SAFE_DESTROY(FHistoryMenu[1][0]);
  333. SAFE_DESTROY(FHistoryMenu[1][1]);
  334. if (FHiddenWindow != NULL)
  335. {
  336. DestroyWindow(FHiddenWindow);
  337. FHiddenWindow = NULL;
  338. }
  339. }
  340. //---------------------------------------------------------------------------
  341. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  342. {
  343. std::unique_ptr<TSessionData> Data;
  344. if (!Session.IsEmpty())
  345. {
  346. bool DefaultsOnly;
  347. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  348. }
  349. TTerminalManager * Manager = TTerminalManager::Instance();
  350. for (int Index = 0; Index < Manager->Count; Index++)
  351. {
  352. TTerminal * Terminal = Manager->Terminals[Index];
  353. if (Session.IsEmpty() ||
  354. Terminal->SessionData->IsSameSite(Data.get()))
  355. {
  356. if (Path.IsEmpty())
  357. {
  358. Terminal->ClearCaches();
  359. }
  360. else
  361. {
  362. Terminal->DirectoryModified(Path, true);
  363. }
  364. }
  365. }
  366. // We should flag a pending refresh for the background terminals or busy foreground terminals
  367. if ((Terminal != NULL) && Terminal->Active &&
  368. CanCommandLineFromAnotherInstance() &&
  369. (Session.IsEmpty() || Terminal->SessionData->IsSameSite(Data.get())) &&
  370. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  371. {
  372. Terminal->ReloadDirectory();
  373. }
  374. }
  375. //---------------------------------------------------------------------------
  376. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  377. {
  378. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  379. size_t MessageSize = sizeof(TCopyDataMessage);
  380. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  381. if (Result)
  382. {
  383. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  384. Result = (Message.Version == TCopyDataMessage::Version1);
  385. if (Result)
  386. {
  387. switch (Message.Command)
  388. {
  389. case TCopyDataMessage::CommandCanCommandLine:
  390. Result = CanCommandLineFromAnotherInstance();
  391. break;
  392. case TCopyDataMessage::CommandCommandLine:
  393. {
  394. UnicodeString CommandLine(Message.CommandLine);
  395. Result = CommandLineFromAnotherInstance(CommandLine);
  396. }
  397. break;
  398. case TCopyDataMessage::RefreshPanel:
  399. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  400. break;
  401. case TCopyDataMessage::MainWindowCheck:
  402. Result = true;
  403. break;
  404. default:
  405. Result = false;
  406. break;
  407. }
  408. }
  409. }
  410. Message.Result = Result ? 1 : 0;
  411. }
  412. //---------------------------------------------------------------------------
  413. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  414. {
  415. WNDCLASS WindowClass = {0};
  416. WindowClass.lpfnWndProc = DefWindowProc;
  417. WindowClass.hInstance = HInstance;
  418. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  419. FHiddenWindow = NULL;
  420. if (RegisterClass(&WindowClass))
  421. {
  422. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  423. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  424. }
  425. }
  426. //---------------------------------------------------------------------------
  427. bool __fastcall TCustomScpExplorerForm::CanConsole()
  428. {
  429. return (Terminal != NULL) && (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  430. }
  431. //---------------------------------------------------------------------------
  432. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  433. {
  434. bool Result = Visible && !NonVisualDataModule->Busy;
  435. return Result;
  436. }
  437. //---------------------------------------------------------------------------
  438. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  439. const UnicodeString & CommandLine)
  440. {
  441. TProgramParams Params(CommandLine);
  442. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  443. if (Result)
  444. {
  445. NonVisualDataModule->StartBusy();
  446. try
  447. {
  448. // this action is initiated from another process,
  449. // so it's likely that our window is not visible,
  450. // and user won't see what is going on
  451. Application->BringToFront();
  452. // reload sessions as we may be asked to open a session
  453. // just stored by another instance
  454. StoredSessions->Reload();
  455. UnicodeString SessionName = Params.Param[1];
  456. std::unique_ptr<TObjectList> DataList(new TObjectList());
  457. UnicodeString DownloadFile; // unused
  458. try
  459. {
  460. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this);
  461. }
  462. catch (EAbort &)
  463. {
  464. // Happens when opening session in PuTTY and in other situations,
  465. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  466. // to process the command-line on its own (or to pass it to yet another instance)
  467. }
  468. if (DataList->Count > 0)
  469. {
  470. TTerminalManager * Manager = TTerminalManager::Instance();
  471. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  472. }
  473. }
  474. __finally
  475. {
  476. NonVisualDataModule->EndBusy();
  477. }
  478. }
  479. return Result;
  480. }
  481. //---------------------------------------------------------------------------
  482. void __fastcall TCustomScpExplorerForm::SetTerminal(TTerminal * value)
  483. {
  484. if (FTerminal != value)
  485. {
  486. TerminalChanging();
  487. DoSetTerminal(value, false);
  488. }
  489. }
  490. //---------------------------------------------------------------------------
  491. void __fastcall TCustomScpExplorerForm::DoSetTerminal(TTerminal * value, bool Replace)
  492. {
  493. FTerminal = value;
  494. bool PrevAllowTransferPresetAutoSelect = FAllowTransferPresetAutoSelect;
  495. FAllowTransferPresetAutoSelect = false;
  496. try
  497. {
  498. TerminalChanged(Replace);
  499. }
  500. __finally
  501. {
  502. FAllowTransferPresetAutoSelect = PrevAllowTransferPresetAutoSelect;
  503. }
  504. if (Terminal != NULL)
  505. {
  506. TransferPresetAutoSelect();
  507. }
  508. }
  509. //---------------------------------------------------------------------------
  510. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TTerminal * value)
  511. {
  512. DoSetTerminal(value, true);
  513. }
  514. //---------------------------------------------------------------------------
  515. void __fastcall TCustomScpExplorerForm::TerminalChanging()
  516. {
  517. if (FTerminal != NULL)
  518. {
  519. UpdateTerminal(Terminal);
  520. }
  521. }
  522. //---------------------------------------------------------------------------
  523. void __fastcall TCustomScpExplorerForm::TerminalChanged(bool Replaced)
  524. {
  525. if (Replaced)
  526. {
  527. RemoteDirView->ReplaceTerminal(Terminal);
  528. }
  529. else
  530. {
  531. RemoteDirView->Terminal = Terminal;
  532. }
  533. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  534. TManagedTerminal * ManagedTerminal = NULL;
  535. if (Terminal != NULL)
  536. {
  537. if (Terminal->Active)
  538. {
  539. Terminal->RefreshDirectory();
  540. }
  541. ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  542. DebugAssert(ManagedTerminal != NULL);
  543. if (WinConfiguration->PreservePanelState)
  544. {
  545. if (ManagedTerminal->RemoteExplorerState != NULL)
  546. {
  547. RemoteDirView->RestoreState(ManagedTerminal->RemoteExplorerState);
  548. }
  549. else
  550. {
  551. RemoteDirView->ClearState();
  552. }
  553. }
  554. InitStatusBar();
  555. }
  556. DoTerminalListChanged();
  557. if (ManagedTerminal != NULL)
  558. {
  559. // this has to be set only after the tab is switched from DoTerminalListChanged,
  560. // otherwise we are changing color of wrong tab
  561. SessionColor = (TColor)ManagedTerminal->StateData->Color;
  562. }
  563. UpdateTransferList();
  564. // Update panels Enable state before refreshing the labels
  565. UpdateControls();
  566. }
  567. //---------------------------------------------------------------------------
  568. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  569. {
  570. if (Queue != value)
  571. {
  572. if (FQueue != NULL)
  573. {
  574. FQueue->OnListUpdate = NULL;
  575. FQueue->OnQueueItemUpdate = NULL;
  576. }
  577. FQueue = value;
  578. if (FQueue != NULL)
  579. {
  580. DebugAssert(FQueue->OnListUpdate == NULL);
  581. FQueue->OnListUpdate = QueueListUpdate;
  582. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  583. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  584. }
  585. QueueChanged();
  586. }
  587. }
  588. //---------------------------------------------------------------------------
  589. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  590. TListItem * Item)
  591. {
  592. if (FQueueActedItem == Item)
  593. {
  594. FQueueActedItem = NULL;
  595. if ((QueueView3->PopupMenu != NULL) &&
  596. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  597. {
  598. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  599. ReleaseCapture();
  600. }
  601. }
  602. if (Item->Data == FPendingQueueActionItem)
  603. {
  604. FPendingQueueActionItem = NULL;
  605. }
  606. }
  607. //---------------------------------------------------------------------------
  608. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  609. {
  610. {
  611. TGuard Guard(FQueueStatusSection);
  612. FQueueStatusInvalidated = false;
  613. if (FQueue != NULL)
  614. {
  615. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  616. }
  617. }
  618. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  619. {
  620. FMaxQueueLength = FQueueStatus->Count;
  621. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  622. }
  623. FQueueController->UpdateQueueStatus(FQueueStatus);
  624. SetQueueProgress();
  625. UpdateQueueView();
  626. bool IsEmpty = (FQueueStatus == NULL) || (FQueueStatus->Count == 0);
  627. if (IsEmpty && (Terminal != NULL))
  628. {
  629. TOnceDoneOperation OnceDoneOperation =
  630. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  631. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  632. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  633. {
  634. FQueue->Enabled = false;
  635. }
  636. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  637. {
  638. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  639. }
  640. }
  641. }
  642. //---------------------------------------------------------------------------
  643. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  644. {
  645. UnicodeString Result;
  646. if (FQueueStatus != NULL)
  647. {
  648. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  649. {
  650. TFileOperationProgressType * ProgressData =
  651. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  652. if ((ProgressData != NULL) && ProgressData->InProgress)
  653. {
  654. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  655. }
  656. }
  657. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  658. {
  659. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  660. }
  661. }
  662. return Result;
  663. }
  664. //---------------------------------------------------------------------------
  665. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  666. {
  667. ComponentVisible[fcQueueView] =
  668. (WinConfiguration->QueueView.Show == qvShow) ||
  669. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  670. (FQueueStatus != NULL) && (FQueueStatus->Count > 0));
  671. }
  672. //---------------------------------------------------------------------------
  673. void __fastcall TCustomScpExplorerForm::QueueChanged()
  674. {
  675. if (FQueueStatus != NULL)
  676. {
  677. delete FQueueStatus;
  678. FQueueStatus = NULL;
  679. }
  680. UpdateQueueStatus(true);
  681. }
  682. //---------------------------------------------------------------------------
  683. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  684. {
  685. if (FQueue == Queue)
  686. {
  687. FQueueStatusInvalidated = true;
  688. }
  689. }
  690. //---------------------------------------------------------------------------
  691. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  692. TQueueItem * Item)
  693. {
  694. if (FQueue == Queue)
  695. {
  696. TGuard Guard(FQueueStatusSection);
  697. // this may be running in parallel with QueueChanged
  698. if (FQueueStatus != NULL)
  699. {
  700. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  701. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  702. {
  703. FRefreshLocalDirectory = (QueueItem == NULL) ||
  704. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  705. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  706. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  707. }
  708. if (QueueItem != NULL)
  709. {
  710. QueueItem->UserData = (void*)true;
  711. FQueueItemInvalidated = true;
  712. }
  713. }
  714. }
  715. }
  716. //---------------------------------------------------------------------------
  717. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  718. {
  719. // during standalone editing, we have no way to see/control queue,
  720. // so we have to always popup prompts automatically
  721. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  722. }
  723. //---------------------------------------------------------------------------
  724. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  725. {
  726. if (FQueueStatus != NULL)
  727. {
  728. bool QueueAutoPopup = IsQueueAutoPopup();
  729. bool NeedRefresh = FQueueController->NeedRefresh();
  730. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  731. FQueueItemInvalidated = false;
  732. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  733. bool Updated = false;
  734. TQueueItemProxy * QueueItem;
  735. bool WasUserAction;
  736. for (int Index = 0; Index < Limit; Index++)
  737. {
  738. bool Update = false;
  739. QueueItem = FQueueStatus->Items[Index];
  740. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  741. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  742. (FPendingQueueActionItem == NULL))
  743. {
  744. FPendingQueueActionItem = QueueItem;
  745. FUserActionTimer->Enabled = true;
  746. }
  747. if ((bool)QueueItem->UserData)
  748. {
  749. QueueItem->UserData = (void*)false;
  750. QueueItem->Update();
  751. Updated = true;
  752. Update = true;
  753. }
  754. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  755. {
  756. Update = true;
  757. }
  758. if (Update)
  759. {
  760. FQueueController->RefreshQueueItem(QueueItem);
  761. }
  762. }
  763. if (Updated)
  764. {
  765. NonVisualDataModule->UpdateNonVisibleActions();
  766. SetQueueProgress();
  767. }
  768. }
  769. }
  770. //---------------------------------------------------------------------------
  771. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  772. {
  773. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  774. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  775. }
  776. //---------------------------------------------------------------------------
  777. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  778. {
  779. if (ProgressData->Operation != foCalculateSize)
  780. {
  781. int OverallProgress;
  782. // FProgressForm is null when this is called from SetQueueProgress
  783. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  784. {
  785. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  786. }
  787. else
  788. {
  789. OverallProgress = ProgressData->OverallProgress();
  790. }
  791. // implies TBPF_NORMAL
  792. FTaskbarList->SetProgressValue(GetMainForm()->Handle, OverallProgress, 100);
  793. }
  794. else
  795. {
  796. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  797. }
  798. }
  799. //---------------------------------------------------------------------------
  800. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  801. {
  802. TTerminalManager::Instance()->QueueStatusUpdated();
  803. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  804. {
  805. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  806. {
  807. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  808. {
  809. TFileOperationProgressType * ProgressData = NULL;
  810. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  811. {
  812. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  813. }
  814. if ((ProgressData != NULL) &&
  815. ProgressData->InProgress)
  816. {
  817. SetTaskbarListProgressValue(ProgressData);
  818. }
  819. else
  820. {
  821. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  822. }
  823. }
  824. else
  825. {
  826. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  827. }
  828. }
  829. else
  830. {
  831. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  832. }
  833. }
  834. UpdateQueueLabel();
  835. }
  836. //---------------------------------------------------------------------------
  837. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  838. {
  839. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  840. if (FQueueStatus != NULL)
  841. {
  842. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  843. if (ActiveAndPendingPrimaryCount > 0)
  844. {
  845. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  846. }
  847. }
  848. QueueLabel->Caption = Caption;
  849. }
  850. //---------------------------------------------------------------------------
  851. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  852. {
  853. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  854. static_cast<TComponent*>(GetComponent(fcTransferList)));
  855. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  856. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  857. TransferList->Strings->BeginUpdate();
  858. try
  859. {
  860. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  861. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  862. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  863. if (FTransferDropDownHint.IsEmpty())
  864. {
  865. FTransferDropDownHint = TransferDropDown->Hint;
  866. }
  867. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  868. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  869. TransferDropDown->Text = StripHotkey(Name);
  870. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  871. (FTransferDropDownHint, StripHotkey(Name),
  872. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  873. FLAGMASK(Terminal != NULL, Terminal->UsableCopyParamAttrs(0).General))));
  874. // update the label, otherwise when it is updated only on the first draw
  875. // of the list, it is drawn "bold" for some reason
  876. FTransferListHoverIndex = TransferList->ItemIndex;
  877. UpdateTransferLabel();
  878. }
  879. __finally
  880. {
  881. TransferList->Strings->EndUpdate();
  882. }
  883. }
  884. //---------------------------------------------------------------------------
  885. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  886. {
  887. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  888. DebugAssert(Toolbar != NULL);
  889. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  890. }
  891. //---------------------------------------------------------------------------
  892. void __fastcall TCustomScpExplorerForm::UpdateActions()
  893. {
  894. TForm::UpdateActions();
  895. if (ComponentVisible[fcCustomCommandsBand])
  896. {
  897. UpdateCustomCommandsToolbar();
  898. }
  899. }
  900. //---------------------------------------------------------------------------
  901. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  902. {
  903. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  904. }
  905. //---------------------------------------------------------------------------
  906. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  907. {
  908. DebugAssert(Configuration && RemoteDirView);
  909. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  910. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  911. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  912. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  913. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  914. if (RemoteDirView->RowSelect != WinConfiguration->FullRowSelect)
  915. {
  916. RemoteDirView->RowSelect = WinConfiguration->FullRowSelect;
  917. // selection is not redrawn automatically when RowSelect changes
  918. RemoteDirView->Invalidate();
  919. }
  920. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  921. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  922. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  923. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  924. UpdateSessionsPageControlHeight();
  925. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  926. UpdateToolbarDisplayMode();
  927. UpdateTransferList();
  928. if (Terminal != NULL)
  929. {
  930. TransferPresetAutoSelect();
  931. }
  932. UpdateQueueView();
  933. UpdateControls();
  934. // this can be called even before constuctor finishes.
  935. if (FEditorManager != NULL)
  936. {
  937. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  938. }
  939. // this can be called even before constuctor finishes.
  940. if (FLocalEditors != NULL)
  941. {
  942. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  943. {
  944. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  945. }
  946. }
  947. if (ComponentVisible[fcCustomCommandsBand])
  948. {
  949. UpdateCustomCommandsToolbar();
  950. }
  951. // show only when keeping queue items forever,
  952. // otherwise, its enough to have in in the context menu
  953. QueueDeleteAllDoneQueueToolbarItem->Visible =
  954. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  955. }
  956. //---------------------------------------------------------------------------
  957. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  958. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  959. void * /*Arg*/)
  960. {
  961. if (Token != NULL)
  962. {
  963. TForm * Editor = dynamic_cast<TForm*>(Token);
  964. DebugAssert(Editor != NULL);
  965. ReconfigureEditorForm(Editor);
  966. }
  967. }
  968. //---------------------------------------------------------------------------
  969. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  970. {
  971. WinConfiguration->DDTransferConfirmation = asOn;
  972. }
  973. //---------------------------------------------------------------------------
  974. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  975. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  976. {
  977. // noop
  978. }
  979. //---------------------------------------------------------------------------
  980. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  981. TTransferDirection Direction, TTransferType Type, bool Temp,
  982. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  983. bool Confirm, bool DragDrop, int Options)
  984. {
  985. bool Result = true;
  986. DebugAssert(Terminal && Terminal->Active);
  987. // these parameters are known in advance
  988. int Params =
  989. FLAGMASK(Type == ttMove, cpDelete);
  990. bool ToTemp = (Temp && (Direction == tdToLocal));
  991. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  992. {
  993. bool DisableNewerOnly =
  994. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  995. ToTemp;
  996. Options |=
  997. FLAGMASK(ToTemp, coTemp) |
  998. FLAGMASK(!Terminal->IsCapable[fsBackgroundTransfers], coDisableQueue) |
  999. coDoNotShowAgain;
  1000. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1001. int CopyParamAttrs = (Direction == tdToRemote ?
  1002. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1003. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1004. int OutputOptions =
  1005. FLAGMASK(DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto),
  1006. cooDoNotShowAgain);
  1007. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1008. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1009. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1010. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions);
  1011. if (Result)
  1012. {
  1013. if (FLAGSET(OutputOptions, cooDoNotShowAgain))
  1014. {
  1015. if (DragDrop)
  1016. {
  1017. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1018. {
  1019. PopupTrayBalloon(NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation,
  1020. NULL, 0, EnableDDTransferConfirmation, NULL);
  1021. }
  1022. WinConfiguration->DDTransferConfirmation = asOff;
  1023. }
  1024. else
  1025. {
  1026. WinConfiguration->ConfirmTransferring = false;
  1027. }
  1028. }
  1029. else
  1030. {
  1031. // User exclicitly unchecked "do not show again",
  1032. // so show him the dialog the next time
  1033. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1034. {
  1035. WinConfiguration->DDTransferConfirmation = asOn;
  1036. }
  1037. }
  1038. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1039. }
  1040. }
  1041. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fsBackgroundTransfers])
  1042. {
  1043. Configuration->Usage->Inc(L"TransfersOnBackground");
  1044. // these parameter are known only after transfer dialog
  1045. Params |=
  1046. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1047. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1048. Result = false;
  1049. ClearTransferSourceSelection(Direction);
  1050. }
  1051. return Result;
  1052. }
  1053. //---------------------------------------------------------------------------
  1054. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1055. {
  1056. if (FOnFileOperationFinished != NULL)
  1057. {
  1058. FOnFileOperationFinished(UnicodeString(), true);
  1059. }
  1060. else
  1061. {
  1062. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1063. if (HasDirView[Side])
  1064. {
  1065. DirView(Side)->SelectAll(smNone);
  1066. }
  1067. }
  1068. }
  1069. //---------------------------------------------------------------------------
  1070. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1071. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1072. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1073. int Params)
  1074. {
  1075. DebugAssert(Queue != NULL);
  1076. bool SingleFile = false;
  1077. if (FileList->Count == 1)
  1078. {
  1079. if (Direction == tdToRemote)
  1080. {
  1081. UnicodeString FileName = FileList->Strings[0];
  1082. SingleFile = FileExists(ApiPath(FileName));
  1083. }
  1084. else
  1085. {
  1086. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1087. SingleFile = !File->IsDirectory;
  1088. }
  1089. }
  1090. TQueueItem * QueueItem;
  1091. if (Direction == tdToRemote)
  1092. {
  1093. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1094. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1095. }
  1096. else
  1097. {
  1098. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1099. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1100. }
  1101. AddQueueItem(Queue, QueueItem, Terminal);
  1102. }
  1103. //---------------------------------------------------------------------------
  1104. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TTerminal * Terminal)
  1105. {
  1106. if (Queue->IsEmpty)
  1107. {
  1108. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  1109. ManagedTerminal->QueueOperationStart = Now();
  1110. }
  1111. Queue->AddItem(QueueItem);
  1112. }
  1113. //---------------------------------------------------------------------------
  1114. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1115. {
  1116. }
  1117. //---------------------------------------------------------------------------
  1118. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1119. {
  1120. DebugAssert(FDocks != NULL);
  1121. for (int Index = 0; Index < ComponentCount; Index++)
  1122. {
  1123. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1124. if ((Dock != NULL) && (Dock->Tag == 0))
  1125. {
  1126. FDocks->Add(Dock);
  1127. }
  1128. }
  1129. CollectItemsWithTextDisplayMode(this);
  1130. ConfigurationChanged();
  1131. QueuePanel->Height =
  1132. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1133. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1134. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1135. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1136. }
  1137. //---------------------------------------------------------------------------
  1138. void __fastcall TCustomScpExplorerForm::StoreParams()
  1139. {
  1140. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1141. WinConfiguration->QueueView.HeightPixelsPerInch = GetControlPixelsPerInch(this);
  1142. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1143. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1144. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1145. }
  1146. //---------------------------------------------------------------------------
  1147. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1148. {
  1149. if (!FFormRestored)
  1150. {
  1151. FFormRestored = true;
  1152. RestoreFormParams();
  1153. }
  1154. TForm::CreateParams(Params);
  1155. }
  1156. //---------------------------------------------------------------------------
  1157. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1158. {
  1159. DebugAssert(FDocks != NULL);
  1160. for (int Index = 0; Index < FDocks->Count; Index++)
  1161. {
  1162. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1163. }
  1164. }
  1165. //---------------------------------------------------------------------------
  1166. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1167. {
  1168. SetDockAllowDrag(true);
  1169. while (!ButtonsStr.IsEmpty())
  1170. {
  1171. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1172. UnicodeString Name = CutToChar(S, L'=', true);
  1173. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1174. {
  1175. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1176. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1177. {
  1178. while (!S.IsEmpty())
  1179. {
  1180. UnicodeString S2 = CutToChar(S, L',', true);
  1181. UnicodeString Name = CutToChar(S2, L':', true);
  1182. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1183. {
  1184. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1185. if (SameText(GetToolbarItemName(Item), Name))
  1186. {
  1187. // forward compatibility
  1188. UnicodeString S3 = CutToChar(S2, L':', true);
  1189. bool Visible = StrToIntDef(S3, 0);
  1190. Item->Visible = Visible;
  1191. }
  1192. }
  1193. }
  1194. }
  1195. }
  1196. }
  1197. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1198. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1199. }
  1200. //---------------------------------------------------------------------------
  1201. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1202. {
  1203. UnicodeString Result;
  1204. SetDockAllowDrag(true);
  1205. Result = ::GetToolbarsLayoutStr(this);
  1206. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1207. return Result;
  1208. }
  1209. //---------------------------------------------------------------------------
  1210. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1211. {
  1212. UnicodeString Result;
  1213. if (Item->Action != NULL)
  1214. {
  1215. Result = Item->Action->Name;
  1216. Result = RemoveSuffix(Result, L"Action");
  1217. }
  1218. else
  1219. {
  1220. Result = Item->Name;
  1221. Result = RemoveSuffix(Result, L"SubmenuItem");
  1222. Result = RemoveSuffix(Result, L"Item");
  1223. }
  1224. return Result;
  1225. }
  1226. //---------------------------------------------------------------------------
  1227. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1228. {
  1229. UnicodeString Result;
  1230. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1231. {
  1232. UnicodeString ToolbarStr;
  1233. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1234. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1235. {
  1236. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1237. {
  1238. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1239. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1240. // Once we introduce any buttons that aer hidden by default, we would have to remember their initial state
  1241. // and save the changes here only.
  1242. if (!Item->Visible)
  1243. {
  1244. UnicodeString Name = GetToolbarItemName(Item);
  1245. DebugAssert(Name.Pos(L"TBX") == 0);
  1246. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1247. }
  1248. }
  1249. }
  1250. if (!ToolbarStr.IsEmpty())
  1251. {
  1252. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1253. }
  1254. }
  1255. return Result;
  1256. }
  1257. //---------------------------------------------------------------------------
  1258. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1259. {
  1260. DebugAssert(FProgressForm == NULL);
  1261. FProgressForm =
  1262. new TProgressForm(Application, (FTransferResumeList != NULL), Terminal->IsCapable[fsSkipTransfer], SynchronizeProgress);
  1263. FProgressForm->DeleteLocalToRecycleBin =
  1264. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1265. FProgressForm->DeleteRemoteToRecycleBin =
  1266. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1267. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1268. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1269. LockWindow();
  1270. if (FTaskbarList != NULL)
  1271. {
  1272. // Actually, do not know what hides the progress once the operation finishes
  1273. SetTaskbarListProgressState(TBPF_NORMAL);
  1274. }
  1275. }
  1276. //---------------------------------------------------------------------------
  1277. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1278. {
  1279. UnlockWindow();
  1280. SAFE_DESTROY(FProgressForm);
  1281. SetQueueProgress();
  1282. }
  1283. //---------------------------------------------------------------------------
  1284. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1285. TFileOperationProgressType & ProgressData)
  1286. {
  1287. // Download to temporary local directory
  1288. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1289. {
  1290. bool Continue = true;
  1291. FPendingTempSpaceWarn = false;
  1292. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1293. if (!Continue)
  1294. {
  1295. Abort();
  1296. }
  1297. }
  1298. // operation is being executed and we still didn't show up progress form
  1299. if (ProgressData.InProgress && !FProgressForm)
  1300. {
  1301. CreateProgressForm(NULL);
  1302. // When main window is hidden, synchronisation form does not exist (we do not get here during synchronization - only comparing),
  1303. // we suppose "/upload" or URL download mode
  1304. if (!Visible && (ProgressData.Operation != foCalculateSize) &&
  1305. (ProgressData.Operation != foCalculateChecksum) &&
  1306. (FSynchronizeProgressForm == NULL) &&
  1307. !FStandaloneEditing)
  1308. {
  1309. FProgressForm->OnceDoneOperation = odoDisconnect;
  1310. }
  1311. }
  1312. // operation is finished (or terminated), so we hide progress form
  1313. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1314. {
  1315. DestroyProgressForm();
  1316. if (ProgressData.Operation == foCalculateSize)
  1317. {
  1318. // When calculating size before transfer, the abort caused by
  1319. // cancel flag set due to "MoveToQueue" below,
  1320. // is not propagated back to us in ExecuteFileOperation,
  1321. // as it is silently swallowed.
  1322. // So we explicitly (re)throw it here.
  1323. if (FMoveToQueue)
  1324. {
  1325. Abort();
  1326. }
  1327. }
  1328. else
  1329. {
  1330. if ((ProgressData.Cancel == csContinue) &&
  1331. !FAutoOperation)
  1332. {
  1333. OperationComplete(ProgressData.StartTime);
  1334. }
  1335. }
  1336. }
  1337. if (FProgressForm &&
  1338. // Can happen only during synchronization, when ending one of the operations
  1339. ProgressData.InProgress)
  1340. {
  1341. FProgressForm->SetProgressData(ProgressData);
  1342. if (FTaskbarList != NULL)
  1343. {
  1344. DebugAssert(ProgressData.InProgress);
  1345. SetTaskbarListProgressValue(&ProgressData);
  1346. }
  1347. if (FProgressForm->Cancel > csContinue)
  1348. {
  1349. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1350. if (FProgressForm->Cancel == csCancelFile)
  1351. {
  1352. FProgressForm->ClearCancel();
  1353. }
  1354. // cancel cancels even the move
  1355. FMoveToQueue = false;
  1356. }
  1357. else if (FProgressForm->MoveToQueue)
  1358. {
  1359. FMoveToQueue = true;
  1360. ProgressData.SetCancelAtLeast(csCancel);
  1361. }
  1362. if ((FTransferResumeList != NULL) &&
  1363. ProgressData.InProgress &&
  1364. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1365. !ProgressData.FullFileName.IsEmpty())
  1366. {
  1367. if ((FTransferResumeList->Count == 0) ||
  1368. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1369. {
  1370. // note that we do not recognize directories from files here
  1371. FTransferResumeList->Add(ProgressData.FullFileName);
  1372. }
  1373. }
  1374. }
  1375. }
  1376. //---------------------------------------------------------------------------
  1377. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1378. const TDateTime & StartTime)
  1379. {
  1380. if (GUIConfiguration->BeepOnFinish &&
  1381. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1382. {
  1383. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1384. DWORD Sound;
  1385. if (!ExtractFileExt(BeepSound).IsEmpty())
  1386. {
  1387. Sound = SND_FILENAME;
  1388. }
  1389. else
  1390. {
  1391. Sound = SND_ALIAS;
  1392. }
  1393. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1394. }
  1395. }
  1396. //---------------------------------------------------------------------------
  1397. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1398. TFileOperationProgressType & ProgressData)
  1399. {
  1400. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1401. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1402. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1403. FileOperationProgress(ProgressData);
  1404. }
  1405. //---------------------------------------------------------------------------
  1406. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1407. {
  1408. UnicodeString Result;
  1409. if (FProgressForm != NULL)
  1410. {
  1411. Result = FProgressForm->ProgressStr();
  1412. }
  1413. return Result;
  1414. }
  1415. //---------------------------------------------------------------------------
  1416. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1417. bool DragDrop)
  1418. {
  1419. return (!DragDrop && (DropSourceControl == NULL)) ||
  1420. (DropSourceControl == RemoteDirView);
  1421. }
  1422. //---------------------------------------------------------------------------
  1423. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1424. TFileOperation Operation, TOperationSide Side,
  1425. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1426. TOnceDoneOperation & OnceDoneOperation)
  1427. {
  1428. if (!FAutoOperation)
  1429. {
  1430. // no selection on "/upload", form servers only as event handler
  1431. // (it is not displayed)
  1432. if (PanelOperation(Side, FDragDropOperation) &&
  1433. Visible && (Operation != foCalculateSize) &&
  1434. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1435. {
  1436. if (FOnFileOperationFinished != NULL)
  1437. {
  1438. FOnFileOperationFinished(FileName, Success);
  1439. }
  1440. else
  1441. {
  1442. TCustomDirView * DView = DirView(Side);
  1443. UnicodeString FileNameOnly = ExtractFileName(FileName, (Side == osRemote));
  1444. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1445. // this can happen when local drive is unplugged in the middle of the operation
  1446. if (Item != NULL)
  1447. {
  1448. if (Success) Item->Selected = false;
  1449. DView->MakeProgressVisible(Item);
  1450. }
  1451. }
  1452. }
  1453. if ((Operation == foCopy) || (Operation == foMove))
  1454. {
  1455. if (Side == osLocal)
  1456. {
  1457. Configuration->Usage->Inc(L"UploadedFiles");
  1458. }
  1459. else
  1460. {
  1461. Configuration->Usage->Inc(L"DownloadedFiles");
  1462. }
  1463. }
  1464. }
  1465. if (Success && (FSynchronizeController != NULL))
  1466. {
  1467. if (Operation == foCopy)
  1468. {
  1469. DebugAssert(Side == osLocal);
  1470. FSynchronizeController->LogOperation(soUpload, FileName);
  1471. }
  1472. else if (Operation == foDelete)
  1473. {
  1474. DebugAssert(Side == osRemote);
  1475. FSynchronizeController->LogOperation(soDelete, FileName);
  1476. }
  1477. }
  1478. if (FProgressForm)
  1479. {
  1480. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1481. }
  1482. }
  1483. //---------------------------------------------------------------------------
  1484. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1485. TFileOperation Operation, TOperationSide Side,
  1486. bool Temp, const UnicodeString & FileName, Boolean Success,
  1487. TOnceDoneOperation & OnceDoneOperation)
  1488. {
  1489. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1490. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1491. OnceDoneOperation);
  1492. }
  1493. //---------------------------------------------------------------------------
  1494. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1495. {
  1496. DebugAssert(GetSide(Side) == osRemote);
  1497. DebugUsedParam(Side);
  1498. return RemoteDirView;
  1499. }
  1500. //---------------------------------------------------------------------------
  1501. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1502. {
  1503. DebugAssert(GetSide(Side) == osRemote);
  1504. DebugUsedParam(Side);
  1505. return (Terminal != NULL);
  1506. }
  1507. //---------------------------------------------------------------------------
  1508. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1509. TOperationSide Side, int FilesOnly)
  1510. {
  1511. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1512. }
  1513. //---------------------------------------------------------------------------
  1514. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1515. TOperationSide Side, int FilesOnly)
  1516. {
  1517. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1518. }
  1519. //---------------------------------------------------------------------------
  1520. struct THistoryItemData
  1521. {
  1522. short int Side;
  1523. short int Index;
  1524. };
  1525. //---------------------------------------------------------------------------
  1526. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1527. {
  1528. DirView(Side)->HistoryGo(Index);
  1529. }
  1530. //---------------------------------------------------------------------------
  1531. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1532. {
  1533. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1534. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1535. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1536. }
  1537. //---------------------------------------------------------------------------
  1538. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1539. bool Back)
  1540. {
  1541. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1542. {
  1543. TCustomDirView * DView = DirView(Side);
  1544. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1545. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1546. if (ICount > 10)
  1547. {
  1548. ICount = 10;
  1549. }
  1550. Menu->Items->Clear();
  1551. THistoryItemData Data;
  1552. Data.Side = Side;
  1553. for (short int i = 1; i <= ICount; i++)
  1554. {
  1555. TTBCustomItem * Item = new TTBXItem(Menu);
  1556. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1557. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, (Side == osRemote));
  1558. Item->Hint = DView->HistoryPath[Data.Index];
  1559. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1560. Item->Tag = *reinterpret_cast<int*>(&Data);
  1561. Item->OnClick = HistoryItemClick;
  1562. Menu->Items->Add(Item);
  1563. }
  1564. }
  1565. }
  1566. //---------------------------------------------------------------------------
  1567. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1568. TOperationSide Side, bool Back)
  1569. {
  1570. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1571. {
  1572. // workaround
  1573. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1574. // operator new allocates memory in Pascal code, but calls inline
  1575. // contructor in C++, leading in problems, the function does
  1576. // both in Pascal code
  1577. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1578. UpdateHistoryMenu(Side, Back);
  1579. }
  1580. return FHistoryMenu[Side == osLocal][Back];
  1581. }
  1582. //---------------------------------------------------------------------------
  1583. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1584. TCustomDirView *Sender)
  1585. {
  1586. TOperationSide Side = (Sender == DirView(osRemote) ? osRemote : osLocal);
  1587. UpdateHistoryMenu(Side, true);
  1588. UpdateHistoryMenu(Side, false);
  1589. }
  1590. //---------------------------------------------------------------------------
  1591. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1592. {
  1593. // remote custom commands can be executed only if the server supports shell commands
  1594. // or have secondary shell
  1595. return (FTerminal != NULL) && (FTerminal->IsCapable[fcSecondaryShell] || FTerminal->IsCapable[fcShellAnyCommand]);
  1596. }
  1597. //---------------------------------------------------------------------------
  1598. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1599. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1600. {
  1601. int Result;
  1602. TFileCustomCommand RemoteCustomCommand;
  1603. TLocalCustomCommand LocalCustomCommand;
  1604. TFileCustomCommand * NonInteractiveCustomCommand =
  1605. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1606. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1607. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1608. if (FLAGCLEAR(Command.Params, ccLocal))
  1609. {
  1610. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1611. // custom command that does not operate with files can be executed anytime ...
  1612. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1613. {
  1614. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1615. {
  1616. // ... but do not show such command in remote file menu (TODO)
  1617. Result = AllowedState;
  1618. }
  1619. else
  1620. {
  1621. Result = -1;
  1622. }
  1623. }
  1624. else
  1625. {
  1626. if ((ListType == ccltAll) || (ListType == ccltFile))
  1627. {
  1628. Result = ((FCurrentSide == osRemote) && DirView(osRemote)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1629. }
  1630. else
  1631. {
  1632. Result = -1;
  1633. }
  1634. }
  1635. }
  1636. else
  1637. {
  1638. // custom command that does not operate with files can be executed anytime
  1639. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1640. {
  1641. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1642. }
  1643. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1644. {
  1645. if ((ListType == ccltAll) || (ListType == ccltFile))
  1646. {
  1647. // special case is "diff"-style command that can be executed over any side,
  1648. // if we have both sides
  1649. Result =
  1650. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1651. // directly (instead of EnableSelectedOperation) to pass
  1652. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1653. ((HasDirView[osLocal] && DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1654. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1655. }
  1656. else if (ListType == ccltBoth)
  1657. {
  1658. Result = 1;
  1659. }
  1660. else
  1661. {
  1662. Result = -1;
  1663. }
  1664. }
  1665. else
  1666. {
  1667. if ((ListType == ccltAll) || (ListType == ccltFile))
  1668. {
  1669. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1670. }
  1671. else
  1672. {
  1673. Result = -1;
  1674. }
  1675. }
  1676. }
  1677. return Result;
  1678. }
  1679. //---------------------------------------------------------------------------
  1680. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1681. TStrings * FileList, const TCustomCommandType & ACommand,
  1682. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1683. {
  1684. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1685. {
  1686. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1687. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1688. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1689. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1690. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1691. bool Capture =
  1692. FLAGSET(ACommand.Params, ccShowResults) ||
  1693. FLAGSET(ACommand.Params, ccCopyResults) ||
  1694. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1695. TCaptureOutputEvent OutputEvent = NULL;
  1696. DebugAssert(FCapturedLog == NULL);
  1697. if (Capture)
  1698. {
  1699. FCapturedLog = new TStringList();
  1700. OutputEvent = TerminalCaptureLog;
  1701. }
  1702. try
  1703. {
  1704. if (!RemoteCustomCommand.IsFileCommand(Command))
  1705. {
  1706. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1707. OutputEvent);
  1708. }
  1709. else
  1710. {
  1711. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1712. }
  1713. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1714. {
  1715. if (FLAGSET(ACommand.Params, ccCopyResults))
  1716. {
  1717. CopyToClipboard(FCapturedLog);
  1718. }
  1719. if (FLAGSET(ACommand.Params, ccShowResults))
  1720. {
  1721. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1722. }
  1723. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1724. {
  1725. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1726. }
  1727. }
  1728. }
  1729. __finally
  1730. {
  1731. SAFE_DESTROY(FCapturedLog);
  1732. }
  1733. }
  1734. }
  1735. //---------------------------------------------------------------------------
  1736. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1737. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1738. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1739. {
  1740. TStrings * LocalFileList = NULL;
  1741. TStrings * RemoteFileList = NULL;
  1742. TStrings * RemoteFileListFull = NULL;
  1743. try
  1744. {
  1745. if (LocalFileCommand)
  1746. {
  1747. if (ALocalFileList == NULL)
  1748. {
  1749. DebugAssert(HasDirView[osLocal]);
  1750. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1751. // directly (instead of EnableSelectedOperation) to pass
  1752. // false to FocusedFileOnlyWhenFocused
  1753. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1754. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1755. }
  1756. else
  1757. {
  1758. LocalFileList = ALocalFileList;
  1759. }
  1760. if (FileListCommand)
  1761. {
  1762. if (LocalFileList->Count != 1)
  1763. {
  1764. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1765. }
  1766. }
  1767. else
  1768. {
  1769. if ((LocalFileList->Count != 1) &&
  1770. (FileList->Count != 1) &&
  1771. (LocalFileList->Count != FileList->Count))
  1772. {
  1773. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1774. }
  1775. }
  1776. }
  1777. UnicodeString RootTempDir;
  1778. UnicodeString TempDir;
  1779. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1780. if (!RemoteFiles)
  1781. {
  1782. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1783. }
  1784. try
  1785. {
  1786. TDateTimes RemoteFileTimes;
  1787. if (RemoteFiles)
  1788. {
  1789. RemoteFileList = FileList;
  1790. }
  1791. else
  1792. {
  1793. RemoteFileList = new TStringList();
  1794. TMakeLocalFileListParams MakeFileListParam;
  1795. MakeFileListParam.FileList = RemoteFileList;
  1796. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1797. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1798. MakeFileListParam.Recursive =
  1799. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1800. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1801. if (!MakeFileListParam.Recursive)
  1802. {
  1803. RemoteFileTimes.clear();
  1804. RemoteFileListFull = new TStringList();
  1805. MakeFileListParam.FileList = RemoteFileListFull;
  1806. MakeFileListParam.Recursive = true;
  1807. MakeFileListParam.IncludeDirs = false;
  1808. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1809. }
  1810. else
  1811. {
  1812. RemoteFileListFull = RemoteFileList;
  1813. }
  1814. }
  1815. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1816. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1817. if (!NonBlocking)
  1818. {
  1819. Progress.Start(foCustomCommand, osRemote, FileListCommand ? 1 : FileList->Count);
  1820. DebugAssert(FProgressForm != NULL);
  1821. FProgressForm->ReadOnly = true;
  1822. }
  1823. try
  1824. {
  1825. if (FileListCommand)
  1826. {
  1827. UnicodeString LocalFile;
  1828. // MakeFileList does not delimit filenames
  1829. UnicodeString FileList = MakeFileList(RemoteFileList);
  1830. if (LocalFileCommand)
  1831. {
  1832. DebugAssert(LocalFileList->Count == 1);
  1833. LocalFile = LocalFileList->Strings[0];
  1834. }
  1835. TLocalCustomCommand CustomCommand(Data,
  1836. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1837. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1838. if (NonBlocking)
  1839. {
  1840. DebugAssert(!POutput);
  1841. ExecuteShellChecked(ShellCommand);
  1842. }
  1843. else
  1844. {
  1845. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1846. }
  1847. }
  1848. else if (LocalFileCommand)
  1849. {
  1850. if (LocalFileList->Count == 1)
  1851. {
  1852. UnicodeString LocalFile = LocalFileList->Strings[0];
  1853. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1854. {
  1855. UnicodeString FileName = RemoteFileList->Strings[Index];
  1856. TLocalCustomCommand CustomCommand(Data,
  1857. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  1858. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1859. }
  1860. }
  1861. else if (RemoteFileList->Count == 1)
  1862. {
  1863. UnicodeString FileName = RemoteFileList->Strings[0];
  1864. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1865. {
  1866. TLocalCustomCommand CustomCommand(
  1867. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1868. FileName, LocalFileList->Strings[Index], L"");
  1869. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1870. }
  1871. }
  1872. else
  1873. {
  1874. if (LocalFileList->Count != RemoteFileList->Count)
  1875. {
  1876. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  1877. }
  1878. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1879. {
  1880. UnicodeString FileName = RemoteFileList->Strings[Index];
  1881. TLocalCustomCommand CustomCommand(
  1882. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1883. FileName, LocalFileList->Strings[Index], L"");
  1884. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1885. }
  1886. }
  1887. }
  1888. else
  1889. {
  1890. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1891. {
  1892. TLocalCustomCommand CustomCommand(Data,
  1893. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1894. RemoteFileList->Strings[Index], L"", L"");
  1895. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1896. }
  1897. }
  1898. }
  1899. __finally
  1900. {
  1901. if (!NonBlocking)
  1902. {
  1903. Progress.Stop();
  1904. }
  1905. }
  1906. DebugAssert(!FAutoOperation);
  1907. if (!RemoteFiles)
  1908. {
  1909. TempDir = IncludeTrailingBackslash(TempDir);
  1910. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  1911. {
  1912. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  1913. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  1914. // Skip directories, process only nested files.
  1915. // The check is redundant as FileAge fails for directories anyway.
  1916. !DirectoryExists(FileName))
  1917. {
  1918. UnicodeString RemoteDir =
  1919. UnixExtractFileDir(
  1920. UnixIncludeTrailingBackslash(FTerminal->CurrentDirectory) +
  1921. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  1922. TDateTime NewTime;
  1923. if (FileAge(FileName, NewTime) &&
  1924. (NewTime != RemoteFileTimes[Index]))
  1925. {
  1926. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  1927. TemporaryFileCopyParam(CopyParam);
  1928. CopyParam.FileMask = L"";
  1929. FAutoOperation = true;
  1930. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  1931. TemporaryFilesList->Add(FileName);
  1932. FTerminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  1933. }
  1934. }
  1935. }
  1936. }
  1937. }
  1938. __finally
  1939. {
  1940. FAutoOperation = false;
  1941. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  1942. {
  1943. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  1944. }
  1945. }
  1946. }
  1947. __finally
  1948. {
  1949. if (RemoteFileList != FileList)
  1950. {
  1951. delete RemoteFileList;
  1952. }
  1953. if (LocalFileList != ALocalFileList)
  1954. {
  1955. delete LocalFileList;
  1956. }
  1957. if (RemoteFileListFull != RemoteFileList)
  1958. {
  1959. delete RemoteFileListFull;
  1960. }
  1961. }
  1962. }
  1963. //---------------------------------------------------------------------------
  1964. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithRemoteFiles(
  1965. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  1966. bool FileListCommand, UnicodeString * POutput)
  1967. {
  1968. std::unique_ptr<TStrings> SelectedFileList(DirView(osLocal)->CreateFileList(false, true, NULL));
  1969. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  1970. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  1971. {
  1972. UnicodeString FileName = SelectedFileList->Strings[Index];
  1973. if (DirectoryExists(FileName))
  1974. {
  1975. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  1976. {
  1977. LocalFileList->Add(FileName);
  1978. }
  1979. if (FLAGSET(ACommand.Params, ccRecursive))
  1980. {
  1981. TMakeLocalFileListParams MakeFileListParam;
  1982. MakeFileListParam.FileList = LocalFileList.get();
  1983. MakeFileListParam.FileTimes = NULL;
  1984. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1985. MakeFileListParam.Recursive = true;
  1986. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  1987. }
  1988. }
  1989. else
  1990. {
  1991. LocalFileList->Add(FileName);
  1992. }
  1993. }
  1994. if (FileListCommand)
  1995. {
  1996. UnicodeString FileList = MakeFileList(LocalFileList.get());
  1997. TLocalCustomCommand CustomCommand(
  1998. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1999. L"", L"", FileList);
  2000. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2001. }
  2002. else
  2003. {
  2004. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2005. Progress.Start(foCustomCommand, osRemote, FileListCommand ? 1 : LocalFileList->Count);
  2006. DebugAssert(FProgressForm != NULL);
  2007. FProgressForm->ReadOnly = true;
  2008. try
  2009. {
  2010. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2011. {
  2012. UnicodeString FileName = LocalFileList->Strings[Index];
  2013. TLocalCustomCommand CustomCommand(
  2014. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2015. FileName, L"", L"");
  2016. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2017. }
  2018. }
  2019. __finally
  2020. {
  2021. Progress.Stop();
  2022. }
  2023. }
  2024. }
  2025. //---------------------------------------------------------------------------
  2026. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2027. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2028. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2029. {
  2030. TLocalCustomCommand LocalCustomCommand(Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory());
  2031. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2032. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2033. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2034. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2035. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2036. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2037. std::unique_ptr<UnicodeString> POutput;
  2038. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2039. FLAGSET(ACommand.Params, ccCopyResults))
  2040. {
  2041. POutput.reset(new UnicodeString());
  2042. }
  2043. if (!LocalCustomCommand.IsFileCommand(Command))
  2044. {
  2045. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2046. }
  2047. // remote files?
  2048. else if ((FCurrentSide == osRemote) || LocalFileCommand)
  2049. {
  2050. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2051. }
  2052. // local files
  2053. else
  2054. {
  2055. LocalCustomCommandWithRemoteFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2056. }
  2057. if (POutput.get() != NULL)
  2058. {
  2059. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2060. int P = POutput->Pos(sLineBreak);
  2061. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2062. {
  2063. POutput->SetLength(P - 1);
  2064. }
  2065. // Copy even empty output to clipboard
  2066. if (FLAGSET(ACommand.Params, ccCopyResults))
  2067. {
  2068. CopyToClipboard(*POutput);
  2069. }
  2070. // But do not show an empty message box.
  2071. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2072. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2073. !POutput->IsEmpty())
  2074. {
  2075. TClipboardHandler ClipboardHandler;
  2076. ClipboardHandler.Text = *POutput;
  2077. TMessageParams Params;
  2078. TQueryButtonAlias Aliases[1];
  2079. Aliases[0].Button = qaRetry;
  2080. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2081. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2082. Params.Aliases = Aliases;
  2083. Params.AliasesCount = LENOF(Aliases);
  2084. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2085. }
  2086. }
  2087. }
  2088. //---------------------------------------------------------------------------
  2089. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2090. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2091. {
  2092. TCustomCommandData Data(Terminal);
  2093. UnicodeString Site = Terminal->SessionData->SessionKey;
  2094. UnicodeString HelpKeyword = ACommand.HomePage;
  2095. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2096. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2097. {
  2098. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2099. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2100. {
  2101. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, Terminal->CurrentDirectory));
  2102. }
  2103. else
  2104. {
  2105. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory()));
  2106. }
  2107. if (!DoCustomCommandOptionsDialog(
  2108. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2109. {
  2110. Abort();
  2111. }
  2112. }
  2113. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2114. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2115. {
  2116. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2117. }
  2118. else
  2119. {
  2120. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2121. }
  2122. }
  2123. //---------------------------------------------------------------------------
  2124. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2125. const TCustomCommandType & Command)
  2126. {
  2127. DebugAssert(FCustomCommandLocalFileList != NULL);
  2128. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2129. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2130. TStrings * LocalFileList = new TStringList();
  2131. TStrings * RemoteFileList = new TStringList();
  2132. try
  2133. {
  2134. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2135. {
  2136. LocalFileList->Clear();
  2137. LocalFileList->AddObject(
  2138. FCustomCommandLocalFileList->Strings[Index],
  2139. FCustomCommandLocalFileList->Objects[Index]);
  2140. RemoteFileList->Clear();
  2141. RemoteFileList->AddObject(
  2142. FCustomCommandRemoteFileList->Strings[Index],
  2143. FCustomCommandRemoteFileList->Objects[Index]);
  2144. CustomCommand(RemoteFileList, Command, LocalFileList);
  2145. }
  2146. }
  2147. __finally
  2148. {
  2149. delete LocalFileList;
  2150. delete RemoteFileList;
  2151. }
  2152. }
  2153. //---------------------------------------------------------------------------
  2154. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2155. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2156. {
  2157. delete FCustomCommandLocalFileList;
  2158. delete FCustomCommandRemoteFileList;
  2159. // takeover ownership,
  2160. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2161. FCustomCommandLocalFileList = LocalFileList;
  2162. FCustomCommandRemoteFileList = RemoteFileList;
  2163. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2164. if (Button != NULL)
  2165. {
  2166. FCustomCommandMenu->Items->Clear();
  2167. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth);
  2168. MenuPopup(FCustomCommandMenu, Button);
  2169. }
  2170. else
  2171. {
  2172. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2173. }
  2174. }
  2175. //---------------------------------------------------------------------------
  2176. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2177. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2178. {
  2179. DebugAssert(FCapturedLog != NULL);
  2180. if ((OutputType == cotOutput) || (OutputType == cotError))
  2181. {
  2182. FCapturedLog->Add(AddedLine);
  2183. }
  2184. }
  2185. //---------------------------------------------------------------------------
  2186. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2187. TOperationSide Side)
  2188. {
  2189. return (Side == osRemote) &&
  2190. ((Control == RemoteDirView) || (Control == RemoteDriveView));
  2191. }
  2192. //---------------------------------------------------------------------------
  2193. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2194. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2195. {
  2196. TTBItemOptions O;
  2197. O = Item->Options;
  2198. TCustomDirView * DView = DirView(Side);
  2199. if ((DView->ItemFocused != NULL) &&
  2200. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2201. // when resolving links is disabled, default action is to enter the directory,
  2202. // no matter what DoubleClickAction is configured to
  2203. ((Side != osRemote) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2204. // Can only Edit files, but can Open/Copy even directories
  2205. ((DoubleClickAction != dcaEdit) ||
  2206. !DView->ItemIsDirectory(DView->ItemFocused)))
  2207. {
  2208. Item->Options = O << tboDefault;
  2209. }
  2210. else
  2211. {
  2212. Item->Options = O >> tboDefault;
  2213. }
  2214. }
  2215. //---------------------------------------------------------------------------
  2216. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2217. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2218. {
  2219. TCustomDirView * DView = DirView(Side);
  2220. TListItem * Item = DView->ItemFocused;
  2221. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2222. EnableFocusedOperation[Side])
  2223. {
  2224. TPoint ClientPoint;
  2225. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2226. TPoint(0, 0) : MousePos;
  2227. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2228. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2229. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2230. }
  2231. }
  2232. //---------------------------------------------------------------------------
  2233. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2234. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2235. {
  2236. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2237. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2238. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2239. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2240. Handled = true;
  2241. }
  2242. //---------------------------------------------------------------------------
  2243. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2244. {
  2245. }
  2246. //---------------------------------------------------------------------------
  2247. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2248. {
  2249. DebugAssert(FErrorList == NULL);
  2250. if (WinConfiguration->ContinueOnError)
  2251. {
  2252. FErrorList = new TStringList();
  2253. Configuration->Usage->Inc(L"ContinuationsOnError");
  2254. }
  2255. NonVisualDataModule->StartBusy();
  2256. }
  2257. //---------------------------------------------------------------------------
  2258. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2259. {
  2260. NonVisualDataModule->EndBusy();
  2261. if (FErrorList)
  2262. {
  2263. HandleErrorList(FErrorList);
  2264. }
  2265. }
  2266. //---------------------------------------------------------------------------
  2267. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2268. const TCopyParamType & CopyParam)
  2269. {
  2270. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2271. {
  2272. Configuration->Usage->Inc(L"FileMaskUses");
  2273. }
  2274. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2275. {
  2276. Configuration->Usage->Inc(L"OperationMaskUses");
  2277. }
  2278. }
  2279. //---------------------------------------------------------------------------
  2280. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2281. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2282. {
  2283. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2284. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2285. UnicodeString TargetDirectory;
  2286. bool Temp = false;
  2287. bool DragDrop = false;
  2288. int Options = 0;
  2289. TAutoSwitch UseQueue = asAuto;
  2290. if (Param != NULL)
  2291. {
  2292. TTransferOperationParam& TParam =
  2293. *static_cast<TTransferOperationParam*>(Param);
  2294. TargetDirectory = TParam.TargetDirectory;
  2295. Temp = TParam.Temp;
  2296. DragDrop = TParam.DragDrop;
  2297. Options = TParam.Options;
  2298. UseQueue = TParam.Queue;
  2299. }
  2300. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2301. switch (UseQueue)
  2302. {
  2303. case asOn:
  2304. CopyParam.Queue = true;
  2305. break;
  2306. case asOff:
  2307. CopyParam.Queue = false;
  2308. break;
  2309. case asAuto:
  2310. default:
  2311. // keep default
  2312. break;
  2313. }
  2314. bool Result =
  2315. CopyParamDialog(Direction, Type, Temp, FileList, TargetDirectory, CopyParam, !NoConfirmation, DragDrop, Options);
  2316. if (Result)
  2317. {
  2318. DebugAssert(Terminal);
  2319. bool SelectionRestored = false;
  2320. TCustomDirView * DView = NULL;
  2321. if (HasDirView[Side])
  2322. {
  2323. DView = DirView(Side);
  2324. DView->SaveSelection();
  2325. DView->SaveSelectedNames();
  2326. }
  2327. UpdateCopyParamCounters(CopyParam);
  2328. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2329. DebugAssert(FTransferResumeList == NULL);
  2330. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2331. FMoveToQueue = false;
  2332. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2333. try
  2334. {
  2335. TStrings * PermanentFileList;
  2336. std::unique_ptr<TStrings> PermanentFileListOwner;
  2337. try
  2338. {
  2339. if (Side == osLocal)
  2340. {
  2341. PermanentFileList = FileList;
  2342. Params |= FLAGMASK(Temp, cpTemporary);
  2343. Terminal->CopyToRemote(FileList, TargetDirectory, &CopyParam, Params, NULL);
  2344. if (Operation == foMove)
  2345. {
  2346. ReloadLocalDirectory();
  2347. if (DView != NULL)
  2348. {
  2349. DView->RestoreSelection();
  2350. }
  2351. SelectionRestored = true;
  2352. }
  2353. }
  2354. else
  2355. {
  2356. // Clone the file list as it may refer to current directory files,
  2357. // which get destroyed, when the source directory is reloaded after foMove operation.
  2358. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2359. // But for a hotfix, we are not going to do such a big change.
  2360. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2361. PermanentFileList = PermanentFileListOwner.get();
  2362. try
  2363. {
  2364. Terminal->CopyToLocal(FileList, TargetDirectory, &CopyParam, Params, NULL);
  2365. }
  2366. __finally
  2367. {
  2368. if (Operation == foMove)
  2369. {
  2370. if (DView != NULL)
  2371. {
  2372. DView->RestoreSelection();
  2373. }
  2374. SelectionRestored = true;
  2375. }
  2376. ReloadLocalDirectory(TargetDirectory);
  2377. }
  2378. }
  2379. }
  2380. catch (EAbort &)
  2381. {
  2382. if (FMoveToQueue)
  2383. {
  2384. Params |= (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. return Result;
  2413. }
  2414. //---------------------------------------------------------------------------
  2415. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2416. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2417. {
  2418. DebugAssert(FileList->Count);
  2419. // We deliberately do not toggle alternative flag (Param), but use OR,
  2420. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2421. // shortcut of CurrentDeleteAlternativeAction
  2422. bool Alternative =
  2423. bool(Param) || UseAlternativeFunction();
  2424. bool Recycle;
  2425. if (Side == osLocal)
  2426. {
  2427. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2428. }
  2429. else
  2430. {
  2431. Recycle =
  2432. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2433. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2434. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2435. }
  2436. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2437. if (!Result)
  2438. {
  2439. UnicodeString Query;
  2440. if (FileList->Count == 1)
  2441. {
  2442. if (Side == osLocal)
  2443. {
  2444. Query = ExtractFileName(FileList->Strings[0]);
  2445. }
  2446. else
  2447. {
  2448. Query = UnixExtractFileName(FileList->Strings[0]);
  2449. }
  2450. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2451. }
  2452. else
  2453. {
  2454. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2455. }
  2456. TMessageParams Params(mpNeverAskAgainCheck);
  2457. Params.ImageName = L"Delete file";
  2458. unsigned int Answer =
  2459. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2460. if (Answer == qaNeverAskAgain)
  2461. {
  2462. Result = true;
  2463. if (Recycle)
  2464. {
  2465. WinConfiguration->ConfirmRecycling = false;
  2466. }
  2467. else
  2468. {
  2469. WinConfiguration->ConfirmDeleting = false;
  2470. }
  2471. }
  2472. else
  2473. {
  2474. Result = (Answer == qaOK);
  2475. }
  2476. }
  2477. if (Result)
  2478. {
  2479. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2480. }
  2481. return Result;
  2482. }
  2483. //---------------------------------------------------------------------------
  2484. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2485. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2486. {
  2487. TAutoBatch AutoBatch(this);
  2488. bool Result;
  2489. if ((Operation == foCopy) || (Operation == foMove))
  2490. {
  2491. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2492. }
  2493. else if (Operation == foRename)
  2494. {
  2495. DebugAssert(DirView(Side)->ItemFocused);
  2496. DirView(Side)->ItemFocused->EditCaption();
  2497. Result = true;
  2498. }
  2499. else if (Operation == foDelete)
  2500. {
  2501. Result = ExecuteDeleteFileOperation(Side, FileList, NoConfirmation, Param);
  2502. }
  2503. else if (Operation == foSetProperties)
  2504. {
  2505. RemoteDirView->SaveSelectedNames();
  2506. Result = SetProperties(Side, FileList);
  2507. }
  2508. else if (Operation == foCustomCommand)
  2509. {
  2510. DebugAssert(Param);
  2511. DebugAssert(Side == osRemote);
  2512. RemoteDirView->SaveSelectedNames();
  2513. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2514. CustomCommand(FileList, *Command, NULL);
  2515. Result = true;
  2516. }
  2517. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2518. {
  2519. DebugAssert(Side == osRemote);
  2520. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2521. (Operation == foRemoteMove), reinterpret_cast<TTerminal *>(Param));
  2522. }
  2523. else if (Operation == foLock)
  2524. {
  2525. DebugAssert(Side == osRemote);
  2526. LockFiles(FileList, true);
  2527. Result = true;
  2528. }
  2529. else if (Operation == foUnlock)
  2530. {
  2531. DebugAssert(Side == osRemote);
  2532. LockFiles(FileList, false);
  2533. Result = true;
  2534. }
  2535. else
  2536. {
  2537. DebugFail();
  2538. }
  2539. return Result;
  2540. }
  2541. //---------------------------------------------------------------------------
  2542. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2543. {
  2544. if (Side == osCurrent)
  2545. {
  2546. Side = FCurrentSide;
  2547. }
  2548. return Side;
  2549. }
  2550. //---------------------------------------------------------------------------
  2551. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2552. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2553. {
  2554. Side = GetSide(Side);
  2555. bool Result;
  2556. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, (Side == osLocal), NULL);
  2557. try
  2558. {
  2559. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2560. }
  2561. __finally
  2562. {
  2563. delete FileList;
  2564. }
  2565. return Result;
  2566. }
  2567. //---------------------------------------------------------------------------
  2568. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2569. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2570. bool NoConfirmation, void * Param)
  2571. {
  2572. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2573. {
  2574. if ((Operation == foCopy) || (Operation == foMove))
  2575. {
  2576. if (GetSide(Side) == osLocal)
  2577. {
  2578. Configuration->Usage->Inc(L"UploadsCommand");
  2579. }
  2580. else
  2581. {
  2582. Configuration->Usage->Inc(L"DownloadsCommand");
  2583. }
  2584. }
  2585. }
  2586. }
  2587. //---------------------------------------------------------------------------
  2588. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2589. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2590. {
  2591. TTransferOperationParam Param;
  2592. if ((WinConfiguration->Interface != ifCommander) ||
  2593. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2594. {
  2595. Flags &= ~cocShortCutHint;
  2596. }
  2597. TCustomDirView * DView = DirView(Side);
  2598. Param.Options =
  2599. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2600. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2601. if (FLAGSET(Flags, cocQueue))
  2602. {
  2603. Param.Queue = asOn;
  2604. }
  2605. else if (FLAGSET(Flags, cocNonQueue))
  2606. {
  2607. Param.Queue = asOff;
  2608. }
  2609. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2610. }
  2611. //---------------------------------------------------------------------------
  2612. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2613. {
  2614. try
  2615. {
  2616. if (ErrorList->Count)
  2617. {
  2618. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2619. if (MessageDialog(Message, qtError,
  2620. qaOK | qaCancel, HELP_NONE) == qaOK)
  2621. {
  2622. unsigned int Answer;
  2623. int Index = 0;
  2624. do
  2625. {
  2626. DebugAssert(Index >= 0 && Index < ErrorList->Count);
  2627. TQueryButtonAlias Aliases[2];
  2628. Aliases[0].Button = qaYes;
  2629. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2630. Aliases[1].Button = qaNo;
  2631. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2632. TMessageParams Params;
  2633. Params.Aliases = Aliases;
  2634. Params.AliasesCount = LENOF(Aliases);
  2635. Answer = MoreMessageDialog(
  2636. FMTLOAD(ERROR_LIST_NUMBER, (Index+1, ErrorList->Count, ErrorList->Strings[Index])),
  2637. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2638. (Index ? qaYes : 0) | (Index < ErrorList->Count - 1 ? qaNo : 0) |
  2639. qaOK, HELP_NONE, &Params);
  2640. if (Answer == qaNo)
  2641. {
  2642. Index++;
  2643. }
  2644. if (Answer == qaYes)
  2645. {
  2646. Index--;
  2647. }
  2648. }
  2649. while (Answer != qaOK);
  2650. }
  2651. }
  2652. }
  2653. __finally
  2654. {
  2655. TStrings * List = ErrorList;
  2656. ErrorList = NULL;
  2657. for (int i = 0; i < List->Count; i++)
  2658. {
  2659. delete List->Objects[i];
  2660. }
  2661. delete List;
  2662. }
  2663. }
  2664. //---------------------------------------------------------------------------
  2665. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2666. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2667. {
  2668. // needed for checking filemasks, as there's no directory object
  2669. // associated with the file object
  2670. File->FullFileName = FullFileName;
  2671. TFileMasks::TParams MaskParams;
  2672. MaskParams.Size = File->Size;
  2673. MaskParams.Modification = File->Modification;
  2674. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2675. }
  2676. //---------------------------------------------------------------------------
  2677. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2678. {
  2679. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2680. Side = GetSide(Side);
  2681. TCustomDirView * DView = DirView(Side);
  2682. TListItem * FocusedItem = DView->ItemFocused;
  2683. UnicodeString Name;
  2684. if ((FocusedItem != NULL) && !DView->ItemIsParentDirectory(FocusedItem))
  2685. {
  2686. Name = DView->ItemFileName(FocusedItem);
  2687. }
  2688. else
  2689. {
  2690. Name = LoadStr(NEW_FILE);
  2691. }
  2692. UnicodeString Names = Name;
  2693. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2694. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2695. HELP_EDIT_NEW, History.get(), true))
  2696. {
  2697. while (!Names.IsEmpty())
  2698. {
  2699. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2700. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2701. UnicodeString TargetFileName;
  2702. UnicodeString LocalFileName;
  2703. UnicodeString RootTempDir;
  2704. UnicodeString TempDir;
  2705. UnicodeString RemoteDirectory;
  2706. bool ExistingFile = false;
  2707. if (Side == osRemote)
  2708. {
  2709. Name = AbsolutePath(FTerminal->CurrentDirectory, Name);
  2710. TRemoteFile * File = NULL;
  2711. if (FTerminal->FileExists(Name, &File))
  2712. {
  2713. try
  2714. {
  2715. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2716. ExistingFile = true;
  2717. }
  2718. __finally
  2719. {
  2720. delete File;
  2721. }
  2722. }
  2723. if (!ExistingFile)
  2724. {
  2725. RemoteDirectory = UnixExtractFilePath(Name);
  2726. TemporaryDirectoryForRemoteFiles(
  2727. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2728. TargetFileName = UnixExtractFileName(Name);
  2729. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2730. LocalFileName = TempDir +
  2731. // We probably do not want to trim the VMS version here
  2732. FTerminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2733. }
  2734. }
  2735. else
  2736. {
  2737. if (ExtractFilePath(Name).IsEmpty())
  2738. {
  2739. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2740. }
  2741. else
  2742. {
  2743. LocalFileName = ExpandFileName(Name);
  2744. }
  2745. TargetFileName = ExtractFileName(Name);
  2746. }
  2747. if (!ExistingFile)
  2748. {
  2749. if (!FileExists(ApiPath(LocalFileName)))
  2750. {
  2751. int File = FileCreate(ApiPath(LocalFileName));
  2752. if (File < 0)
  2753. {
  2754. if (!RootTempDir.IsEmpty())
  2755. {
  2756. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2757. }
  2758. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2759. }
  2760. else
  2761. {
  2762. FileClose(File);
  2763. }
  2764. }
  2765. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2766. const TEditorData * ExternalEditor = NULL;
  2767. TFileMasks::TParams MaskParams; // size not known
  2768. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2769. false, MaskParams);
  2770. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2771. ExternalEditor, RootTempDir, RemoteDirectory);
  2772. }
  2773. }
  2774. }
  2775. }
  2776. //---------------------------------------------------------------------------
  2777. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2778. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2779. {
  2780. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2781. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2782. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2783. return
  2784. ((ExecuteFileBy == efInternalEditor)) ||
  2785. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2786. }
  2787. //---------------------------------------------------------------------------
  2788. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2789. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2790. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2791. UnicodeString RemoteDirectory)
  2792. {
  2793. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2794. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2795. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2796. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2797. Side = GetSide(Side);
  2798. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2799. if (Side == osRemote)
  2800. {
  2801. Data->Terminal = Terminal;
  2802. Data->Queue = Queue;
  2803. Data->SessionData = CloneCurrentSessionData();
  2804. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2805. Data->RemoteDirectory = RemoteDirectory;
  2806. Data->SessionName = Terminal->SessionData->SessionName;
  2807. Data->LocalRootDirectory = LocalRootDirectory;
  2808. Data->OriginalFileName = OriginalFileName;
  2809. Data->Command = L""; // will be changed later for external editor
  2810. }
  2811. if (ExecuteFileBy == efInternalEditor)
  2812. {
  2813. if (Side == osRemote)
  2814. {
  2815. UnicodeString Caption = UnixIncludeTrailingBackslash(RemoteDirectory) + OriginalFileName +
  2816. L" - " + Terminal->SessionData->SessionName;
  2817. TForm * Editor;
  2818. try
  2819. {
  2820. TNotifyEvent OnFileChanged = NULL;
  2821. TNotifyEvent OnSaveAll = NULL;
  2822. TAnyModifiedEvent OnAnyModified = NULL;
  2823. // Edited files are uploaded in background queue, what we do not support with encrypted files
  2824. if (Terminal->IsCapable[fsBackgroundTransfers])
  2825. {
  2826. OnFileChanged = FEditorManager->FileChanged;
  2827. OnSaveAll = SaveAllInternalEditors;
  2828. OnAnyModified = AnyInternalEditorModified;
  2829. }
  2830. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  2831. FEditorManager->FileReload, FEditorManager->FileClosed,
  2832. OnSaveAll, OnAnyModified,
  2833. Caption, FStandaloneEditing, SessionColor, Terminal->SessionData->InternalEditorEncoding);
  2834. }
  2835. catch(...)
  2836. {
  2837. if (!LocalRootDirectory.IsEmpty())
  2838. {
  2839. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  2840. }
  2841. throw;
  2842. }
  2843. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  2844. }
  2845. else
  2846. {
  2847. DebugAssert(!FStandaloneEditing);
  2848. TForm * Editor =
  2849. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  2850. SaveAllInternalEditors, AnyInternalEditorModified,
  2851. L"", false, SessionColor, -1);
  2852. FLocalEditors->Add(Editor);
  2853. }
  2854. }
  2855. else
  2856. {
  2857. HANDLE Process;
  2858. if (ExecuteFileBy == efExternalEditor)
  2859. {
  2860. UnicodeString Program, Params, Dir;
  2861. Data->Command = ExternalEditor->ExternalEditor;
  2862. ReformatFileNameCommand(Data->Command);
  2863. SplitCommand(Data->Command, Program, Params, Dir);
  2864. Params = ExpandFileNameCommand(Params, FileName);
  2865. Program = ExpandEnvironmentVariables(Program);
  2866. if (!ExecuteShell(Program, Params, Process))
  2867. {
  2868. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  2869. }
  2870. }
  2871. else
  2872. {
  2873. DebugAssert(Side == osRemote);
  2874. if (!ExecuteShell(FileName, L"", Process))
  2875. {
  2876. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  2877. }
  2878. }
  2879. if ((Side == osLocal) ||
  2880. ((ExecuteFileBy == efShell) &&
  2881. !WinConfiguration->Editor.SDIShellEditor) ||
  2882. ((ExecuteFileBy == efExternalEditor) &&
  2883. !ExternalEditor->SDIExternalEditor))
  2884. {
  2885. // no need for handle
  2886. if (Process != NULL)
  2887. {
  2888. DebugCheck(CloseHandle(Process));
  2889. }
  2890. Process = INVALID_HANDLE_VALUE;
  2891. }
  2892. else
  2893. {
  2894. if (Process == NULL)
  2895. {
  2896. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  2897. }
  2898. }
  2899. if (Side == osRemote)
  2900. {
  2901. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  2902. }
  2903. }
  2904. }
  2905. //---------------------------------------------------------------------------
  2906. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  2907. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2908. void * /*Arg*/)
  2909. {
  2910. if (Token != NULL)
  2911. {
  2912. EditorFormFileSave(static_cast<TForm *>(Token));
  2913. }
  2914. }
  2915. //---------------------------------------------------------------------------
  2916. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  2917. {
  2918. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  2919. {
  2920. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  2921. }
  2922. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  2923. }
  2924. //---------------------------------------------------------------------------
  2925. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  2926. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2927. void * Arg)
  2928. {
  2929. if ((Token != NULL) &&
  2930. IsEditorFormModified(static_cast<TForm *>(Token)))
  2931. {
  2932. *static_cast<bool *>(Arg) = true;
  2933. }
  2934. }
  2935. //---------------------------------------------------------------------------
  2936. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  2937. TObject * /*Sender*/, bool & Modified)
  2938. {
  2939. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  2940. {
  2941. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  2942. {
  2943. Modified = true;
  2944. }
  2945. }
  2946. if (!Modified)
  2947. {
  2948. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  2949. }
  2950. }
  2951. //---------------------------------------------------------------------------
  2952. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  2953. {
  2954. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  2955. }
  2956. //---------------------------------------------------------------------------
  2957. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  2958. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  2959. UnicodeString & Result, UnicodeString & RootDirectory)
  2960. {
  2961. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  2962. {
  2963. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  2964. Result = RootDirectory;
  2965. }
  2966. else
  2967. {
  2968. RootDirectory = L"";
  2969. Result = WinConfiguration->ExpandedTemporaryDirectory();
  2970. Result = IncludeTrailingBackslash(Result);
  2971. }
  2972. if (WinConfiguration->TemporaryDirectoryAppendSession)
  2973. {
  2974. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  2975. }
  2976. if (WinConfiguration->TemporaryDirectoryAppendPath)
  2977. {
  2978. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  2979. {
  2980. RemoteDirectory.Delete(1, 1);
  2981. }
  2982. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  2983. }
  2984. if (!ForceDirectories(ApiPath(Result)))
  2985. {
  2986. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  2987. }
  2988. }
  2989. //---------------------------------------------------------------------------
  2990. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  2991. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  2992. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  2993. {
  2994. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2995. if (ForceText)
  2996. {
  2997. CopyParam.TransferMode = tmAscii;
  2998. }
  2999. TemporaryFileCopyParam(CopyParam);
  3000. if (AllFiles)
  3001. {
  3002. CopyParam.IncludeFileMask = TFileMasks();
  3003. }
  3004. if (TempDir.IsEmpty())
  3005. {
  3006. TemporaryDirectoryForRemoteFiles(FTerminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3007. }
  3008. DebugAssert(!FAutoOperation);
  3009. FAutoOperation = AutoOperation;
  3010. Terminal->ExceptionOnFail = true;
  3011. try
  3012. {
  3013. try
  3014. {
  3015. // turn off confirmations, as for MDI editors we may possibly download
  3016. // the same file over
  3017. Terminal->CopyToLocal(
  3018. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3019. if (GetTargetNames)
  3020. {
  3021. for (int i = 0; i < FileList->Count; i++)
  3022. {
  3023. FileList->Strings[i] =
  3024. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3025. }
  3026. }
  3027. }
  3028. catch(...)
  3029. {
  3030. if (!RootTempDir.IsEmpty())
  3031. {
  3032. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3033. }
  3034. throw;
  3035. }
  3036. }
  3037. __finally
  3038. {
  3039. FAutoOperation = false;
  3040. Terminal->ExceptionOnFail = false;
  3041. }
  3042. }
  3043. //---------------------------------------------------------------------------
  3044. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3045. {
  3046. // Do not waste time checking for default editor list the next time,
  3047. // or testing if default editor is not notepad.
  3048. bool TryNextTime = false;
  3049. UnicodeString UsageState;
  3050. if (!WinConfiguration->EditorList->IsDefaultList())
  3051. {
  3052. UsageState = "H";
  3053. }
  3054. else
  3055. {
  3056. UnicodeString Executable;
  3057. UnicodeString ExecutableDescription;
  3058. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3059. {
  3060. UnicodeString Message =
  3061. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3062. unsigned int Answer =
  3063. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3064. if (Answer != qaOK)
  3065. {
  3066. UsageState = "R";
  3067. }
  3068. else
  3069. {
  3070. UsageState = "A";
  3071. TEditorData EditorData;
  3072. EditorData.Editor = edExternal;
  3073. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3074. EditorData.DecideExternalEditorText();
  3075. TEditorList EditorList;
  3076. EditorList = *WinConfiguration->EditorList;
  3077. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3078. WinConfiguration->EditorList = &EditorList;
  3079. }
  3080. }
  3081. }
  3082. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3083. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3084. }
  3085. //---------------------------------------------------------------------------
  3086. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3087. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3088. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3089. {
  3090. if (ExecuteFileBy == efDefaultEditor)
  3091. {
  3092. if (!WinConfiguration->OfferedEditorAutoConfig)
  3093. {
  3094. EditorAutoConfig();
  3095. }
  3096. const TEditorPreferences * Editor =
  3097. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3098. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3099. {
  3100. ExecuteFileBy = efInternalEditor;
  3101. ExternalEditor = NULL;
  3102. }
  3103. else if (Editor->Data->Editor == edOpen)
  3104. {
  3105. ExecuteFileBy = efShell;
  3106. ExternalEditor = NULL;
  3107. }
  3108. else
  3109. {
  3110. ExecuteFileBy = efExternalEditor;
  3111. ExternalEditor = Editor->Data;
  3112. }
  3113. }
  3114. }
  3115. //---------------------------------------------------------------------------
  3116. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3117. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3118. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3119. {
  3120. UnicodeString OriginalFileName;
  3121. UnicodeString LocalRootDirectory;
  3122. UnicodeString RemoteDirectory;
  3123. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3124. (Side == osLocal), MaskParams);
  3125. UnicodeString Counter;
  3126. UnicodeString LocalFileName;
  3127. bool Handled = false;
  3128. if (Side == osRemote)
  3129. {
  3130. // We need to trim VMS version here, so that we use name without version
  3131. // when uploading back to create a new version of the file
  3132. OriginalFileName = FTerminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3133. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3134. TObject * Token = NULL;
  3135. UnicodeString LocalDirectory;
  3136. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3137. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3138. LocalDirectory))
  3139. {
  3140. if (Token != NULL)
  3141. {
  3142. TForm * Form = dynamic_cast<TForm *>(Token);
  3143. if (Form->WindowState == wsMinimized)
  3144. {
  3145. ShowWindow(Form->Handle, SW_RESTORE);
  3146. }
  3147. else
  3148. {
  3149. Form->SetFocus();
  3150. }
  3151. Handled = true;
  3152. }
  3153. else
  3154. {
  3155. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3156. }
  3157. }
  3158. if (!Handled)
  3159. {
  3160. TStringList * FileList1 = new TStringList();
  3161. try
  3162. {
  3163. FileList1->AddObject(FullFileName, Object);
  3164. TemporarilyDownloadFiles(FileList1,
  3165. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3166. LocalRootDirectory, LocalDirectory, true, true, true);
  3167. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3168. }
  3169. __finally
  3170. {
  3171. delete FileList1;
  3172. }
  3173. switch (ExecuteFileBy)
  3174. {
  3175. case efShell:
  3176. Counter = "RemoteFilesExecuted";
  3177. break;
  3178. case efInternalEditor:
  3179. Counter = "RemoteFilesOpenedInInternalEditor";
  3180. break;
  3181. case efExternalEditor:
  3182. Counter = "RemoteFilesOpenedInExternalEditor";
  3183. break;
  3184. default:
  3185. DebugFail();
  3186. }
  3187. }
  3188. }
  3189. else
  3190. {
  3191. LocalFileName = FullFileName;
  3192. OriginalFileName = ExtractFileName(FullFileName);
  3193. switch (ExecuteFileBy)
  3194. {
  3195. case efShell:
  3196. Counter = "LocalFilesExecuted";
  3197. break;
  3198. case efInternalEditor:
  3199. Counter = "LocalFilesOpenedInInternalEditor";
  3200. break;
  3201. case efExternalEditor:
  3202. Counter = "LocalFilesOpenedInExternalEditor";
  3203. break;
  3204. default:
  3205. DebugFail();
  3206. }
  3207. }
  3208. if (!Handled)
  3209. {
  3210. Configuration->Usage->Inc(Counter);
  3211. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3212. ExternalEditor, LocalRootDirectory, RemoteDirectory);
  3213. }
  3214. }
  3215. //---------------------------------------------------------------------------
  3216. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3217. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3218. bool AllSelected, bool OnFocused)
  3219. {
  3220. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3221. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3222. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3223. Side = GetSide(Side);
  3224. TCustomDirView * DView = DirView(Side);
  3225. TStrings * FileList = AllSelected ?
  3226. DView->CreateFileList(OnFocused, Side == osLocal) :
  3227. DView->CreateFocusedFileList(Side == osLocal);
  3228. try
  3229. {
  3230. DebugAssert(AllSelected || (FileList->Count == 1));
  3231. for (int i = 0; i < FileList->Count; i++)
  3232. {
  3233. UnicodeString ListFileName = FileList->Strings[i];
  3234. UnicodeString FileNameOnly = (Side == osRemote) ?
  3235. UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3236. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3237. if (!DView->ItemIsDirectory(Item))
  3238. {
  3239. UnicodeString FullFileName;
  3240. if (Side == osRemote)
  3241. {
  3242. FullFileName = RemoteDirView->Path + ListFileName;
  3243. }
  3244. else
  3245. {
  3246. FullFileName = ListFileName;
  3247. }
  3248. TFileMasks::TParams MaskParams;
  3249. MaskParams.Size = DView->ItemFileSize(Item);
  3250. TDateTimePrecision Precision;
  3251. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3252. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3253. FileList->Objects[i], MaskParams);
  3254. }
  3255. }
  3256. }
  3257. __finally
  3258. {
  3259. delete FileList;
  3260. }
  3261. }
  3262. //---------------------------------------------------------------------------
  3263. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3264. {
  3265. // do not forget to add additional options to TemporarilyDownloadFiles, and AS
  3266. CopyParam.FileNameCase = ncNoChange;
  3267. CopyParam.PreserveRights = false;
  3268. CopyParam.PreserveReadOnly = false;
  3269. CopyParam.ReplaceInvalidChars = true;
  3270. CopyParam.IncludeFileMask = TFileMasks();
  3271. CopyParam.NewerOnly = false;
  3272. CopyParam.FileMask = L"";
  3273. }
  3274. //---------------------------------------------------------------------------
  3275. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3276. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3277. {
  3278. TTerminalManager * Manager = TTerminalManager::Instance();
  3279. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3280. {
  3281. if (!NonVisualDataModule->Busy)
  3282. {
  3283. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3284. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3285. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3286. if (Data->Terminal == NULL)
  3287. {
  3288. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3289. if (SameSiteTerminal != NULL)
  3290. {
  3291. UnicodeString Message =
  3292. FMTLOAD(EDIT_SESSION_REATTACH,
  3293. (FileNameOnly, Data->SessionName, FileNameOnly));
  3294. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3295. {
  3296. Data->Terminal = Terminal;
  3297. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3298. Data->SessionName = Terminal->SessionData->SessionName;
  3299. // We might also overwrite session data
  3300. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3301. }
  3302. else
  3303. {
  3304. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3305. Abort();
  3306. }
  3307. }
  3308. }
  3309. // foreground session should reconnect itself
  3310. else if (Terminal != Data->Terminal)
  3311. {
  3312. UnicodeString Message =
  3313. MainInstructions(
  3314. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3315. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3316. {
  3317. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3318. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3319. }
  3320. else
  3321. {
  3322. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3323. Abort();
  3324. }
  3325. }
  3326. }
  3327. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3328. {
  3329. Configuration->Usage->Inc(L"EditInactiveSession");
  3330. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3331. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3332. (ExtractFileName(FileName), Data->SessionName)));
  3333. }
  3334. }
  3335. TStrings * FileList = new TStringList();
  3336. try
  3337. {
  3338. FileList->Add(FileName);
  3339. // consider using the same settings (preset) as when the file was downloaded
  3340. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3341. TemporaryFileCopyParam(CopyParam);
  3342. if (Data->ForceText)
  3343. {
  3344. CopyParam.TransferMode = tmAscii;
  3345. }
  3346. // so i do not need to worry if masking algorithm works in all cases
  3347. // ("" means "copy file name", no masking is actually done)
  3348. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3349. {
  3350. CopyParam.FileMask = L"";
  3351. }
  3352. else
  3353. {
  3354. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3355. }
  3356. DebugAssert(Data->Queue != NULL);
  3357. int Params = cpNoConfirmation | cpTemporary;
  3358. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3359. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3360. QueueItem->CompleteEvent = UploadCompleteEvent;
  3361. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3362. }
  3363. __finally
  3364. {
  3365. delete FileList;
  3366. }
  3367. }
  3368. //---------------------------------------------------------------------------
  3369. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3370. const UnicodeString FileName, const TEditedFileData * Data)
  3371. {
  3372. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3373. DebugAssert(!NonVisualDataModule->Busy);
  3374. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3375. {
  3376. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3377. (ExtractFileName(FileName), Data->SessionName)));
  3378. }
  3379. TTerminal * PrevTerminal = TTerminalManager::Instance()->ActiveTerminal;
  3380. TTerminalManager::Instance()->ActiveTerminal = Data->Terminal;
  3381. NonVisualDataModule->StartBusy();
  3382. try
  3383. {
  3384. std::unique_ptr<TRemoteFile> File;
  3385. UnicodeString RemoteFileName =
  3386. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3387. FTerminal->ExceptionOnFail = true;
  3388. try
  3389. {
  3390. TRemoteFile * AFile = NULL;
  3391. FTerminal->ReadFile(RemoteFileName, AFile);
  3392. File.reset(AFile);
  3393. if (!File->HaveFullFileName)
  3394. {
  3395. File->FullFileName = RemoteFileName;
  3396. }
  3397. }
  3398. __finally
  3399. {
  3400. FTerminal->ExceptionOnFail = false;
  3401. }
  3402. std::unique_ptr<TStrings> FileList(new TStringList());
  3403. FileList->AddObject(RemoteFileName, File.get());
  3404. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3405. UnicodeString TempDir = ExtractFilePath(FileName);
  3406. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3407. TempDir, true, true, true);
  3408. // sanity check, the target file name should be still the same
  3409. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3410. }
  3411. __finally
  3412. {
  3413. NonVisualDataModule->EndBusy();
  3414. // it actually may not exist anymore...
  3415. TTerminalManager::Instance()->ActiveTerminal = PrevTerminal;
  3416. }
  3417. }
  3418. //---------------------------------------------------------------------------
  3419. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3420. const TEditedFileData * Data, bool & KeepOpen)
  3421. {
  3422. // Command is set for external editors only (not for "shell" open).
  3423. if (!Data->Command.IsEmpty())
  3424. {
  3425. bool AnyFound = false;
  3426. bool AnyMDI = false;
  3427. bool AnyNonMDI = false;
  3428. bool AnyDetect = false;
  3429. TEditorList * EditorList = new TEditorList();
  3430. try
  3431. {
  3432. *EditorList = *WinConfiguration->EditorList;
  3433. for (int i = 0; i < EditorList->Count; i++)
  3434. {
  3435. const TEditorPreferences * Editor = EditorList->Editors[i];
  3436. if ((Editor->Data->Editor == edExternal) &&
  3437. (Editor->Data->ExternalEditor == Data->Command))
  3438. {
  3439. AnyFound = true;
  3440. if (Editor->Data->SDIExternalEditor)
  3441. {
  3442. AnyNonMDI = true;
  3443. if (Editor->Data->DetectMDIExternalEditor)
  3444. {
  3445. AnyDetect = true;
  3446. }
  3447. }
  3448. else
  3449. {
  3450. AnyMDI = true;
  3451. }
  3452. }
  3453. }
  3454. bool EnableMDI = false;
  3455. bool DisableDetect = false;
  3456. if (AnyMDI)
  3457. {
  3458. KeepOpen = true;
  3459. if (AnyNonMDI)
  3460. {
  3461. // there is at least one instance of the editor with MDI support enabled,
  3462. // and one with disabled, enable it for all instances
  3463. EnableMDI = true;
  3464. }
  3465. }
  3466. else if (AnyFound && !AnyDetect)
  3467. {
  3468. // at least once instance found but all have MDI autodetection disabled
  3469. // => close the file (default action)
  3470. }
  3471. else
  3472. {
  3473. // no instance of the editor has MDI support enabled
  3474. TMessageParams Params;
  3475. if (AnyFound)
  3476. {
  3477. // there is at least one instance of the editor
  3478. Params.Params |= mpNeverAskAgainCheck;
  3479. }
  3480. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3481. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3482. switch (Answer)
  3483. {
  3484. case qaNeverAskAgain:
  3485. DisableDetect = true;
  3486. break;
  3487. case qaNo:
  3488. EnableMDI = true;
  3489. KeepOpen = true;
  3490. break;
  3491. }
  3492. }
  3493. if (AnyFound && (EnableMDI || DisableDetect))
  3494. {
  3495. bool Changed = false;
  3496. for (int i = 0; i < EditorList->Count; i++)
  3497. {
  3498. const TEditorPreferences * Editor = EditorList->Editors[i];
  3499. if ((Editor->Data->Editor == edExternal) &&
  3500. (Editor->Data->ExternalEditor == Data->Command) &&
  3501. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3502. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3503. {
  3504. Changed = true;
  3505. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3506. if (EnableMDI)
  3507. {
  3508. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3509. }
  3510. if (DisableDetect)
  3511. {
  3512. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3513. }
  3514. EditorList->Change(i, UpdatedEditor);
  3515. }
  3516. }
  3517. if (Changed)
  3518. {
  3519. WinConfiguration->EditorList = EditorList;
  3520. }
  3521. }
  3522. }
  3523. __finally
  3524. {
  3525. delete EditorList;
  3526. }
  3527. }
  3528. else
  3529. {
  3530. // "open" case
  3531. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3532. qaOK, HELP_APP_EARLY_CLOSED);
  3533. }
  3534. }
  3535. //---------------------------------------------------------------------------
  3536. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3537. {
  3538. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3539. }
  3540. //---------------------------------------------------------------------------
  3541. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3542. {
  3543. SideEnter(osRemote);
  3544. }
  3545. //---------------------------------------------------------------------------
  3546. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3547. {
  3548. MakeNextInTabOrder(RemoteDirView, RemoteDriveView);
  3549. SideEnter(osRemote);
  3550. }
  3551. //---------------------------------------------------------------------------
  3552. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3553. {
  3554. FCurrentSide = Side;
  3555. if (Visible)
  3556. {
  3557. UpdateControls();
  3558. }
  3559. }
  3560. //---------------------------------------------------------------------------
  3561. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3562. TStrings * FileList, bool Alternative)
  3563. {
  3564. TCustomDirView * DView = DirView(Side);
  3565. DView->SaveSelection();
  3566. DView->SaveSelectedNames();
  3567. DebugAssert(!FAlternativeDelete);
  3568. FAlternativeDelete = Alternative;
  3569. try
  3570. {
  3571. if (Side == osRemote)
  3572. {
  3573. DebugAssert(Terminal != NULL);
  3574. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3575. }
  3576. else
  3577. {
  3578. try
  3579. {
  3580. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3581. }
  3582. __finally
  3583. {
  3584. ReloadLocalDirectory();
  3585. }
  3586. }
  3587. FAlternativeDelete = false;
  3588. }
  3589. catch(...)
  3590. {
  3591. FAlternativeDelete = false;
  3592. DView->DiscardSavedSelection();
  3593. throw;
  3594. }
  3595. DView->RestoreSelection();
  3596. }
  3597. //---------------------------------------------------------------------------
  3598. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3599. {
  3600. DebugAssert(Terminal);
  3601. RemoteDirView->SaveSelection();
  3602. RemoteDirView->SaveSelectedNames();
  3603. try
  3604. {
  3605. if (Lock)
  3606. {
  3607. Terminal->LockFiles(FileList);
  3608. }
  3609. else
  3610. {
  3611. Terminal->UnlockFiles(FileList);
  3612. }
  3613. }
  3614. catch(...)
  3615. {
  3616. RemoteDirView->DiscardSavedSelection();
  3617. throw;
  3618. }
  3619. RemoteDirView->RestoreSelection();
  3620. }
  3621. //---------------------------------------------------------------------------
  3622. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TTerminal *& Session,
  3623. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3624. bool NoConfirmation, bool Move)
  3625. {
  3626. DebugAssert(Terminal != NULL);
  3627. // update Terminal->StateData->RemoteDirectory
  3628. UpdateTerminal(Terminal);
  3629. if (Session == NULL)
  3630. {
  3631. Session = TTerminalManager::Instance()->ActiveTerminal;
  3632. }
  3633. if (Session == TTerminalManager::Instance()->ActiveTerminal)
  3634. {
  3635. if (RemoteDriveView->DropTarget != NULL)
  3636. {
  3637. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3638. }
  3639. else if (RemoteDirView->DropTarget != NULL)
  3640. {
  3641. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3642. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3643. }
  3644. else
  3645. {
  3646. Target = RemoteDirView->Path;
  3647. }
  3648. }
  3649. else
  3650. {
  3651. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(Session));
  3652. Target = ManagedTerminal->StateData->RemoteDirectory;
  3653. }
  3654. Target = UnixIncludeTrailingBackslash(Target);
  3655. if (FileList->Count == 1)
  3656. {
  3657. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3658. }
  3659. else
  3660. {
  3661. FileMask = AnyMask;
  3662. }
  3663. DirectCopy = FTerminal->IsCapable[fcRemoteCopy] || FTerminal->IsCapable[fcSecondaryShell];
  3664. bool Result = true;
  3665. if (!NoConfirmation)
  3666. {
  3667. bool Multi = (FileList->Count > 1);
  3668. if (Move)
  3669. {
  3670. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3671. }
  3672. else
  3673. {
  3674. TStrings * Sessions = TTerminalManager::Instance()->TerminalList;
  3675. TStrings * Directories = new TStringList;
  3676. try
  3677. {
  3678. for (int Index = 0; Index < Sessions->Count; Index++)
  3679. {
  3680. TManagedTerminal * Terminal =
  3681. dynamic_cast<TManagedTerminal *>(Sessions->Objects[Index]);
  3682. Directories->Add(Terminal->StateData->RemoteDirectory);
  3683. }
  3684. TDirectRemoteCopy AllowDirectCopy;
  3685. if (FTerminal->IsCapable[fcRemoteCopy] || FTerminal->CommandSessionOpened)
  3686. {
  3687. DebugAssert(DirectCopy);
  3688. AllowDirectCopy = drcAllow;
  3689. }
  3690. else if (FTerminal->IsCapable[fcSecondaryShell])
  3691. {
  3692. DebugAssert(DirectCopy);
  3693. AllowDirectCopy = drcConfirmCommandSession;
  3694. }
  3695. else
  3696. {
  3697. DebugAssert(!DirectCopy);
  3698. AllowDirectCopy = drcDisallow;
  3699. }
  3700. void * ASession = Session;
  3701. Result = DoRemoteCopyDialog(Sessions, Directories, AllowDirectCopy,
  3702. Multi, ASession, Target, FileMask, DirectCopy, TTerminalManager::Instance()->ActiveTerminal);
  3703. Session = static_cast<TTerminal *>(ASession);
  3704. }
  3705. __finally
  3706. {
  3707. delete Directories;
  3708. }
  3709. }
  3710. }
  3711. return Result;
  3712. }
  3713. //---------------------------------------------------------------------------
  3714. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3715. TStrings * FileList, bool NoConfirmation, bool Move, TTerminal * Session)
  3716. {
  3717. bool DirectCopy;
  3718. UnicodeString Target, FileMask;
  3719. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3720. if (Result)
  3721. {
  3722. if (!Move && !DirectCopy)
  3723. {
  3724. Configuration->Usage->Inc("RemoteCopyTemp");
  3725. UnicodeString RootTempDir;
  3726. UnicodeString TempDir;
  3727. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3728. TStrings * TemporaryFilesList = new TStringList();
  3729. try
  3730. {
  3731. TMakeLocalFileListParams MakeFileListParam;
  3732. MakeFileListParam.FileList = TemporaryFilesList;
  3733. MakeFileListParam.FileTimes = NULL;
  3734. MakeFileListParam.IncludeDirs = true;
  3735. MakeFileListParam.Recursive = false;
  3736. ProcessLocalDirectory(TempDir, FTerminal->MakeLocalFileList, &MakeFileListParam);
  3737. TTerminalManager::Instance()->ActiveTerminal = Session;
  3738. if (TemporaryFilesList->Count > 0)
  3739. {
  3740. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3741. CopyParam.FileMask = FileMask;
  3742. DebugAssert(!FAutoOperation);
  3743. FAutoOperation = true;
  3744. FTerminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3745. }
  3746. }
  3747. __finally
  3748. {
  3749. delete TemporaryFilesList;
  3750. FAutoOperation = false;
  3751. if (!RootTempDir.IsEmpty())
  3752. {
  3753. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3754. }
  3755. }
  3756. }
  3757. else
  3758. {
  3759. RemoteDirView->SaveSelection();
  3760. RemoteDirView->SaveSelectedNames();
  3761. try
  3762. {
  3763. if (Move)
  3764. {
  3765. Configuration->Usage->Inc("RemoteMove");
  3766. Terminal->MoveFiles(FileList, Target, FileMask);
  3767. }
  3768. else
  3769. {
  3770. Configuration->Usage->Inc("RemoteCopyDirect");
  3771. DebugAssert(DirectCopy);
  3772. DebugAssert(Session == FTerminal);
  3773. if (FTerminal->IsCapable[fcRemoteCopy] ||
  3774. FTerminal->CommandSessionOpened ||
  3775. CommandSessionFallback())
  3776. {
  3777. Terminal->CopyFiles(FileList, Target, FileMask);
  3778. }
  3779. }
  3780. }
  3781. catch(...)
  3782. {
  3783. RemoteDirView->DiscardSavedSelection();
  3784. throw;
  3785. }
  3786. RemoteDirView->RestoreSelection();
  3787. }
  3788. }
  3789. return Result;
  3790. }
  3791. //---------------------------------------------------------------------------
  3792. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3793. const UnicodeString & Path, TRemoteProperties & Properties)
  3794. {
  3795. Properties.Valid = Properties.Valid << vpEncrypt;
  3796. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  3797. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  3798. }
  3799. //---------------------------------------------------------------------------
  3800. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3801. {
  3802. Side = GetSide(Side);
  3803. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3804. TRemoteProperties * AProperties = (Side == osRemote ? &Properties : NULL);
  3805. UnicodeString Name = LoadStr(NEW_FOLDER);
  3806. int AllowedChanges =
  3807. FLAGMASK((Side == osRemote) && Terminal->IsCapable[fcModeChanging], cpMode);
  3808. bool SaveSettings = false;
  3809. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  3810. {
  3811. TWindowLock Lock(this);
  3812. if (Side == osRemote)
  3813. {
  3814. if (SaveSettings)
  3815. {
  3816. GUIConfiguration->NewDirectoryProperties = Properties;
  3817. }
  3818. CreateRemoteDirectory(Name, Properties);
  3819. }
  3820. else
  3821. {
  3822. DirView(Side)->CreateDirectory(Name);
  3823. }
  3824. }
  3825. }
  3826. //---------------------------------------------------------------------------
  3827. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  3828. {
  3829. TWindowLock Lock(this);
  3830. DirView(Side)->ExecuteHomeDirectory();
  3831. }
  3832. //---------------------------------------------------------------------------
  3833. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  3834. {
  3835. DoOpenDirectoryDialog(odBrowse, Side);
  3836. }
  3837. //---------------------------------------------------------------------------
  3838. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  3839. {
  3840. UnicodeString Path = Bookmark->GetSideDirectory(Side);
  3841. bool Result = !Path.IsEmpty();
  3842. if (Result)
  3843. {
  3844. // While we might get here when the session is closed (from location profiles),
  3845. // it's not a problem as the Path setter is noop then
  3846. DirView(Side)->Path = Path;
  3847. }
  3848. return Result;
  3849. }
  3850. //---------------------------------------------------------------------------
  3851. void __fastcall TCustomScpExplorerForm::CalculateSize(
  3852. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  3853. bool & Close)
  3854. {
  3855. // terminal can be already closed (e.g. dropped connection)
  3856. if (Terminal != NULL)
  3857. {
  3858. try
  3859. {
  3860. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  3861. }
  3862. catch(...)
  3863. {
  3864. if (!Terminal->Active)
  3865. {
  3866. Close = true;
  3867. }
  3868. throw;
  3869. }
  3870. }
  3871. }
  3872. //---------------------------------------------------------------------------
  3873. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  3874. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  3875. bool & Close)
  3876. {
  3877. // terminal can be already closed (e.g. dropped connection)
  3878. if (Terminal != NULL)
  3879. {
  3880. Configuration->Usage->Inc(L"ChecksumCalculated");
  3881. try
  3882. {
  3883. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  3884. }
  3885. catch(...)
  3886. {
  3887. if (!Terminal->Active)
  3888. {
  3889. Close = true;
  3890. }
  3891. throw;
  3892. }
  3893. }
  3894. }
  3895. //---------------------------------------------------------------------------
  3896. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  3897. {
  3898. bool Result;
  3899. if (Side == osRemote)
  3900. {
  3901. TRemoteTokenList * GroupList = NULL;
  3902. TRemoteTokenList * UserList = NULL;
  3903. try
  3904. {
  3905. TRemoteProperties CurrentProperties;
  3906. if (Terminal->LoadFilesProperties(FileList))
  3907. {
  3908. RemoteDirView->Invalidate();
  3909. }
  3910. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  3911. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  3912. if (CapableGroupChanging || CapableOwnerChanging)
  3913. {
  3914. if (CapableGroupChanging)
  3915. {
  3916. GroupList = Terminal->Groups->Duplicate();
  3917. }
  3918. if (CapableOwnerChanging)
  3919. {
  3920. UserList = Terminal->Users->Duplicate();
  3921. }
  3922. TRemoteDirectory * Files = Terminal->Files;
  3923. int Count = Files->Count;
  3924. if (Count > 100)
  3925. {
  3926. Count = 100;
  3927. }
  3928. for (int Index = 0; Index < Count; Index++)
  3929. {
  3930. TRemoteFile * File = Files->Files[Index];
  3931. if (CapableGroupChanging)
  3932. {
  3933. GroupList->AddUnique(File->Group);
  3934. }
  3935. if (CapableOwnerChanging)
  3936. {
  3937. UserList->AddUnique(File->Owner);
  3938. }
  3939. }
  3940. // if we haven't collected tokens for all files in current directory,
  3941. // make sure we collect them at least for all selected files.
  3942. // (note that so far the files in FileList has to be from current direcotry)
  3943. if (Count < Files->Count)
  3944. {
  3945. for (int Index = 0; Index < FileList->Count; Index++)
  3946. {
  3947. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  3948. if (CapableGroupChanging)
  3949. {
  3950. GroupList->AddUnique(File->Group);
  3951. }
  3952. if (CapableOwnerChanging)
  3953. {
  3954. UserList->AddUnique(File->Owner);
  3955. }
  3956. }
  3957. }
  3958. }
  3959. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  3960. int Flags = 0;
  3961. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  3962. if (CapableOwnerChanging) Flags |= cpOwner;
  3963. if (CapableGroupChanging) Flags |= cpGroup;
  3964. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  3965. if (Terminal->IsCapable[fcCalculatingChecksum])
  3966. {
  3967. CalculateChecksumEvent = CalculateChecksum;
  3968. }
  3969. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  3970. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  3971. TRemoteProperties NewProperties = CurrentProperties;
  3972. Result =
  3973. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  3974. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  3975. Terminal->IsCapable[fcGroupOwnerChangingByID],
  3976. CalculateSize, CalculateChecksumEvent);
  3977. if (Result)
  3978. {
  3979. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  3980. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  3981. }
  3982. }
  3983. __finally
  3984. {
  3985. delete GroupList;
  3986. delete UserList;
  3987. }
  3988. }
  3989. else
  3990. {
  3991. DirView(Side)->DisplayPropertiesMenu();
  3992. Result = true;
  3993. }
  3994. return Result;
  3995. }
  3996. //---------------------------------------------------------------------------
  3997. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  3998. {
  3999. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4000. {
  4001. FIgnoreNextDialogChar = Key;
  4002. }
  4003. Key = 0;
  4004. }
  4005. //---------------------------------------------------------------------------
  4006. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4007. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4008. {
  4009. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4010. if (Command != NULL)
  4011. {
  4012. KeyProcessed(Key, Shift);
  4013. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4014. {
  4015. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4016. false, false, const_cast<TCustomCommandType *>(Command));
  4017. }
  4018. }
  4019. }
  4020. //---------------------------------------------------------------------------
  4021. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4022. {
  4023. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4024. {
  4025. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4026. }
  4027. if (!DirView(osCurrent)->IsEditing())
  4028. {
  4029. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4030. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4031. {
  4032. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4033. if (((Action->ShortCut == KeyShortCut) ||
  4034. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4035. AllowedAction(Action, aaShortCut))
  4036. {
  4037. // Has to be called before the action as the dialog char is already in queue.
  4038. // So when the action consumes message queue, we already need to have the
  4039. // FIgnoreNextDialogChar set
  4040. KeyProcessed(Key, Shift);
  4041. // Reset reference to previous component (when menu/toolbar was clicked).
  4042. // Needed to detect that action was invoked by keyboard shortcut
  4043. // in TNonVisualDataModule::ExplorerActionsExecute
  4044. Action->ActionComponent = NULL;
  4045. Action->Execute();
  4046. return;
  4047. }
  4048. }
  4049. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4050. {
  4051. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4052. {
  4053. KeyProcessed(Key, Shift);
  4054. TTerminalManager::Instance()->ActiveTerminalIndex = i;
  4055. return;
  4056. }
  4057. }
  4058. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4059. {
  4060. KeyProcessed(Key, Shift);
  4061. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4062. }
  4063. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4064. if (IsCustomShortCut(CustomShortCut))
  4065. {
  4066. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4067. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4068. if (WinConfiguration->SharedBookmarks != NULL)
  4069. {
  4070. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4071. if ((Bookmark != NULL) &&
  4072. OpenBookmark(FCurrentSide, Bookmark))
  4073. {
  4074. KeyProcessed(Key, Shift);
  4075. }
  4076. }
  4077. }
  4078. }
  4079. TForm::KeyDown(Key, Shift);
  4080. }
  4081. //---------------------------------------------------------------------------
  4082. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4083. {
  4084. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4085. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  4086. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4087. DebugAssert(Terminal);
  4088. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4089. bool SecurityEnabled = !SessionInfo.SecurityProtocolName.IsEmpty();
  4090. SessionStatusBar->Panels->Items[Offset + 0]->Enabled = SecurityEnabled;
  4091. // expanded from ?: to avoid memory leaks
  4092. if (SecurityEnabled)
  4093. {
  4094. SessionStatusBar->Panels->Items[Offset + 0]->Hint =
  4095. FMTLOAD(STATUS_SECURE, (SessionInfo.SecurityProtocolName));
  4096. }
  4097. else
  4098. {
  4099. SessionStatusBar->Panels->Items[Offset + 0]->Hint = LoadStr(STATUS_INSECURE);
  4100. }
  4101. if (FileSystemInfo.ProtocolName.IsEmpty())
  4102. {
  4103. SessionStatusBar->Panels->Items[Offset + 1]->Caption = SessionInfo.ProtocolName;
  4104. }
  4105. else
  4106. {
  4107. SessionStatusBar->Panels->Items[Offset + 1]->Caption = FileSystemInfo.ProtocolName;
  4108. }
  4109. SessionStatusBar->Panels->Items[Offset + 1]->Hint = LoadStr(STATUS_PROTOCOL_HINT);
  4110. SessionStatusBar->Panels->Items[Offset + 2]->Enabled =
  4111. (!SessionInfo.CSCompression.IsEmpty() || !SessionInfo.SCCompression.IsEmpty());
  4112. UnicodeString CSCompressionStr = DefaultStr(SessionInfo.CSCompression, LoadStr(NO_STR));
  4113. UnicodeString SCCompressionStr = DefaultStr(SessionInfo.SCCompression, LoadStr(NO_STR));
  4114. UnicodeString CompressionStr = CSCompressionStr;
  4115. if (CSCompressionStr != SCCompressionStr)
  4116. {
  4117. CompressionStr += UnicodeString(L"/") + SCCompressionStr;
  4118. }
  4119. SessionStatusBar->Panels->Items[Offset + 2]->Hint = FMTLOAD(STATUS_COMPRESSION_HINT, (CompressionStr));
  4120. SessionStatusBar->Panels->Items[Offset + 3]->Hint = LoadStr(STATUS_DURATION_HINT);
  4121. UpdateStatusBar();
  4122. }
  4123. //---------------------------------------------------------------------------
  4124. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4125. {
  4126. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4127. DebugAssert(SessionStatusBar != NULL);
  4128. if (!Terminal || !Terminal->Active || Terminal->Status < ssOpened)
  4129. {
  4130. // note: (Terminal->Status < sshReady) currently never happens here,
  4131. // so STATUS_CONNECTING is never used
  4132. SessionStatusBar->SimplePanel = true;
  4133. SessionStatusBar->SimpleText = LoadStr(
  4134. !Terminal || !Terminal->Active ? STATUS_NOT_CONNECTED : STATUS_CONNECTING);
  4135. }
  4136. else
  4137. {
  4138. DebugAssert(Terminal);
  4139. SessionStatusBar->SimplePanel = false;
  4140. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4141. if (!FNote.IsEmpty())
  4142. {
  4143. SessionStatusBar->Panels->Items[0]->Caption = FNote;
  4144. }
  4145. else
  4146. {
  4147. UpdateStatusPanelText(SessionStatusBar->Panels->Items[0]);
  4148. }
  4149. SessionStatusBar->Panels->Items[0]->Hint = FNoteHints;
  4150. SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1]->Caption =
  4151. FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4152. }
  4153. }
  4154. //---------------------------------------------------------------------------
  4155. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4156. {
  4157. Panel->Caption = L"";
  4158. }
  4159. //---------------------------------------------------------------------------
  4160. void __fastcall TCustomScpExplorerForm::Idle()
  4161. {
  4162. if (FShowing || FStandaloneEditing)
  4163. {
  4164. FEditorManager->Check();
  4165. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4166. // that needs to know if queue view is visible (and it may be closed after queue update)
  4167. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4168. }
  4169. if (FShowing)
  4170. {
  4171. if (!NonVisualDataModule->Busy &&
  4172. // Menu is opened or drag&drop is going on
  4173. (Mouse->Capture == NULL))
  4174. {
  4175. if (FRefreshRemoteDirectory)
  4176. {
  4177. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4178. {
  4179. Terminal->RefreshDirectory();
  4180. }
  4181. FRefreshRemoteDirectory = false;
  4182. }
  4183. if (FRefreshLocalDirectory)
  4184. {
  4185. ReloadLocalDirectory();
  4186. FRefreshLocalDirectory = false;
  4187. }
  4188. if (WinConfiguration->RefreshRemotePanel)
  4189. {
  4190. TManagedTerminal * ManagedTerminal =
  4191. dynamic_cast<TManagedTerminal *>(Terminal);
  4192. if ((ManagedTerminal != NULL) && (Terminal->Status == ssOpened) &&
  4193. (Now() - ManagedTerminal->DirectoryLoaded >
  4194. WinConfiguration->RefreshRemotePanelInterval))
  4195. {
  4196. RemoteDirView->ReloadDirectory();
  4197. }
  4198. }
  4199. }
  4200. }
  4201. if (FShowing || FStandaloneEditing)
  4202. {
  4203. if (FQueueStatusInvalidated)
  4204. {
  4205. UpdateQueueStatus(false);
  4206. }
  4207. RefreshQueueItems();
  4208. }
  4209. if (FShowing)
  4210. {
  4211. UpdateStatusBar();
  4212. }
  4213. FIgnoreNextDialogChar = 0;
  4214. }
  4215. //---------------------------------------------------------------------------
  4216. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4217. {
  4218. try
  4219. {
  4220. FUserActionTimer->Enabled = false;
  4221. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4222. {
  4223. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4224. {
  4225. FPendingQueueActionItem->ProcessUserAction();
  4226. }
  4227. }
  4228. }
  4229. __finally
  4230. {
  4231. FPendingQueueActionItem = NULL;
  4232. }
  4233. }
  4234. //---------------------------------------------------------------------------
  4235. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4236. {
  4237. if (WinConfiguration->MinimizeToTray)
  4238. {
  4239. UpdateTrayIcon();
  4240. FTrayIcon->Visible = true;
  4241. if (Visible)
  4242. {
  4243. ShowWindow(Handle, SW_HIDE);
  4244. }
  4245. }
  4246. }
  4247. //---------------------------------------------------------------------------
  4248. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4249. {
  4250. // WORKAROUND
  4251. // When restoring maximized window from minimization,
  4252. // rarely some controls do not align properly.
  4253. // Two instances seen (both for Commander):
  4254. // - When restoring, window is temporarily narrower (not maximized),
  4255. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4256. // Once maximized already, top dock shinks back, but the session PageControl,
  4257. // do not align up, leaving space between TopDock and PageControl.
  4258. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4259. for (int Index = 0; Index < ControlCount; Index++)
  4260. {
  4261. RealignControl(Controls[Index]);
  4262. }
  4263. if (FTrayIcon->Visible)
  4264. {
  4265. FTrayIcon->Visible = false;
  4266. if (Visible)
  4267. {
  4268. ShowWindow(Handle, SW_SHOW);
  4269. }
  4270. }
  4271. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4272. {
  4273. FNeedSession = false;
  4274. NonVisualDataModule->StartBusy();
  4275. try
  4276. {
  4277. NeedSession(false);
  4278. }
  4279. __finally
  4280. {
  4281. NonVisualDataModule->EndBusy();
  4282. }
  4283. }
  4284. }
  4285. //---------------------------------------------------------------------------
  4286. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4287. {
  4288. FTrayIcon->Hint = GetMainForm()->Caption;
  4289. }
  4290. //---------------------------------------------------------------------------
  4291. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4292. {
  4293. UpdateTrayIcon();
  4294. }
  4295. //---------------------------------------------------------------------------
  4296. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4297. {
  4298. // workaround
  4299. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4300. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4301. // But WM_ACTIVATEAPP message can be received even when
  4302. // the main window is minimized to the tray and internal editor window is focused
  4303. // (after another application was previously active)
  4304. if (::IsIconic(Handle))
  4305. {
  4306. if (!IsAppIconic())
  4307. {
  4308. SetAppIconic(true);
  4309. }
  4310. }
  4311. ::ApplicationRestore();
  4312. Application->BringToFront();
  4313. }
  4314. //---------------------------------------------------------------------------
  4315. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4316. {
  4317. RestoreApp();
  4318. }
  4319. //---------------------------------------------------------------------------
  4320. void __fastcall TCustomScpExplorerForm::NewSession(bool FromSite, const UnicodeString & SessionUrl)
  4321. {
  4322. if (OpenInNewWindow())
  4323. {
  4324. // todo: Pass FromSite
  4325. ExecuteNewInstance(SessionUrl);
  4326. }
  4327. else
  4328. {
  4329. TTerminalManager::Instance()->NewSession(FromSite, SessionUrl);
  4330. }
  4331. }
  4332. //---------------------------------------------------------------------------
  4333. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4334. {
  4335. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->SessionName;
  4336. // current working directories become defaults here, what is not right
  4337. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4338. StoredSessions->NewSession(SessionName, SessionData.get());
  4339. // modified only, explicit
  4340. StoredSessions->Save(false, true);
  4341. // encode session name because of slashes in hierarchical sessions
  4342. return EncodeUrlString(SessionName);
  4343. }
  4344. //---------------------------------------------------------------------------
  4345. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4346. {
  4347. if (OpenInNewWindow())
  4348. {
  4349. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4350. }
  4351. else
  4352. {
  4353. // current working directories become defaults here, what is not right
  4354. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4355. TTerminalManager * Manager = TTerminalManager::Instance();
  4356. TTerminal * Terminal = Manager->NewTerminal(SessionData.get());
  4357. Manager->ActiveTerminal = Terminal;
  4358. }
  4359. }
  4360. //---------------------------------------------------------------------------
  4361. void __fastcall TCustomScpExplorerForm::RenameSession()
  4362. {
  4363. UnicodeString Name = Terminal->SessionData->Name;
  4364. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME))
  4365. {
  4366. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4367. TSessionData::ValidateName(Name);
  4368. // This is inconsistent with how color (for example) is handled.
  4369. Terminal->SessionData->Name = Name;
  4370. UpdateControls();
  4371. // Add/Remove distinguishing paths from sessions of the same name.
  4372. DoTerminalListChanged();
  4373. }
  4374. }
  4375. //---------------------------------------------------------------------------
  4376. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4377. {
  4378. DebugAssert(Queue != NULL);
  4379. bool Result = Queue->IsEmpty;
  4380. if (!Result)
  4381. {
  4382. SetFocus();
  4383. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4384. }
  4385. return Result;
  4386. }
  4387. //---------------------------------------------------------------------------
  4388. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4389. {
  4390. return CanCloseQueue(FQueue);
  4391. }
  4392. //---------------------------------------------------------------------------
  4393. void __fastcall TCustomScpExplorerForm::CloseSession()
  4394. {
  4395. if (CanCloseQueue())
  4396. {
  4397. TTerminalManager::Instance()->FreeActiveTerminal();
  4398. }
  4399. }
  4400. //---------------------------------------------------------------------------
  4401. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4402. {
  4403. if (OpenInNewWindow())
  4404. {
  4405. // encode session name because of slashes in hierarchical sessions
  4406. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4407. }
  4408. else
  4409. {
  4410. TTerminalManager * Manager = TTerminalManager::Instance();
  4411. TTerminal * Terminal = Manager->NewTerminal(Data);
  4412. Manager->ActiveTerminal = Terminal;
  4413. }
  4414. }
  4415. //---------------------------------------------------------------------------
  4416. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4417. {
  4418. if (OpenInNewWindow())
  4419. {
  4420. ExecuteNewInstance(Name);
  4421. }
  4422. else
  4423. {
  4424. TTerminalManager * Manager = TTerminalManager::Instance();
  4425. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4426. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4427. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  4428. }
  4429. }
  4430. //---------------------------------------------------------------------------
  4431. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4432. bool &CanClose)
  4433. {
  4434. if (Terminal != NULL)
  4435. {
  4436. if (Terminal->Active && WinConfiguration->ConfirmClosingSession)
  4437. {
  4438. unsigned int Result;
  4439. TMessageParams Params(mpNeverAskAgainCheck);
  4440. UnicodeString Message;
  4441. int Answers = qaOK | qaCancel;
  4442. if (TTerminalManager::Instance()->Count > 1)
  4443. {
  4444. if (!WinConfiguration->AutoSaveWorkspace)
  4445. {
  4446. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE2);
  4447. Answers = qaYes | qaNo | qaCancel;
  4448. }
  4449. else
  4450. {
  4451. Message = MainInstructions(LoadStr(CLOSE_SESSIONS));
  4452. }
  4453. }
  4454. else
  4455. {
  4456. Message = MainInstructions(FMTLOAD(CLOSE_SESSION, (Terminal->SessionData->SessionName)));
  4457. }
  4458. if (WinConfiguration->AutoSaveWorkspace)
  4459. {
  4460. Message =
  4461. FORMAT("%s\n\n%s", (Message,
  4462. FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()))));
  4463. }
  4464. SetFocus();
  4465. Result = MessageDialog(Message, qtConfirmation,
  4466. Answers, HELP_NONE, &Params);
  4467. if (Result == qaNeverAskAgain)
  4468. {
  4469. WinConfiguration->ConfirmClosingSession = false;
  4470. }
  4471. if (Result == qaNo)
  4472. {
  4473. CanClose = SaveWorkspace(true);
  4474. // note that the workspace will be saved redundatly again from FormClose
  4475. }
  4476. else
  4477. {
  4478. CanClose =
  4479. (Result == qaOK) ||
  4480. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4481. (Result == qaNeverAskAgain);
  4482. }
  4483. }
  4484. if (CanClose)
  4485. {
  4486. CanClose = CanCloseQueue();
  4487. }
  4488. }
  4489. if (CanClose)
  4490. {
  4491. CanClose =
  4492. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4493. FEditorManager->CloseExternalFilesWithoutProcess();
  4494. if (CanClose)
  4495. {
  4496. while (CanClose && (FLocalEditors->Count > 0))
  4497. {
  4498. int PrevCount = FLocalEditors->Count;
  4499. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4500. CanClose = (FLocalEditors->Count < PrevCount);
  4501. }
  4502. if (CanClose)
  4503. {
  4504. CanClose = FEditorManager->Empty(true);
  4505. if (!CanClose)
  4506. {
  4507. SetFocus();
  4508. CanClose =
  4509. (MessageDialog(
  4510. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4511. }
  4512. }
  4513. }
  4514. }
  4515. }
  4516. //---------------------------------------------------------------------------
  4517. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4518. {
  4519. TForm * Form = dynamic_cast<TForm *>(Sender);
  4520. DebugAssert(Form != NULL);
  4521. Form->Close();
  4522. }
  4523. //---------------------------------------------------------------------------
  4524. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4525. {
  4526. TForm * Form = dynamic_cast<TForm *>(Sender);
  4527. delete Form;
  4528. }
  4529. //---------------------------------------------------------------------------
  4530. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4531. {
  4532. while (FLocalEditors->Count > 0)
  4533. {
  4534. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4535. }
  4536. }
  4537. //---------------------------------------------------------------------------
  4538. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4539. TObject *Sender)
  4540. {
  4541. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4542. try
  4543. {
  4544. SetProperties(osRemote, FileList);
  4545. }
  4546. __finally
  4547. {
  4548. delete FileList;
  4549. }
  4550. }
  4551. //---------------------------------------------------------------------------
  4552. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4553. {
  4554. if (value)
  4555. {
  4556. if (Component == fcCustomCommandsBand)
  4557. {
  4558. UpdateCustomCommandsToolbar();
  4559. }
  4560. }
  4561. }
  4562. //---------------------------------------------------------------------------
  4563. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4564. {
  4565. TControl * Control = GetComponent(Component);
  4566. DebugAssert(Control);
  4567. bool Changed = (Control->Visible != value);
  4568. if (Changed)
  4569. {
  4570. ComponentShowing(Component, value);
  4571. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4572. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4573. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4574. if (value)
  4575. {
  4576. int RemainingHeight = Control->Parent->ClientHeight;
  4577. int RemainingWidth = Control->Parent->ClientWidth;
  4578. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4579. {
  4580. TControl * ChildControl = Control->Parent->Controls[i];
  4581. if (ChildControl->Visible)
  4582. {
  4583. switch (ChildControl->Align)
  4584. {
  4585. case alTop:
  4586. case alBottom:
  4587. RemainingHeight -= ChildControl->Height;
  4588. break;
  4589. case alLeft:
  4590. case alRight:
  4591. RemainingWidth -= ChildControl->Width;
  4592. break;
  4593. }
  4594. }
  4595. }
  4596. int Reserve = ScaleByTextHeight(this, 32);
  4597. // queue in explorer, trees in commander
  4598. if (Control->Height > RemainingHeight - Reserve)
  4599. {
  4600. Control->Height = RemainingHeight / 2;
  4601. }
  4602. if (Control->Width > RemainingWidth - Reserve)
  4603. {
  4604. Control->Width = RemainingWidth / 2;
  4605. }
  4606. }
  4607. Control->Visible = value;
  4608. if (WasFocused && Visible)
  4609. {
  4610. DirView(osCurrent)->SetFocus();
  4611. }
  4612. FixControlsPlacement();
  4613. }
  4614. }
  4615. //---------------------------------------------------------------------------
  4616. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4617. {
  4618. TControl * Control = GetComponent(Component);
  4619. if (Control == NULL)
  4620. {
  4621. return false;
  4622. }
  4623. else
  4624. {
  4625. return Control->Visible;
  4626. }
  4627. }
  4628. //---------------------------------------------------------------------------
  4629. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4630. {
  4631. bool Result;
  4632. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4633. {
  4634. Result = !IsUWP();
  4635. }
  4636. else
  4637. {
  4638. Result = true;
  4639. }
  4640. return Result;
  4641. }
  4642. //---------------------------------------------------------------------------
  4643. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4644. {
  4645. if (RemoteDirView->ItemFocused != NULL)
  4646. {
  4647. RemoteDirView->ItemFocused->MakeVisible(false);
  4648. }
  4649. QueueSplitter->Visible = QueuePanel->Visible;
  4650. RemotePanelSplitter->Visible = RemoteDriveView->Visible;
  4651. }
  4652. //---------------------------------------------------------------------------
  4653. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4654. {
  4655. switch (Component) {
  4656. case fcStatusBar: return RemoteStatusBar;
  4657. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4658. case fcQueueView: return QueuePanel;
  4659. case fcQueueToolbar: return QueueDock;
  4660. case fcRemoteTree: return RemoteDriveView;
  4661. case fcSessionsTabs: return SessionsPageControl;
  4662. default: return NULL;
  4663. }
  4664. }
  4665. //---------------------------------------------------------------------------
  4666. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4667. TObject *Sender, TListColumn *Column, TPoint &Point)
  4668. {
  4669. DebugAssert(NonVisualDataModule && Column && Sender);
  4670. NonVisualDataModule->ListColumn = Column;
  4671. TPopupMenu * DirViewColumnMenu;
  4672. if (Sender == RemoteDirView)
  4673. {
  4674. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4675. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4676. (Column->Index == uvName);
  4677. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4678. (Column->Index == uvSize);
  4679. }
  4680. else
  4681. {
  4682. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4683. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4684. (Column->Index == dvName);
  4685. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4686. (Column->Index == dvSize);
  4687. }
  4688. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  4689. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  4690. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  4691. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4692. }
  4693. //---------------------------------------------------------------------------
  4694. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4695. TObject *Sender, TListItem *Item, bool &AllowExec)
  4696. {
  4697. DoDirViewExecFile(Sender, Item, AllowExec);
  4698. }
  4699. //---------------------------------------------------------------------------
  4700. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4701. TListItem * Item, bool & AllowExec)
  4702. {
  4703. DebugAssert(Sender && Item && Configuration);
  4704. DebugAssert(AllowExec);
  4705. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  4706. bool Remote = (ADirView == DirView(osRemote));
  4707. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks;
  4708. TOperationSide Side = (Remote ? osRemote : osLocal);
  4709. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  4710. // on files only (not directories)
  4711. // and only when symlinks are resolved (apply to remote panel only)
  4712. if (!ADirView->ItemIsDirectory(Item) &&
  4713. (ResolvedSymlinks || FForceExecution || Terminal->IsEncryptingFiles()))
  4714. {
  4715. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  4716. !FForceExecution &&
  4717. (ResolvedSymlinks || Terminal->IsEncryptingFiles()))
  4718. {
  4719. if (WinConfiguration->DoubleClickAction == dcaCopy)
  4720. {
  4721. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  4722. UnlockWindow();
  4723. try
  4724. {
  4725. ExecuteFileOperation(
  4726. foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  4727. }
  4728. __finally
  4729. {
  4730. LockWindow();
  4731. }
  4732. AllowExec = false;
  4733. }
  4734. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  4735. {
  4736. if (!Remote || !WinConfiguration->DisableOpenEdit)
  4737. {
  4738. ExecuteFile(Side, efDefaultEditor);
  4739. AllowExec = false;
  4740. }
  4741. }
  4742. else
  4743. {
  4744. DebugFail();
  4745. }
  4746. }
  4747. // if we have not done anything special, fall back to default behavior
  4748. if (AllowExec)
  4749. {
  4750. if (Remote && !WinConfiguration->DisableOpenEdit)
  4751. {
  4752. ExecuteFile(osRemote, efShell);
  4753. AllowExec = false;
  4754. }
  4755. }
  4756. }
  4757. }
  4758. //---------------------------------------------------------------------------
  4759. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  4760. {
  4761. return ((Side == osRemote) || (Side == osCurrent));
  4762. }
  4763. //---------------------------------------------------------------------------
  4764. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  4765. {
  4766. DebugFail();
  4767. }
  4768. //---------------------------------------------------------------------------
  4769. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  4770. {
  4771. DebugFail();
  4772. }
  4773. //---------------------------------------------------------------------------
  4774. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  4775. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, bool UseDefaults)
  4776. {
  4777. TSynchronizeParamType Params;
  4778. Params.LocalDirectory = LocalDirectory;
  4779. Params.RemoteDirectory = RemoteDirectory;
  4780. int UnusedParams =
  4781. (GUIConfiguration->SynchronizeParams &
  4782. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize));
  4783. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  4784. Params.Options = GUIConfiguration->SynchronizeOptions;
  4785. bool SaveSettings = false;
  4786. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  4787. &DoSynchronizeTooManyDirectories);
  4788. DebugAssert(FSynchronizeController == NULL);
  4789. FSynchronizeController = &Controller;
  4790. bool Result;
  4791. try
  4792. {
  4793. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4794. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  4795. int Options =
  4796. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  4797. DebugAssert(FOnFeedSynchronizeError == NULL);
  4798. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  4799. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  4800. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  4801. if (Result)
  4802. {
  4803. if (SaveSettings)
  4804. {
  4805. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  4806. GUIConfiguration->SynchronizeOptions = Params.Options;
  4807. }
  4808. else
  4809. {
  4810. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  4811. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  4812. FLAGSET(Params.Options, soSynchronize))
  4813. {
  4814. GUIConfiguration->SynchronizeOptions =
  4815. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  4816. soSynchronize;
  4817. }
  4818. }
  4819. LocalDirectory = Params.LocalDirectory;
  4820. RemoteDirectory = Params.RemoteDirectory;
  4821. }
  4822. }
  4823. __finally
  4824. {
  4825. FSynchronizeController = NULL;
  4826. DebugAssert(FOnFeedSynchronizeError == NULL);
  4827. FOnFeedSynchronizeError = NULL;
  4828. }
  4829. return Result;
  4830. }
  4831. //---------------------------------------------------------------------------
  4832. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  4833. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  4834. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  4835. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  4836. TSynchronizeOptions * Options, bool Full)
  4837. {
  4838. // Keep-up-to-date mode
  4839. if (Terminal->Status == ssOpened)
  4840. {
  4841. try
  4842. {
  4843. int PParams = Params.Params;
  4844. if (!Full)
  4845. {
  4846. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  4847. if (FLAGSET(Params.Options, soRecurse))
  4848. {
  4849. PParams |= TTerminal::spSubDirs;
  4850. }
  4851. }
  4852. else
  4853. {
  4854. // if keepuptodate is non-recursive,
  4855. // full sync before has to be non-recursive as well
  4856. if (FLAGCLEAR(Params.Options, soRecurse))
  4857. {
  4858. PParams |= TTerminal::spNoRecurse;
  4859. }
  4860. }
  4861. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  4862. PParams, Checklist, Options);
  4863. }
  4864. catch (Exception & E)
  4865. {
  4866. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  4867. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  4868. {
  4869. // noop, already logged in MoreMessageDialog
  4870. }
  4871. else
  4872. {
  4873. ShowExtendedExceptionEx(Terminal, &E);
  4874. throw;
  4875. }
  4876. }
  4877. }
  4878. }
  4879. //---------------------------------------------------------------------------
  4880. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  4881. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  4882. const UnicodeString ErrorStr)
  4883. {
  4884. if (!Directory.IsEmpty())
  4885. {
  4886. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  4887. }
  4888. else
  4889. {
  4890. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  4891. }
  4892. }
  4893. //---------------------------------------------------------------------------
  4894. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  4895. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  4896. {
  4897. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  4898. {
  4899. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  4900. }
  4901. else
  4902. {
  4903. TMessageParams Params(mpNeverAskAgainCheck);
  4904. unsigned int Result = MessageDialog(
  4905. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  4906. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  4907. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  4908. {
  4909. MaxDirectories *= 2;
  4910. if (Result == qaNeverAskAgain)
  4911. {
  4912. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  4913. }
  4914. }
  4915. else
  4916. {
  4917. Abort();
  4918. }
  4919. }
  4920. }
  4921. //---------------------------------------------------------------------------
  4922. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  4923. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  4924. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  4925. TSynchronizeOptions * Options)
  4926. {
  4927. // Keep-up-to-date mode
  4928. DebugAssert(!FAutoOperation);
  4929. void * BatchStorage;
  4930. BatchStart(BatchStorage);
  4931. FAutoOperation = true;
  4932. bool AnyOperation = false;
  4933. TDateTime StartTime = Now();
  4934. TSynchronizeChecklist * AChecklist = NULL;
  4935. try
  4936. {
  4937. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  4938. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  4939. {
  4940. FSynchronizeProgressForm->Start();
  4941. }
  4942. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  4943. static_cast<TTerminal::TSynchronizeMode>(Mode),
  4944. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  4945. Options);
  4946. SAFE_DESTROY(FSynchronizeProgressForm);
  4947. AnyOperation = (AChecklist->CheckedCount > 0);
  4948. if (AnyOperation)
  4949. {
  4950. TSynchronizeProgress SynchronizeProgress(AChecklist);
  4951. CreateProgressForm(&SynchronizeProgress);
  4952. try
  4953. {
  4954. Terminal->SynchronizeApply(
  4955. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  4956. SynchronizeProcessedItem, NULL, NULL, NULL);
  4957. }
  4958. __finally
  4959. {
  4960. DestroyProgressForm();
  4961. }
  4962. }
  4963. }
  4964. __finally
  4965. {
  4966. if (Checklist == NULL)
  4967. {
  4968. delete AChecklist;
  4969. }
  4970. else
  4971. {
  4972. *Checklist = AChecklist;
  4973. }
  4974. FAutoOperation = false;
  4975. SAFE_DESTROY(FSynchronizeProgressForm);
  4976. BatchEnd(BatchStorage);
  4977. ReloadLocalDirectory();
  4978. if (AnyOperation)
  4979. {
  4980. OperationComplete(StartTime);
  4981. }
  4982. }
  4983. }
  4984. //---------------------------------------------------------------------------
  4985. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  4986. {
  4987. // can be called from command line
  4988. return Visible &&
  4989. ((DirView(osRemote)->SelCount > 0) ||
  4990. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  4991. }
  4992. //---------------------------------------------------------------------------
  4993. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  4994. {
  4995. LogSynchronizeEvent(Terminal, Message);
  4996. }
  4997. //---------------------------------------------------------------------------
  4998. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  4999. int Params, TSynchronizeOptions & Options)
  5000. {
  5001. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5002. {
  5003. Options.Filter = new TStringList();
  5004. Options.Filter->CaseSensitive = false;
  5005. Options.Filter->Duplicates = Types::dupAccept;
  5006. if (DirView(osRemote)->SelCount > 0)
  5007. {
  5008. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5009. }
  5010. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5011. {
  5012. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5013. }
  5014. Options.Filter->Sort();
  5015. }
  5016. }
  5017. //---------------------------------------------------------------------------
  5018. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5019. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5020. {
  5021. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5022. UnicodeString AdditionalParams =
  5023. FORMAT(L"%s \"%s\" \"%s\" %d %d %s", (
  5024. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5025. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options,
  5026. TProgramParams::FormatSwitch(DEFAULTS_SWITCH)));
  5027. TCopyParamType Defaults;
  5028. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5029. ConfigStorage->AccessMode = smRead;
  5030. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false, false))
  5031. {
  5032. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5033. }
  5034. ConfigStorage.reset(NULL);
  5035. std::unique_ptr<TStringList> Options(new TStringList());
  5036. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5037. GUIConfiguration->SaveCopyParam(OptionsStorage.get(), CopyParams, &Defaults);
  5038. if (Options->Count > 0)
  5039. {
  5040. AdditionalParams +=
  5041. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAW_CONFIG_SWITCH), StringsToParams(Options.get())));
  5042. }
  5043. ExecuteNewInstance(SessionName, AdditionalParams);
  5044. }
  5045. //---------------------------------------------------------------------------
  5046. struct TSynchronizeParams
  5047. {
  5048. UnicodeString LocalDirectory;
  5049. UnicodeString RemoteDirectory;
  5050. TSynchronizeMode Mode;
  5051. TSynchronizeChecklist * Checklist;
  5052. int Params;
  5053. TCopyParamType * CopyParam;
  5054. TDateTime CollectElapsed;
  5055. TDateTime * StartTime;
  5056. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5057. };
  5058. //---------------------------------------------------------------------------
  5059. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5060. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5061. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5062. {
  5063. DebugAssert(!FAutoOperation);
  5064. void * BatchStorage;
  5065. BatchStart(BatchStorage);
  5066. FAutoOperation = true;
  5067. TFileOperationStatistics Statistics;
  5068. TDateTime Start = Now();
  5069. try
  5070. {
  5071. Params.OnProcessedItem = OnProcessedItem;
  5072. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5073. CreateProgressForm(&SynchronizeProgress);
  5074. Terminal->SynchronizeApply(
  5075. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5076. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5077. &Statistics);
  5078. }
  5079. __finally
  5080. {
  5081. Params.OnProcessedItem = NULL;
  5082. FAutoOperation = false;
  5083. DestroyProgressForm();
  5084. BatchEnd(BatchStorage);
  5085. ReloadLocalDirectory();
  5086. }
  5087. if (WinConfiguration->SynchronizeSummary)
  5088. {
  5089. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5090. // The statistics should be 0 anyway in this case
  5091. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5092. {
  5093. Message += L"\n";
  5094. if (Statistics.FilesUploaded > 0)
  5095. {
  5096. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5097. }
  5098. if (Statistics.FilesDownloaded > 0)
  5099. {
  5100. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5101. }
  5102. if (Statistics.FilesDeletedLocal > 0)
  5103. {
  5104. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5105. }
  5106. if (Statistics.FilesDeletedRemote > 0)
  5107. {
  5108. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5109. }
  5110. }
  5111. TDateTime Elapsed = (Now() - Start);
  5112. Message +=
  5113. L"\n" +
  5114. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5115. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5116. TMessageParams Params(mpNeverAskAgainCheck);
  5117. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5118. if (Result == qaNeverAskAgain)
  5119. {
  5120. WinConfiguration->SynchronizeSummary = false;
  5121. }
  5122. }
  5123. }
  5124. //---------------------------------------------------------------------------
  5125. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5126. {
  5127. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5128. {
  5129. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5130. }
  5131. // Not set in keep-up-to-date mode - Synchronize() method
  5132. if (Token != NULL)
  5133. {
  5134. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5135. if (Params.OnProcessedItem != NULL)
  5136. {
  5137. Params.OnProcessedItem(NULL, ChecklistItem);
  5138. }
  5139. }
  5140. }
  5141. //---------------------------------------------------------------------------
  5142. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5143. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5144. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5145. {
  5146. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5147. *Params.StartTime = Now();
  5148. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5149. }
  5150. //---------------------------------------------------------------------------
  5151. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5152. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5153. {
  5154. // terminal can be already closed (e.g. dropped connection)
  5155. if (Terminal != NULL)
  5156. {
  5157. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5158. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5159. }
  5160. }
  5161. //---------------------------------------------------------------------------
  5162. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5163. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5164. {
  5165. TAutoBatch AutoBatch(this);
  5166. TAutoFlag AutoOperationFlag(FAutoOperation);
  5167. if (Side == osRemote)
  5168. {
  5169. std::unique_ptr<TStrings> FileList(new TStringList());
  5170. FileList->AddObject(FileName, RemoteFile);
  5171. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5172. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5173. RemoteDirView->SaveSelection();
  5174. RemoteDirView->SaveSelectedNames();
  5175. try
  5176. {
  5177. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5178. }
  5179. catch(...)
  5180. {
  5181. RemoteDirView->DiscardSavedSelection();
  5182. throw;
  5183. }
  5184. RemoteDirView->RestoreSelection();
  5185. }
  5186. else if (DebugAlwaysTrue(Side == osLocal))
  5187. {
  5188. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5189. {
  5190. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5191. }
  5192. UnicodeString Directory = ExtractFileDir(FileName);
  5193. ReloadLocalDirectory(Directory);
  5194. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5195. if (!SamePaths(Directory, NewDirectory))
  5196. {
  5197. ReloadLocalDirectory(NewDirectory);
  5198. }
  5199. }
  5200. }
  5201. //---------------------------------------------------------------------------
  5202. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5203. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5204. TSynchronizeMode & Mode, bool & SaveMode, bool UseDefaults)
  5205. {
  5206. int Result;
  5207. int Params = GUIConfiguration->SynchronizeParams;
  5208. bool SaveSettings = false;
  5209. int Options =
  5210. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5211. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5212. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5213. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5214. bool Continue =
  5215. UseDefaults ||
  5216. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5217. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs);
  5218. if (Continue)
  5219. {
  5220. Configuration->Usage->Inc(L"Synchronizations");
  5221. UpdateCopyParamCounters(CopyParam);
  5222. TSynchronizeOptions SynchronizeOptions;
  5223. GetSynchronizeOptions(Params, SynchronizeOptions);
  5224. if (SaveSettings)
  5225. {
  5226. GUIConfiguration->SynchronizeParams = Params;
  5227. }
  5228. else
  5229. {
  5230. SaveMode = false;
  5231. }
  5232. TDateTime StartTime = Now();
  5233. TSynchronizeChecklist * Checklist = NULL;
  5234. try
  5235. {
  5236. DebugAssert(!FAutoOperation);
  5237. FAutoOperation = true;
  5238. try
  5239. {
  5240. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  5241. FSynchronizeProgressForm->Start();
  5242. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5243. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5244. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5245. &SynchronizeOptions);
  5246. }
  5247. __finally
  5248. {
  5249. FAutoOperation = false;
  5250. SAFE_DESTROY(FSynchronizeProgressForm);
  5251. }
  5252. TSynchronizeParams SynchronizeParams;
  5253. SynchronizeParams.LocalDirectory = LocalDirectory;
  5254. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5255. SynchronizeParams.Mode = Mode;
  5256. SynchronizeParams.CopyParam = &CopyParam;
  5257. SynchronizeParams.Params = Params;
  5258. SynchronizeParams.Checklist = Checklist;
  5259. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5260. SynchronizeParams.StartTime = &StartTime;
  5261. SynchronizeParams.OnProcessedItem = NULL;
  5262. Result = Checklist->Count;
  5263. if (Checklist->Count > 0)
  5264. {
  5265. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5266. {
  5267. if (!DoSynchronizeChecklistDialog(
  5268. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5269. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, &SynchronizeParams))
  5270. {
  5271. Result = -1;
  5272. }
  5273. }
  5274. else
  5275. {
  5276. FullSynchronize(SynchronizeParams, NULL, NULL);
  5277. }
  5278. }
  5279. else
  5280. {
  5281. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5282. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5283. }
  5284. }
  5285. __finally
  5286. {
  5287. delete Checklist;
  5288. }
  5289. OperationComplete(StartTime);
  5290. }
  5291. else
  5292. {
  5293. Result = -1;
  5294. }
  5295. return Result;
  5296. }
  5297. //---------------------------------------------------------------------------
  5298. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5299. const UnicodeString LocalDirectory, const UnicodeString RemoteDirectory,
  5300. bool & Continue, bool Collect)
  5301. {
  5302. if (Collect)
  5303. {
  5304. DebugAssert(FSynchronizeProgressForm != NULL);
  5305. if (!FSynchronizeProgressForm->Started)
  5306. {
  5307. FSynchronizeProgressForm->Start();
  5308. }
  5309. FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Continue);
  5310. }
  5311. else
  5312. {
  5313. DebugAssert(FSynchronizeProgressForm == NULL);
  5314. DebugAssert(FProgressForm != NULL);
  5315. }
  5316. }
  5317. //---------------------------------------------------------------------------
  5318. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5319. {
  5320. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  5321. TRemoteFile * File = NULL;
  5322. Terminal->ReadFile(FullFileName, File);
  5323. if (File != NULL)
  5324. {
  5325. std::unique_ptr<TRemoteFile> FileOwner(File);
  5326. TAutoFlag Flag(FStandaloneEditing);
  5327. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5328. Application->ShowMainForm = false;
  5329. Application->Run();
  5330. }
  5331. }
  5332. //---------------------------------------------------------------------------
  5333. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  5334. {
  5335. DebugFail();
  5336. }
  5337. //---------------------------------------------------------------------------
  5338. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5339. {
  5340. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5341. SessionData->Assign(Terminal->SessionData);
  5342. UpdateSessionData(SessionData.get());
  5343. Terminal->UpdateSessionCredentials(SessionData.get());
  5344. if (!Terminal->SessionData->HasSessionName())
  5345. {
  5346. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5347. // once IsWorkspace is cleared
  5348. SessionData->Name = UnicodeString();
  5349. SessionData->IsWorkspace = false;
  5350. }
  5351. return SessionData.release();
  5352. }
  5353. //---------------------------------------------------------------------------
  5354. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5355. {
  5356. TSessionData * SessionData = CloneCurrentSessionData();
  5357. try
  5358. {
  5359. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5360. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5361. }
  5362. __finally
  5363. {
  5364. delete SessionData;
  5365. }
  5366. }
  5367. //---------------------------------------------------------------------------
  5368. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5369. {
  5370. TTerminalManager * Manager = TTerminalManager::Instance();
  5371. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5372. if (DebugAlwaysTrue(Terminal != NULL))
  5373. {
  5374. // Update (Managed)Terminal->StateData
  5375. UpdateTerminal(Terminal);
  5376. }
  5377. Manager->SaveWorkspace(DataList.get());
  5378. return DataList.release();
  5379. }
  5380. //---------------------------------------------------------------------------
  5381. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5382. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5383. {
  5384. WinConfiguration->LastWorkspace = Name;
  5385. if (!SavePasswords)
  5386. {
  5387. for (int Index = 0; Index < DataList->Count; Index++)
  5388. {
  5389. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5390. if (SessionData->Link.IsEmpty())
  5391. {
  5392. SessionData->ClearSessionPasswords();
  5393. }
  5394. }
  5395. }
  5396. StoredSessions->NewWorkspace(Name, DataList);
  5397. // modified only
  5398. StoredSessions->Save(false, Explicit);
  5399. }
  5400. //---------------------------------------------------------------------------
  5401. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5402. {
  5403. return
  5404. DefaultStr(WinConfiguration->LastWorkspace,
  5405. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5406. }
  5407. //---------------------------------------------------------------------------
  5408. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5409. {
  5410. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5411. bool AnyNonStoredSessionWithPassword = false;
  5412. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5413. bool AllNonStoredSessionsAnonymous = true;
  5414. TTerminalManager * Manager = TTerminalManager::Instance();
  5415. for (int Index = 0; Index < DataList->Count; Index++)
  5416. {
  5417. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5418. if (SessionData->Link.IsEmpty())
  5419. {
  5420. if (SessionData->HasAnySessionPassword())
  5421. {
  5422. AnyNonStoredSessionWithPassword = true;
  5423. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  5424. {
  5425. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5426. }
  5427. }
  5428. if (!SameText(SessionData->UserName, AnonymousUserName))
  5429. {
  5430. AllNonStoredSessionsAnonymous = false;
  5431. }
  5432. }
  5433. }
  5434. bool SavePasswords;
  5435. bool * PSavePasswords;
  5436. bool NotRecommendedSavingPasswords =
  5437. !CustomWinConfiguration->UseMasterPassword &&
  5438. !AllNonStoredSessionsAnonymous;
  5439. if (Configuration->DisablePasswordStoring ||
  5440. !AnyNonStoredSessionWithPassword)
  5441. {
  5442. PSavePasswords = NULL;
  5443. SavePasswords = false;
  5444. }
  5445. else
  5446. {
  5447. PSavePasswords = &SavePasswords;
  5448. SavePasswords =
  5449. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5450. !NotRecommendedSavingPasswords;
  5451. }
  5452. UnicodeString Name = WorkspaceName();
  5453. bool CreateShortcut = false;
  5454. bool Result =
  5455. DoSaveWorkspaceDialog(
  5456. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5457. EnableAutoSave);
  5458. if (Result)
  5459. {
  5460. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5461. if (CreateShortcut)
  5462. {
  5463. TOperationVisualizer Visualizer;
  5464. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5465. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5466. }
  5467. if (EnableAutoSave)
  5468. {
  5469. WinConfiguration->AutoSaveWorkspace = true;
  5470. WinConfiguration->AutoWorkspace = Name;
  5471. if (PSavePasswords != NULL)
  5472. {
  5473. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5474. }
  5475. }
  5476. }
  5477. return Result;
  5478. }
  5479. //---------------------------------------------------------------------------
  5480. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TTerminal * Terminal)
  5481. {
  5482. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  5483. DebugAssert(ManagedTerminal != NULL);
  5484. SAFE_DESTROY(ManagedTerminal->RemoteExplorerState);
  5485. if (WinConfiguration->PreservePanelState)
  5486. {
  5487. ManagedTerminal->RemoteExplorerState = RemoteDirView->SaveState();
  5488. }
  5489. UpdateSessionData(ManagedTerminal->StateData);
  5490. }
  5491. //---------------------------------------------------------------------------
  5492. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5493. {
  5494. // Keep in sync with TSessionData::CopyStateData
  5495. DebugAssert(Data != NULL);
  5496. // This is inconsistent with how color (for example) is handled.
  5497. Data->Name = Terminal->SessionData->Name;
  5498. // cannot use RemoteDirView->Path, because it is empty if connection
  5499. // was already closed
  5500. // also only peek, we may not be connected at all atm
  5501. // so make sure we do not try retrieving current directory from the server
  5502. // (particularly with FTP)
  5503. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5504. if (!ACurrentDirectory.IsEmpty())
  5505. {
  5506. Data->RemoteDirectory = ACurrentDirectory;
  5507. }
  5508. Data->Color = SessionColor;
  5509. }
  5510. //---------------------------------------------------------------------------
  5511. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5512. {
  5513. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5514. DebugAssert(Toolbar != NULL);
  5515. for (int i = 0; i < Toolbar->Items->Count; i++)
  5516. {
  5517. TTBXCustomDropDownItem * DropDownItem;
  5518. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5519. if (DropDownItem != NULL)
  5520. {
  5521. ToolbarItemResize(DropDownItem,
  5522. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5523. Toolbar->NonClientWidth);
  5524. break;
  5525. }
  5526. }
  5527. }
  5528. //---------------------------------------------------------------------------
  5529. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5530. {
  5531. Item->EditWidth = Width;
  5532. }
  5533. //---------------------------------------------------------------------------
  5534. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5535. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5536. {
  5537. for (int i = 0; i < Toolbar->Items->Count; i++)
  5538. {
  5539. TTBXCustomDropDownItem * DropDownItem;
  5540. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5541. if (DropDownItem != NULL)
  5542. {
  5543. ASize.x -= DropDownItem->EditWidth;
  5544. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5545. }
  5546. }
  5547. }
  5548. //---------------------------------------------------------------------------
  5549. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5550. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5551. {
  5552. if (WinConfiguration->DDWarnLackOfTempSpace)
  5553. {
  5554. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5555. if (!ADrive.IsEmpty())
  5556. {
  5557. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5558. DebugAssert(RequiredSpace >= 0);
  5559. __int64 RequiredWithReserve;
  5560. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5561. if (FreeSpace < RequiredWithReserve)
  5562. {
  5563. unsigned int Result;
  5564. TMessageParams Params(mpNeverAskAgainCheck);
  5565. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5566. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5567. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5568. if (Result == qaNeverAskAgain)
  5569. {
  5570. WinConfiguration->DDWarnLackOfTempSpace = false;
  5571. }
  5572. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5573. }
  5574. }
  5575. }
  5576. }
  5577. //---------------------------------------------------------------------------
  5578. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5579. {
  5580. DoOpenDirectoryDialog(odAddBookmark, Side);
  5581. }
  5582. //---------------------------------------------------------------------------
  5583. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5584. {
  5585. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5586. TStringList * VisitedDirectories = CreateSortedStringList((Side == osRemote));
  5587. try
  5588. {
  5589. TCustomDirView * DView = DirView(Side);
  5590. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5591. {
  5592. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5593. }
  5594. }
  5595. catch (...)
  5596. {
  5597. delete VisitedDirectories;
  5598. throw;
  5599. }
  5600. return VisitedDirectories;
  5601. }
  5602. //---------------------------------------------------------------------------
  5603. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5604. TOpenDirectoryMode Mode, TOperationSide Side)
  5605. {
  5606. bool Continue = true;
  5607. if (Mode == odAddBookmark)
  5608. {
  5609. TMessageParams Params(mpNeverAskAgainCheck);
  5610. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5611. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5612. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  5613. unsigned int Answer =
  5614. MessageDialog(Message,
  5615. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  5616. if (Answer == qaNeverAskAgain)
  5617. {
  5618. Continue = true;
  5619. WinConfiguration->UseSharedBookmarks = true;
  5620. }
  5621. else if (Answer == qaOK)
  5622. {
  5623. Continue = true;
  5624. WinConfiguration->UseSharedBookmarks = false;
  5625. }
  5626. else
  5627. {
  5628. Continue = false;
  5629. }
  5630. }
  5631. if (Continue)
  5632. {
  5633. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  5634. try
  5635. {
  5636. UnicodeString Name = DirView(Side)->PathName;
  5637. bool Repeat;
  5638. do
  5639. {
  5640. Repeat = false;
  5641. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  5642. // do not allow switching to location profiles,
  5643. // if we are not connected
  5644. HasDirView[osLocal] && (Terminal != NULL)))
  5645. {
  5646. TWindowLock Lock(this);
  5647. if (!TryOpenDirectory(Side, Name))
  5648. {
  5649. Repeat = true;
  5650. Mode = odBrowse;
  5651. }
  5652. }
  5653. }
  5654. while (Repeat);
  5655. }
  5656. __finally
  5657. {
  5658. delete VisitedDirectories;
  5659. }
  5660. }
  5661. }
  5662. //---------------------------------------------------------------------------
  5663. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  5664. {
  5665. DebugAssert(!FTerminal->CommandSessionOpened);
  5666. return TTerminalManager::Instance()->ConnectTerminal(FTerminal->CommandSession);
  5667. }
  5668. //---------------------------------------------------------------------------
  5669. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  5670. {
  5671. bool Result = FTerminal->IsCapable[Capability] ||
  5672. FTerminal->CommandSessionOpened;
  5673. if (!Result)
  5674. {
  5675. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  5676. if (!GUIConfiguration->ConfirmCommandSession)
  5677. {
  5678. Result = true;
  5679. }
  5680. else
  5681. {
  5682. TMessageParams Params(mpNeverAskAgainCheck);
  5683. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  5684. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  5685. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  5686. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  5687. if (Answer == qaNeverAskAgain)
  5688. {
  5689. GUIConfiguration->ConfirmCommandSession = false;
  5690. Result = true;
  5691. }
  5692. else if (Answer == qaOK)
  5693. {
  5694. Result = true;
  5695. }
  5696. }
  5697. if (Result)
  5698. {
  5699. Result = CommandSessionFallback();
  5700. }
  5701. }
  5702. return Result;
  5703. }
  5704. //---------------------------------------------------------------------------
  5705. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  5706. {
  5707. if (EnsureCommandSessionFallback(fcAnyCommand))
  5708. {
  5709. DoConsoleDialog(Terminal, Command);
  5710. }
  5711. }
  5712. //---------------------------------------------------------------------------
  5713. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  5714. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  5715. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  5716. {
  5717. if (IsFileControl(DropSourceControl, osRemote) &&
  5718. (FDDExtMapFile != NULL))
  5719. {
  5720. Accept = true;
  5721. }
  5722. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  5723. DebugAssert(FDDTargetControl != NULL);
  5724. }
  5725. //---------------------------------------------------------------------------
  5726. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  5727. _di_IDataObject DataObj, int KeyState,
  5728. const TPoint & Point, int & Effect, bool & Accept)
  5729. {
  5730. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  5731. }
  5732. //---------------------------------------------------------------------------
  5733. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  5734. _di_IDataObject DataObj, int KeyState,
  5735. const TPoint & Point, int & Effect, bool & Accept)
  5736. {
  5737. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  5738. }
  5739. //---------------------------------------------------------------------------
  5740. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  5741. TObject * Sender)
  5742. {
  5743. DebugUsedParam(Sender);
  5744. DebugAssert(FDDTargetControl == Sender);
  5745. FDDTargetControl = NULL;
  5746. }
  5747. //---------------------------------------------------------------------------
  5748. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  5749. {
  5750. FileControlDDDragLeave(SessionsPageControl);
  5751. }
  5752. //---------------------------------------------------------------------------
  5753. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  5754. {
  5755. FileControlDDDragLeave(QueueView3);
  5756. }
  5757. //---------------------------------------------------------------------------
  5758. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  5759. {
  5760. DebugAssert(GetSide(Side) == osRemote);
  5761. DebugUsedParam(Side);
  5762. bool Edit = false;
  5763. TRemoteFile * File = NULL;
  5764. UnicodeString FileName;
  5765. UnicodeString PointTo;
  5766. bool SymbolicLink = true;
  5767. if (RemoteDirView->ItemFocused)
  5768. {
  5769. DebugAssert(RemoteDirView->ItemFocused->Data);
  5770. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  5771. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  5772. if (Edit)
  5773. {
  5774. FileName = File->FileName;
  5775. PointTo = File->LinkTo;
  5776. }
  5777. else
  5778. {
  5779. PointTo = File->FileName;
  5780. }
  5781. }
  5782. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  5783. Terminal->IsCapable[fcHardLink]))
  5784. {
  5785. Configuration->Usage->Inc(L"RemoteLinksCreated");
  5786. if (Edit)
  5787. {
  5788. DebugAssert(File->FileName == FileName);
  5789. int Params = dfNoRecursive;
  5790. Terminal->ExceptionOnFail = true;
  5791. try
  5792. {
  5793. Terminal->DeleteFile(L"", File, &Params);
  5794. }
  5795. __finally
  5796. {
  5797. Terminal->ExceptionOnFail = false;
  5798. }
  5799. }
  5800. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  5801. }
  5802. }
  5803. //---------------------------------------------------------------------------
  5804. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  5805. {
  5806. return
  5807. (Terminal != NULL) &&
  5808. ((GetSide(Side) != osRemote) ||
  5809. (Terminal->ResolvingSymlinks &&
  5810. Terminal->IsCapable[fcSymbolicLink]));
  5811. }
  5812. //---------------------------------------------------------------------------
  5813. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  5814. {
  5815. return
  5816. (FCurrentSide == osRemote) &&
  5817. (RemoteDirView->ItemFocused != NULL) &&
  5818. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  5819. Terminal->SessionData->ResolveSymlinks;
  5820. }
  5821. //---------------------------------------------------------------------------
  5822. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  5823. {
  5824. DebugAssert(!WinConfiguration->DisableOpenEdit);
  5825. FForceExecution = true;
  5826. try
  5827. {
  5828. DirView(osCurrent)->ExecuteCurrentFile();
  5829. }
  5830. __finally
  5831. {
  5832. FForceExecution = false;
  5833. }
  5834. }
  5835. //---------------------------------------------------------------------------
  5836. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  5837. {
  5838. TEditorData ExternalEditor;
  5839. ExternalEditor.Editor = edExternal;
  5840. bool Remember = false;
  5841. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc,
  5842. (GetSide(osCurrent) == osRemote)))
  5843. {
  5844. if (Remember)
  5845. {
  5846. TEditorList * EditorList = new TEditorList;
  5847. try
  5848. {
  5849. *EditorList = *WinConfiguration->EditorList;
  5850. bool Found = false;
  5851. int i = 0;
  5852. while (!Found && (i < EditorList->Count))
  5853. {
  5854. const TEditorPreferences * Editor = EditorList->Editors[i];
  5855. if ((Editor->Data->Editor == edExternal) &&
  5856. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  5857. {
  5858. Found = true;
  5859. }
  5860. i++;
  5861. }
  5862. if (!Found)
  5863. {
  5864. EditorList->Add(new TEditorPreferences(ExternalEditor));
  5865. WinConfiguration->EditorList = EditorList;
  5866. }
  5867. }
  5868. __finally
  5869. {
  5870. delete EditorList;
  5871. }
  5872. }
  5873. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  5874. }
  5875. }
  5876. //---------------------------------------------------------------------------
  5877. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  5878. {
  5879. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  5880. if (FFileFindTerminal == Sender)
  5881. {
  5882. FFileFindTerminal = NULL;
  5883. HideFileFindDialog();
  5884. }
  5885. if (FClipboardTerminal == Sender)
  5886. {
  5887. ClipboardClear(); // implies ClipboardStop
  5888. }
  5889. }
  5890. //---------------------------------------------------------------------------
  5891. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  5892. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  5893. {
  5894. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  5895. DebugAssert(Terminal != NULL);
  5896. if (Data->Terminal == Terminal)
  5897. {
  5898. Data->Terminal = NULL;
  5899. }
  5900. }
  5901. //---------------------------------------------------------------------------
  5902. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  5903. {
  5904. UpdateControls();
  5905. SessionColor = TColor(0);
  5906. UpdateRemotePathComboBox(false);
  5907. NeedSession(true);
  5908. }
  5909. //---------------------------------------------------------------------------
  5910. void __fastcall TCustomScpExplorerForm::NeedSession(bool ReloadSessions)
  5911. {
  5912. try
  5913. {
  5914. try
  5915. {
  5916. TTerminalManager::Instance()->NewSession(false, L"", ReloadSessions, this);
  5917. }
  5918. __finally
  5919. {
  5920. if (!WinConfiguration->KeepOpenWhenNoSession &&
  5921. (!Terminal || !Terminal->Active))
  5922. {
  5923. TerminateApplication();
  5924. }
  5925. }
  5926. }
  5927. catch (EAbort &)
  5928. {
  5929. // swallow
  5930. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  5931. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  5932. }
  5933. }
  5934. //---------------------------------------------------------------------------
  5935. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged()
  5936. {
  5937. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  5938. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  5939. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  5940. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  5941. try
  5942. {
  5943. while ((SessionsPageControl->PageCount > TerminalList->Count + 1) ||
  5944. // Clear the design time unthemed tab
  5945. ((SessionsPageControl->PageCount > 0) &&
  5946. (dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[SessionsPageControl->PageCount - 1]) == NULL)))
  5947. {
  5948. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  5949. }
  5950. for (int Index = 0; Index <= TerminalList->Count; Index++)
  5951. {
  5952. TThemeTabSheet * TabSheet;
  5953. if (Index >= SessionsPageControl->PageCount)
  5954. {
  5955. TabSheet = new TThemeTabSheet(SessionsPageControl);
  5956. TabSheet->PageControl = SessionsPageControl;
  5957. }
  5958. else
  5959. {
  5960. TabSheet = DebugNotNull(dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[Index]));
  5961. }
  5962. bool IsSessionTab = (Index < TerminalList->Count);
  5963. if (IsSessionTab)
  5964. {
  5965. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  5966. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  5967. UpdateSessionTab(TabSheet);
  5968. }
  5969. else
  5970. {
  5971. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  5972. TabSheet->Tag = 0; // not really needed
  5973. // 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)
  5974. UpdateNewSessionTab();
  5975. }
  5976. TabSheet->ShowCloseButton = IsSessionTab;
  5977. }
  5978. }
  5979. __finally
  5980. {
  5981. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  5982. }
  5983. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  5984. }
  5985. //---------------------------------------------------------------------------
  5986. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  5987. {
  5988. DoTerminalListChanged();
  5989. }
  5990. //---------------------------------------------------------------------------
  5991. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  5992. {
  5993. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  5994. DebugAssert(TabSheet->ImageIndex == 0);
  5995. TabSheet->Caption =
  5996. WinConfiguration->SelectiveToolbarText ?
  5997. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  5998. UnicodeString();
  5999. }
  6000. //---------------------------------------------------------------------------
  6001. TTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  6002. {
  6003. return reinterpret_cast<TTerminal *>(TabSheet->Tag);
  6004. }
  6005. //---------------------------------------------------------------------------
  6006. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  6007. {
  6008. if (DebugAlwaysTrue(TabSheet != NULL))
  6009. {
  6010. TManagedTerminal * ManagedTerminal =
  6011. dynamic_cast<TManagedTerminal *>(GetSessionTabTerminal(TabSheet));
  6012. if (DebugAlwaysTrue(ManagedTerminal != NULL))
  6013. {
  6014. TColor Color = (ManagedTerminal == FTerminal) ? FSessionColor : ManagedTerminal->StateData->Color;
  6015. TabSheet->ImageIndex = AddSessionColor(Color);
  6016. UnicodeString TabCaption = TTerminalManager::Instance()->GetTerminalTitle(ManagedTerminal, true);
  6017. TabSheet->Caption = SessionsPageControl->FormatCaptionWithCloseButton(TabCaption);
  6018. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  6019. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  6020. {
  6021. ThemeTabSheet->Shadowed = !ManagedTerminal->Active;
  6022. }
  6023. }
  6024. }
  6025. }
  6026. //---------------------------------------------------------------------------
  6027. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6028. {
  6029. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6030. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  6031. bool Result = (Terminal != NULL);
  6032. if (Result)
  6033. {
  6034. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6035. }
  6036. else
  6037. {
  6038. try
  6039. {
  6040. NewSession(false);
  6041. }
  6042. __finally
  6043. {
  6044. DoTerminalListChanged();
  6045. }
  6046. FSessionsPageControlNewSessionTime = Now();
  6047. }
  6048. return Result;
  6049. }
  6050. //---------------------------------------------------------------------------
  6051. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6052. {
  6053. SessionTabSwitched();
  6054. }
  6055. //---------------------------------------------------------------------------
  6056. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6057. {
  6058. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6059. DebugAssert(TransferList != NULL);
  6060. UnicodeString Name;
  6061. if (TransferList->ItemIndex <= 0)
  6062. {
  6063. Name = L"";
  6064. }
  6065. else
  6066. {
  6067. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6068. }
  6069. if (FCopyParamAutoSelected.IsEmpty())
  6070. {
  6071. // if previous preset was not autoselected, make new preset the "default"
  6072. FCopyParamDefault = Name;
  6073. }
  6074. GUIConfiguration->CopyParamCurrent = Name;
  6075. }
  6076. //---------------------------------------------------------------------------
  6077. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6078. {
  6079. // sanity check
  6080. bool ExistingPreset =
  6081. (FTransferListHoverIndex >= 0) &&
  6082. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6083. DebugAssert(ExistingPreset);
  6084. if (ExistingPreset)
  6085. {
  6086. HDC DC = GetDC(0);
  6087. TCanvas * Canvas = new TCanvas();
  6088. try
  6089. {
  6090. Canvas->Handle = DC;
  6091. Canvas->Font = GetToolbarFont(this);
  6092. UnicodeString Name;
  6093. if (FTransferListHoverIndex == 0)
  6094. {
  6095. Name = L"";
  6096. }
  6097. else
  6098. {
  6099. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6100. }
  6101. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6102. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6103. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6104. static_cast<TObject*>(GetComponent(fcTransferList)));
  6105. UnicodeString InfoStr =
  6106. GUIConfiguration->CopyParamPreset[Name].
  6107. GetInfoStr(L"; ",
  6108. FLAGMASK(Terminal != NULL, Terminal->UsableCopyParamAttrs(0).General));
  6109. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6110. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6111. {
  6112. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6113. {
  6114. InfoStr.SetLength(InfoStr.Length() - 1);
  6115. }
  6116. InfoStr += Ellipsis;
  6117. }
  6118. // UpdateCaption does not cause invalidation of whole submenu, while
  6119. // setting Caption property does.
  6120. // also it probably does not resize the label, even if necessary
  6121. // (we do not want that anyway)
  6122. TransferLabel->UpdateCaption(InfoStr);
  6123. }
  6124. __finally
  6125. {
  6126. Canvas->Handle = NULL;
  6127. ReleaseDC(0, DC);
  6128. delete Canvas;
  6129. }
  6130. }
  6131. }
  6132. //---------------------------------------------------------------------------
  6133. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6134. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6135. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6136. {
  6137. if (FTransferListHoverIndex != AHoverIndex)
  6138. {
  6139. FTransferListHoverIndex = AHoverIndex;
  6140. UpdateTransferLabel();
  6141. }
  6142. }
  6143. //---------------------------------------------------------------------------
  6144. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6145. {
  6146. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6147. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6148. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6149. {
  6150. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6151. {
  6152. OpenDirectory(GetSide(osCurrent));
  6153. Message.Result = 1;
  6154. }
  6155. else
  6156. {
  6157. TForm::Dispatch(&Message);
  6158. }
  6159. }
  6160. else
  6161. {
  6162. TForm::Dispatch(&Message);
  6163. }
  6164. }
  6165. //---------------------------------------------------------------------------
  6166. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6167. {
  6168. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6169. if ((FIgnoreNextDialogChar != 0) &&
  6170. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6171. {
  6172. Message.Result = 1;
  6173. }
  6174. else
  6175. {
  6176. TForm::Dispatch(&Message);
  6177. }
  6178. FIgnoreNextDialogChar = 0;
  6179. }
  6180. //---------------------------------------------------------------------------
  6181. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6182. {
  6183. // The four low-order bits are used internally by Windows
  6184. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6185. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6186. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6187. // 61730 - restore thru double click - undocumented
  6188. // 61490 - maximize thru double click - undocumented
  6189. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6190. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6191. {
  6192. SysResizing(Cmd);
  6193. }
  6194. TForm::Dispatch(&Message);
  6195. }
  6196. //---------------------------------------------------------------------------
  6197. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6198. {
  6199. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6200. // Also there no point returning TRUE as we are not able to
  6201. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6202. // Hence the process termination might be safer :)
  6203. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6204. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6205. {
  6206. Message.Result = FALSE;
  6207. }
  6208. else
  6209. {
  6210. Message.Result = TRUE;
  6211. }
  6212. // Do not call default handling as that triggers OnCloseQuery,
  6213. // where our implementation will popup confirmation dialogs, what we do not want,
  6214. // as per Vista guidelines:
  6215. // https://docs.microsoft.com/en-us/windows/desktop/shutdown/shutting-down
  6216. }
  6217. //---------------------------------------------------------------------------
  6218. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6219. {
  6220. if (Message.EndSession && IsApplicationMinimized())
  6221. {
  6222. // WORKAROUND
  6223. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6224. // when it receives WM_ENDSESSION,
  6225. // but that sometimes (particularly when application is minimized) cause crashes.
  6226. // Crash popups message that blocks log off.
  6227. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6228. // so we call ExitProcess() immediately, not even trying to cleanup.
  6229. // It still causes beep, so there's likely some popup, but it does not block
  6230. // log off.
  6231. ExitProcess(0);
  6232. }
  6233. TForm::Dispatch(&Message);
  6234. }
  6235. #ifdef _DEBUG
  6236. //---------------------------------------------------------------------------
  6237. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6238. {
  6239. TForm::Dispatch(&Message);
  6240. }
  6241. #endif
  6242. //---------------------------------------------------------------------------
  6243. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6244. {
  6245. }
  6246. //---------------------------------------------------------------------------
  6247. void __fastcall TCustomScpExplorerForm::DoShow()
  6248. {
  6249. // only now are the controls resized finally, so the size constraints
  6250. // will not conflict with possibly very small window size
  6251. RestoreFormParams();
  6252. FixControlsPlacement();
  6253. if (Position == poDefaultPosOnly)
  6254. {
  6255. CutFormToDesktop(this);
  6256. }
  6257. TForm::DoShow();
  6258. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6259. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6260. if (Terminal == NULL)
  6261. {
  6262. StartingDisconnected();
  6263. }
  6264. UpdatePixelsPerInchMainWindowCounter();
  6265. FShowing = true;
  6266. }
  6267. //---------------------------------------------------------------------------
  6268. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6269. {
  6270. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6271. }
  6272. //---------------------------------------------------------------------------
  6273. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  6274. {
  6275. DoTerminalListChanged();
  6276. }
  6277. //---------------------------------------------------------------------------
  6278. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6279. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6280. TNotifyEvent OnBalloonClick, TObject * UserData)
  6281. {
  6282. bool Do;
  6283. UnicodeString Message;
  6284. if (E == NULL)
  6285. {
  6286. Message = Str;
  6287. Do = true;
  6288. }
  6289. else
  6290. {
  6291. Do = ExceptionMessage(E, Message);
  6292. }
  6293. if (Do && WinConfiguration->BalloonNotifications)
  6294. {
  6295. UnicodeString Title;
  6296. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6297. {
  6298. Message = TrimLeft(Message);
  6299. }
  6300. else
  6301. {
  6302. Message = UnformatMessage(Message);
  6303. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6304. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6305. Title = LoadResourceString(Captions[Type]);
  6306. if (Terminal != NULL)
  6307. {
  6308. Title = FORMAT(L"%s - %s",
  6309. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), Title));
  6310. }
  6311. }
  6312. if (Seconds == 0)
  6313. {
  6314. Seconds = WinConfiguration->NotificationsTimeout;
  6315. }
  6316. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6317. }
  6318. }
  6319. //---------------------------------------------------------------------------
  6320. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6321. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6322. UnicodeString HelpKeyword, const TMessageParams * Params,
  6323. TTerminal * Terminal)
  6324. {
  6325. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6326. (FOnFeedSynchronizeError != NULL)) &&
  6327. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6328. {
  6329. if (FOnFeedSynchronizeError != NULL)
  6330. {
  6331. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6332. }
  6333. else
  6334. {
  6335. DebugAssert(FErrorList != NULL);
  6336. TStringList * MoreMessagesCopy = NULL;
  6337. if (MoreMessages)
  6338. {
  6339. MoreMessagesCopy = new TStringList();
  6340. MoreMessagesCopy->Assign(MoreMessages);
  6341. }
  6342. FErrorList->AddObject(Message, MoreMessagesCopy);
  6343. }
  6344. PopupTrayBalloon(Terminal, Message, Type);
  6345. return ContinueAnswer(Answers);
  6346. }
  6347. else
  6348. {
  6349. bool UseBalloon = FTrayIcon->Visible;
  6350. if (UseBalloon)
  6351. {
  6352. PopupTrayBalloon(Terminal, Message, Type);
  6353. }
  6354. unsigned int Result;
  6355. try
  6356. {
  6357. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6358. }
  6359. __finally
  6360. {
  6361. // cancel only balloon we popped up, otherwise we may cancel notification
  6362. // balloon that was there before the message dialog
  6363. // (such as "dd confirmation opt in balloon)
  6364. if (UseBalloon)
  6365. {
  6366. FTrayIcon->CancelBalloon();
  6367. }
  6368. }
  6369. return Result;
  6370. }
  6371. }
  6372. //---------------------------------------------------------------------------
  6373. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6374. TTerminal * Terminal, Exception * E)
  6375. {
  6376. if (FTrayIcon->Visible)
  6377. {
  6378. PopupTrayBalloon(Terminal, L"", qtError, E);
  6379. }
  6380. // particularly prevent opening new session from jump list,
  6381. // while exception is shown
  6382. NonVisualDataModule->StartBusy();
  6383. try
  6384. {
  6385. ShowExtendedExceptionEx(Terminal, E);
  6386. }
  6387. __finally
  6388. {
  6389. NonVisualDataModule->EndBusy();
  6390. FTrayIcon->CancelBalloon();
  6391. }
  6392. }
  6393. //---------------------------------------------------------------------------
  6394. void __fastcall TCustomScpExplorerForm::TerminalReady()
  6395. {
  6396. // cannot rely on active page being page for active terminal,
  6397. // as it can happen that active page is the "new session" page
  6398. // (e.g. when reconnecting active terminal, while login dialog
  6399. // invoked from "new session" page is modal)
  6400. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6401. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  6402. }
  6403. //---------------------------------------------------------------------------
  6404. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6405. TTerminal * Terminal, Exception * E)
  6406. {
  6407. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6408. if (!Terminal->Active)
  6409. {
  6410. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6411. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6412. {
  6413. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6414. }
  6415. }
  6416. }
  6417. //---------------------------------------------------------------------------
  6418. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6419. UnicodeString Message, TQueryType Type,
  6420. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6421. {
  6422. if ((E == NULL) ||
  6423. ExceptionMessage(E, Message))
  6424. {
  6425. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6426. if (Important)
  6427. {
  6428. Seconds *= 5;
  6429. }
  6430. UnicodeString NoteMessage(UnformatMessage(Message));
  6431. if (Terminal != NULL)
  6432. {
  6433. NoteMessage = FORMAT(L"%s: %s",
  6434. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), NoteMessage));
  6435. }
  6436. if (WinConfiguration->BalloonNotifications)
  6437. {
  6438. AddNote(NoteMessage);
  6439. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6440. }
  6441. else
  6442. {
  6443. FlashOnBackground();
  6444. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6445. }
  6446. }
  6447. }
  6448. //---------------------------------------------------------------------------
  6449. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6450. {
  6451. RestoreApp();
  6452. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6453. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6454. !NonVisualDataModule->Busy)
  6455. {
  6456. TTerminal * Terminal = TerminalNoteData->Terminal;
  6457. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6458. if (!ComponentVisible[fcQueueView])
  6459. {
  6460. ToggleQueueVisibility();
  6461. GoToQueue();
  6462. }
  6463. }
  6464. }
  6465. //---------------------------------------------------------------------------
  6466. void __fastcall TCustomScpExplorerForm::QueueEvent(TTerminal * ATerminal,
  6467. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6468. {
  6469. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  6470. UnicodeString Message;
  6471. TNotifyEvent OnClick = NULL;
  6472. TObject * UserData = NULL;
  6473. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6474. switch (Event)
  6475. {
  6476. case qeEmptyButMonitored:
  6477. if ((ATerminal != Terminal) || QueueInvisible)
  6478. {
  6479. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6480. OnClick = QueueEmptyNoteClicked;
  6481. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6482. TerminalNoteData->Terminal = ATerminal;
  6483. UserData = TerminalNoteData;
  6484. }
  6485. break;
  6486. case qeEmpty:
  6487. OperationComplete(ManagedTerminal->QueueOperationStart);
  6488. break;
  6489. case qePendingUserAction:
  6490. if ((ATerminal != Terminal) ||
  6491. (QueueInvisible && !IsQueueAutoPopup()))
  6492. {
  6493. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6494. }
  6495. break;
  6496. default:
  6497. DebugFail();
  6498. }
  6499. if (!Message.IsEmpty())
  6500. {
  6501. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6502. }
  6503. }
  6504. //---------------------------------------------------------------------------
  6505. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6506. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6507. {
  6508. if (FDDExtMapFile != NULL)
  6509. {
  6510. CloseHandle(FDDExtMapFile);
  6511. FDDExtMapFile = NULL;
  6512. }
  6513. if (WinConfiguration->DDFakeFile)
  6514. {
  6515. DDFakeFileInitDrag(FileList, Created);
  6516. }
  6517. }
  6518. //---------------------------------------------------------------------------
  6519. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  6520. {
  6521. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  6522. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  6523. {
  6524. FFakeFileDropTarget = FileName;
  6525. }
  6526. }
  6527. //---------------------------------------------------------------------------
  6528. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  6529. {
  6530. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6531. if (!ForceDirectories(ApiPath(Result)))
  6532. {
  6533. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  6534. }
  6535. FileSetAttr(ApiPath(Result), faHidden);
  6536. return Result;
  6537. }
  6538. //---------------------------------------------------------------------------
  6539. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  6540. bool & Created)
  6541. {
  6542. FFakeFileDropTarget = UnicodeString();
  6543. FDragFakeDirectory = CreateFakeTransferDirectory();
  6544. FileList->AddItem(NULL, FDragFakeDirectory);
  6545. Created = true;
  6546. if (!WinConfiguration->IsDDExtRunning() || (GetWindowsBuild() >= 17134))
  6547. {
  6548. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  6549. }
  6550. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6551. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6552. {
  6553. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6554. TDragExtCommStruct* CommStruct;
  6555. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6556. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6557. DebugAssert(CommStruct != NULL);
  6558. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  6559. CommStruct->Dragging = true;
  6560. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  6561. LENOF(CommStruct->DropDest));
  6562. NULL_TERMINATE(CommStruct->DropDest);
  6563. UnmapViewOfFile(CommStruct);
  6564. }
  6565. }
  6566. //---------------------------------------------------------------------------
  6567. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  6568. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  6569. {
  6570. bool Result;
  6571. if (Sender == RemoteDirView)
  6572. {
  6573. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  6574. }
  6575. else
  6576. {
  6577. DebugAssert(Sender == RemoteDriveView);
  6578. TStrings * FileList = RemoteDriveView->DragFileList();
  6579. try
  6580. {
  6581. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  6582. }
  6583. __finally
  6584. {
  6585. delete FileList;
  6586. }
  6587. }
  6588. if (FDDTargetControl == RemoteDriveView)
  6589. {
  6590. RemoteDriveView->UpdateDropTarget();
  6591. }
  6592. // foRemoteMove happens when file/dir is dragged within the remote tree view
  6593. // or from tree view to dir view.
  6594. // foMove happens when file/dir is dragged from remote tree view outside of
  6595. // application via dragex
  6596. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  6597. (DropSourceControl == RemoteDriveView))
  6598. {
  6599. RemoteDriveView->UpdateDropSource();
  6600. }
  6601. return Result;
  6602. }
  6603. //---------------------------------------------------------------------------
  6604. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  6605. {
  6606. // This also handles drops of remote files to queue.
  6607. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  6608. SAFE_DESTROY(FDDFileList);
  6609. if (!FFakeFileDropTarget.IsEmpty())
  6610. {
  6611. RemoveDir(ApiPath(FFakeFileDropTarget));
  6612. }
  6613. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  6614. {
  6615. try
  6616. {
  6617. TDragResult DDResult = (Sender == RemoteDirView) ?
  6618. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  6619. // Focus is moved to the target application,
  6620. // but as we are going to present the UI, we need to steal the focus back.
  6621. // This most likely won't work though (windows does not allow application
  6622. // to steal focus most of the time)
  6623. Application->BringToFront();
  6624. // On older version of Windows we never got drMove here, see also comment below.
  6625. // On Windows 10, we get the "move".
  6626. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  6627. {
  6628. UnicodeString TargetDirectory;
  6629. TFileOperation Operation;
  6630. // drInvalid may mean drMove, see comment below
  6631. switch (DDResult)
  6632. {
  6633. case drCopy:
  6634. Operation = foCopy;
  6635. break;
  6636. case drMove:
  6637. Operation = foMove;
  6638. break;
  6639. default:
  6640. DebugFail();
  6641. case drInvalid:
  6642. // prefer "copy" for safety
  6643. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  6644. break;
  6645. }
  6646. TTransferOperationParam Param;
  6647. UnicodeString CounterName;
  6648. bool ForceQueue;
  6649. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  6650. {
  6651. // we get drInvalid both if d&d was intercepted by ddext,
  6652. // and when users drops on no-drop location.
  6653. // we tell the difference by existence of response from ddext,
  6654. // so we ignore absence of response in this case
  6655. if (DDResult != drInvalid)
  6656. {
  6657. // here we know that the extension is installed,
  6658. // as it is checked as soon as drag&drop starts from
  6659. // RemoteFileControlDDCreateDragFileList
  6660. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  6661. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  6662. }
  6663. }
  6664. else
  6665. {
  6666. // download using fake file
  6667. Param.Temp = false;
  6668. Param.DragDrop = true;
  6669. if (ForceQueue)
  6670. {
  6671. Param.Queue = asOn;
  6672. }
  6673. if (Sender == RemoteDirView)
  6674. {
  6675. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  6676. }
  6677. if (RemoteFileControlFileOperation(Sender, Operation,
  6678. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  6679. {
  6680. Configuration->Usage->Inc(CounterName);
  6681. }
  6682. }
  6683. }
  6684. }
  6685. __finally
  6686. {
  6687. CloseHandle(FDDExtMapFile);
  6688. FDDExtMapFile = NULL;
  6689. delete FDragFakeMonitors;
  6690. FDragFakeMonitors = NULL;
  6691. RemoveDir(ApiPath(FDragFakeDirectory));
  6692. FDragFakeDirectory = L"";
  6693. FFakeFileDropTarget = UnicodeString();
  6694. }
  6695. }
  6696. if (FDragDropSshTerminate.get() != NULL)
  6697. {
  6698. FDragDropSshTerminate->Rethrow();
  6699. }
  6700. }
  6701. //---------------------------------------------------------------------------
  6702. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  6703. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  6704. {
  6705. // Remember drop effect so we know (when user drops files), if we copy or move
  6706. FLastDropEffect = dwEffect;
  6707. }
  6708. //---------------------------------------------------------------------------
  6709. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  6710. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  6711. {
  6712. bool Result;
  6713. if (FDDTargetControl == QueueView3)
  6714. {
  6715. Directory = DefaultDownloadTargetDirectory();
  6716. Result = true;
  6717. CounterName = L"DownloadsDragDropQueue";
  6718. ForceQueue = true;
  6719. }
  6720. else if (!FFakeFileDropTarget.IsEmpty())
  6721. {
  6722. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  6723. Result = true;
  6724. ForceQueue = false;
  6725. CounterName = L"DownloadsDragDropFakeFile";
  6726. }
  6727. else
  6728. {
  6729. ForceQueue = false;
  6730. Enabled = false;
  6731. try
  6732. {
  6733. int Timer = 0;
  6734. Result = false;
  6735. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  6736. {
  6737. {
  6738. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6739. TDragExtCommStruct* CommStruct;
  6740. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6741. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6742. DebugAssert(CommStruct != NULL);
  6743. Result = !CommStruct->Dragging;
  6744. if (Result)
  6745. {
  6746. Directory = ExtractFilePath(CommStruct->DropDest);
  6747. CounterName = L"DownloadsDragDropExternalExt";
  6748. }
  6749. UnmapViewOfFile(CommStruct);
  6750. }
  6751. if (!Result)
  6752. {
  6753. Sleep(50);
  6754. Timer += 50;
  6755. Application->ProcessMessages();
  6756. }
  6757. }
  6758. }
  6759. __finally
  6760. {
  6761. Enabled = true;
  6762. }
  6763. }
  6764. return Result;
  6765. }
  6766. //---------------------------------------------------------------------------
  6767. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  6768. const UnicodeString TempDir, int SecDelay)
  6769. {
  6770. TDateTime Alarm = Now() + (double)((double)SecDelay*OneMillisecond);
  6771. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  6772. if (FDelayedDeletionTimer == NULL)
  6773. {
  6774. DebugAssert(HandleAllocated());
  6775. FDelayedDeletionTimer = new TTimer(this);
  6776. FDelayedDeletionTimer->Interval = 10000;
  6777. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  6778. }
  6779. else
  6780. {
  6781. FDelayedDeletionTimer->Enabled = true;
  6782. }
  6783. }
  6784. //---------------------------------------------------------------------------
  6785. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  6786. {
  6787. DebugAssert(FDelayedDeletionList != NULL);
  6788. TDateTime N = Now();
  6789. TDateTime Alert;
  6790. UnicodeString Directory;
  6791. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  6792. {
  6793. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  6794. if ((N >= Alert) || (Sender == NULL))
  6795. {
  6796. Directory = FDelayedDeletionList->Strings[Index];
  6797. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  6798. {
  6799. FDelayedDeletionList->Delete(Index);
  6800. }
  6801. }
  6802. }
  6803. if (FDelayedDeletionList->Count == 0)
  6804. {
  6805. FDelayedDeletionTimer->Enabled = false;
  6806. }
  6807. }
  6808. //---------------------------------------------------------------------------
  6809. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  6810. {
  6811. if (IsFileControl(FDDTargetControl, osRemote) ||
  6812. (FDDTargetControl == SessionsPageControl))
  6813. {
  6814. TTerminal * TargetTerminal = NULL;
  6815. TFileOperation Operation = foNone;
  6816. if (FDDTargetControl == SessionsPageControl)
  6817. {
  6818. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  6819. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  6820. // do not allow dropping on the "+" tab
  6821. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  6822. if (TargetTerminal != NULL)
  6823. {
  6824. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  6825. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  6826. {
  6827. Operation = foRemoteMove;
  6828. }
  6829. else
  6830. {
  6831. Operation = foRemoteCopy;
  6832. }
  6833. }
  6834. }
  6835. else
  6836. {
  6837. if (FLastDropEffect == DROPEFFECT_MOVE)
  6838. {
  6839. Operation = foRemoteMove;
  6840. }
  6841. else if (FLastDropEffect == DROPEFFECT_COPY)
  6842. {
  6843. Operation = foRemoteCopy;
  6844. }
  6845. }
  6846. if (Operation != foNone)
  6847. {
  6848. RemoteFileControlFileOperation(DropSourceControl,
  6849. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  6850. }
  6851. // abort drag&drop
  6852. Abort();
  6853. }
  6854. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  6855. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  6856. (FDDTargetControl != QueueView3))
  6857. {
  6858. DebugAssert(!FDragTempDir.IsEmpty());
  6859. TTransferType Type;
  6860. UnicodeString TempDir = FDragTempDir;
  6861. // We clear FDragTempDir before calling
  6862. // just in case it fail (raises exception)
  6863. FDragTempDir = L"";
  6864. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  6865. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  6866. // empty directory parameter means temp directory -> don't display it!
  6867. UnicodeString TargetDir = L"";
  6868. int Options = 0;
  6869. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  6870. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  6871. {
  6872. Abort();
  6873. }
  6874. // TargetDir is set when dropped on local file control
  6875. // (this was workaround for legacy dirview event handling, now it should be
  6876. // made prettier)
  6877. if (TargetDir.IsEmpty())
  6878. {
  6879. TargetDir = TempDir;
  6880. if (ForceDirectories(ApiPath(TargetDir)))
  6881. {
  6882. DebugAssert(Terminal && !TargetDir.IsEmpty());
  6883. FPendingTempSpaceWarn = true;
  6884. try
  6885. {
  6886. FDragDropOperation = true;
  6887. int Params = cpTemporary |
  6888. (Type == ttMove ? cpDelete : 0);
  6889. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  6890. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  6891. }
  6892. __finally
  6893. {
  6894. FDragDropOperation = false;
  6895. FPendingTempSpaceWarn = false;
  6896. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  6897. }
  6898. }
  6899. else
  6900. {
  6901. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  6902. }
  6903. }
  6904. }
  6905. }
  6906. //---------------------------------------------------------------------------
  6907. void __fastcall TCustomScpExplorerForm::DDDownload(
  6908. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  6909. {
  6910. TAutoBatch AutoBatch(this);
  6911. UpdateCopyParamCounters(*CopyParam);
  6912. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  6913. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  6914. {
  6915. RemoteDriveView->UpdateDropSource();
  6916. }
  6917. }
  6918. //---------------------------------------------------------------------------
  6919. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  6920. {
  6921. public:
  6922. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  6923. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  6924. {
  6925. }
  6926. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  6927. {
  6928. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  6929. TDataObjectFilesEx::AllowData(FormatEtc);
  6930. }
  6931. };
  6932. //---------------------------------------------------------------------------
  6933. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  6934. TObject * Sender, TDataObject *& DataObject)
  6935. {
  6936. if (FDDExtMapFile != NULL)
  6937. {
  6938. TFileList * FileList = DragDropFiles(Sender)->FileList;
  6939. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  6940. {
  6941. Abort();
  6942. }
  6943. if (FileList->Count > 0)
  6944. {
  6945. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  6946. if (!FilesObject->IsValid(true, true))
  6947. {
  6948. FilesObject->_Release();
  6949. }
  6950. else
  6951. {
  6952. DataObject = FilesObject;
  6953. }
  6954. }
  6955. }
  6956. }
  6957. //---------------------------------------------------------------------------
  6958. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  6959. {
  6960. DebugFail();
  6961. }
  6962. //---------------------------------------------------------------------------
  6963. void __fastcall TCustomScpExplorerForm::GoToTree()
  6964. {
  6965. ComponentVisible[fcRemoteTree] = true;
  6966. RemoteDriveView->SetFocus();
  6967. }
  6968. //---------------------------------------------------------------------------
  6969. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  6970. TPanelExport Export)
  6971. {
  6972. TCustomDirView * DirView = this->DirView(Side);
  6973. std::unique_ptr<TStrings> ExportData(new TStringList());
  6974. switch (Export)
  6975. {
  6976. case pePath:
  6977. ExportData->Add(DirView->PathName);
  6978. break;
  6979. case peFileList:
  6980. case peFullFileList:
  6981. {
  6982. bool FullPath = (Export == peFullFileList);
  6983. DirView->CreateFileList(false, FullPath, ExportData.get());
  6984. for (int Index = 0; Index < ExportData->Count; Index++)
  6985. {
  6986. if (ExportData->Strings[Index].Pos(L" ") > 0)
  6987. {
  6988. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  6989. }
  6990. }
  6991. }
  6992. break;
  6993. default:
  6994. DebugFail();
  6995. }
  6996. return ExportData.release();
  6997. }
  6998. //---------------------------------------------------------------------------
  6999. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7000. TPanelExport Export, TPanelExportDestination Destination)
  7001. {
  7002. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7003. PanelExportStore(Side, Export, Destination, ExportData.get());
  7004. }
  7005. //---------------------------------------------------------------------------
  7006. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7007. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7008. TStrings * ExportData)
  7009. {
  7010. if (Destination == pedClipboard)
  7011. {
  7012. TInstantOperationVisualizer Visualizer;
  7013. CopyToClipboard(ExportData);
  7014. }
  7015. else
  7016. {
  7017. DebugFail();
  7018. }
  7019. }
  7020. //---------------------------------------------------------------------------
  7021. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7022. {
  7023. TCustomDirView * DirView = this->DirView(Side);
  7024. TFileFilter Filter;
  7025. DefaultFileFilter(Filter);
  7026. Filter.Masks = DirView->Mask;
  7027. if (DoFilterMaskDialog(DirView, &Filter))
  7028. {
  7029. DirView->Mask = TFileMasks::NormalizeMask(Filter.Masks);
  7030. Configuration->Usage->Inc(L"Filters");
  7031. }
  7032. }
  7033. //---------------------------------------------------------------------------
  7034. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7035. {
  7036. return FQueueController->DefaultOperation();
  7037. }
  7038. //---------------------------------------------------------------------------
  7039. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7040. TQueueOperation Operation, void ** Param)
  7041. {
  7042. switch (Operation)
  7043. {
  7044. case qoPreferences:
  7045. return true;
  7046. case qoGoTo:
  7047. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7048. case qoOnceEmpty:
  7049. return !FQueueController->Empty;
  7050. default:
  7051. return FQueueController->AllowOperation(Operation, Param);
  7052. }
  7053. }
  7054. //---------------------------------------------------------------------------
  7055. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7056. {
  7057. if (DebugAlwaysTrue(QueueView3->Visible))
  7058. {
  7059. QueueView3->SetFocus();
  7060. }
  7061. }
  7062. //---------------------------------------------------------------------------
  7063. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7064. TQueueOperation Operation, void * Param)
  7065. {
  7066. if (Operation == qoGoTo)
  7067. {
  7068. GoToQueue();
  7069. }
  7070. else if (Operation == qoPreferences)
  7071. {
  7072. PreferencesDialog(pmQueue);
  7073. }
  7074. else
  7075. {
  7076. FQueueController->ExecuteOperation(Operation, Param);
  7077. }
  7078. }
  7079. //---------------------------------------------------------------------------
  7080. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7081. {
  7082. return (Queue != NULL) && Queue->Enabled;
  7083. }
  7084. //---------------------------------------------------------------------------
  7085. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7086. {
  7087. DebugAssert(Queue != NULL);
  7088. if (Queue != NULL)
  7089. {
  7090. Queue->Enabled = !Queue->Enabled;
  7091. }
  7092. }
  7093. //---------------------------------------------------------------------------
  7094. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7095. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7096. {
  7097. FQueueActedItem = QueueView3->ItemFocused;
  7098. }
  7099. //---------------------------------------------------------------------------
  7100. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7101. {
  7102. return TopDock->Height + QueueSplitter->Height;
  7103. }
  7104. //---------------------------------------------------------------------------
  7105. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7106. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7107. {
  7108. // when queue is hidden by double-clicking splitter, stray attempt to
  7109. // resize the panel with strange value arrives, make sure it is ignored
  7110. if (ComponentVisible[fcQueueView])
  7111. {
  7112. int HeightLimit = ClientHeight - GetStaticComponentsHeight() -
  7113. RemotePanel->Constraints->MinHeight;
  7114. if (NewSize > HeightLimit)
  7115. {
  7116. NewSize = HeightLimit;
  7117. }
  7118. }
  7119. else
  7120. {
  7121. Accept = false;
  7122. }
  7123. }
  7124. //---------------------------------------------------------------------------
  7125. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7126. TDragObject *& /*DragObject*/)
  7127. {
  7128. FQueueActedItem = QueueView3->ItemFocused;
  7129. }
  7130. //---------------------------------------------------------------------------
  7131. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7132. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7133. {
  7134. Accept = true;
  7135. if (Source == QueueView3)
  7136. {
  7137. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7138. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7139. if (Accept)
  7140. {
  7141. TQueueItemProxy * QueueItem;
  7142. TQueueItemProxy * DestQueueItem;
  7143. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7144. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7145. Accept = (QueueItem != DestQueueItem) &&
  7146. (QueueItem->Status == TQueueItem::qsPending) &&
  7147. (DestQueueItem->Status == TQueueItem::qsPending);
  7148. }
  7149. }
  7150. }
  7151. //---------------------------------------------------------------------------
  7152. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7153. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7154. {
  7155. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7156. {
  7157. TQueueItemProxy * QueueItem;
  7158. TQueueItemProxy * DestQueueItem;
  7159. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7160. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7161. QueueItem->Move(DestQueueItem);
  7162. }
  7163. }
  7164. //---------------------------------------------------------------------------
  7165. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7166. {
  7167. if ((QueueView3->ItemFocused == NULL) &&
  7168. (QueueView3->Items->Count > 0))
  7169. {
  7170. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7171. }
  7172. QueueLabelUpdateStatus();
  7173. }
  7174. //---------------------------------------------------------------------------
  7175. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7176. {
  7177. QueueLabelUpdateStatus();
  7178. }
  7179. //---------------------------------------------------------------------------
  7180. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7181. {
  7182. QueueLabel->UpdateStatus();
  7183. }
  7184. //---------------------------------------------------------------------------
  7185. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7186. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7187. {
  7188. if (Selected)
  7189. {
  7190. NonVisualDataModule->UpdateNonVisibleActions();
  7191. }
  7192. }
  7193. //---------------------------------------------------------------------------
  7194. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7195. {
  7196. TDragDropFilesEx * Result = NULL;
  7197. if (Sender == SessionsPageControl)
  7198. {
  7199. Result = FSessionsDragDropFilesEx;
  7200. }
  7201. else if (Sender == QueueView3)
  7202. {
  7203. Result = FQueueDragDropFilesEx;
  7204. }
  7205. else
  7206. {
  7207. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7208. if (DirView != NULL)
  7209. {
  7210. Result = DirView->DragDropFilesEx;
  7211. }
  7212. else
  7213. {
  7214. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7215. if (DriveView != NULL)
  7216. {
  7217. Result = DriveView->DragDropFilesEx;
  7218. }
  7219. }
  7220. }
  7221. DebugAssert(Result != NULL);
  7222. return Result;
  7223. }
  7224. //---------------------------------------------------------------------------
  7225. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7226. {
  7227. return (DView->SelCount == DView->FilesCount);
  7228. }
  7229. //---------------------------------------------------------------------------
  7230. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7231. {
  7232. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7233. }
  7234. //---------------------------------------------------------------------------
  7235. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7236. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue)
  7237. {
  7238. TFileOperation Operation;
  7239. switch (Effect)
  7240. {
  7241. case DROPEFFECT_MOVE:
  7242. Operation = foMove;
  7243. break;
  7244. case DROPEFFECT_COPY:
  7245. // occures on WinXP (reported by user)
  7246. default:
  7247. Operation = foCopy;
  7248. break;
  7249. };
  7250. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7251. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7252. if (DragDropFilesEx->FileList->Count > 0)
  7253. {
  7254. TStrings * FileList = new TStringList();
  7255. try
  7256. {
  7257. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7258. {
  7259. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7260. }
  7261. FDragDropOperation = true;
  7262. TTransferOperationParam Param;
  7263. Param.TargetDirectory = TargetPath;
  7264. // upload, no temp dirs
  7265. Param.Temp = false;
  7266. Param.DragDrop = true;
  7267. Param.Options =
  7268. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7269. if (ForceQueue)
  7270. {
  7271. Param.Queue = asOn;
  7272. }
  7273. if (ExecuteFileOperation(Operation, osLocal, FileList,
  7274. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7275. {
  7276. if (IsFileControl(DropSourceControl, osLocal))
  7277. {
  7278. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7279. }
  7280. else
  7281. {
  7282. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7283. }
  7284. }
  7285. }
  7286. __finally
  7287. {
  7288. FDragDropOperation = false;
  7289. delete FileList;
  7290. }
  7291. }
  7292. }
  7293. //---------------------------------------------------------------------------
  7294. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7295. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7296. UnicodeString TargetPath, bool & /*DoOperation*/)
  7297. {
  7298. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false);
  7299. }
  7300. //---------------------------------------------------------------------------
  7301. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7302. TObject * Sender, int grfKeyState, int & dwEffect)
  7303. {
  7304. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7305. if ((dwEffect != DROPEFFECT_None) &&
  7306. IsFileControl(DropSourceControl, osRemote))
  7307. {
  7308. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7309. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7310. (RemoteDirView->DropTarget == NULL))
  7311. {
  7312. dwEffect = DROPEFFECT_None;
  7313. }
  7314. else
  7315. {
  7316. if (dwEffect == DROPEFFECT_Copy)
  7317. {
  7318. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  7319. // currently we support copying always (at least via temporary directory);
  7320. // remove associated checks once this all proves stable and working
  7321. bool CopyCapable = true;
  7322. // if we do not support neither of operations, there's no discussion
  7323. if (!MoveCapable && !CopyCapable)
  7324. {
  7325. dwEffect = DROPEFFECT_None;
  7326. }
  7327. else
  7328. {
  7329. // The default effect inside remote panel is move,
  7330. // unless we do not support it, but support copy
  7331. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7332. {
  7333. dwEffect = MoveCapable ? DROPEFFECT_Move : DROPEFFECT_Copy;
  7334. }
  7335. else
  7336. {
  7337. // with ctrl-down, we want copy unless it is not supported
  7338. dwEffect = CopyCapable ? DROPEFFECT_Copy : DROPEFFECT_None;
  7339. }
  7340. }
  7341. }
  7342. }
  7343. }
  7344. }
  7345. //---------------------------------------------------------------------------
  7346. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7347. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7348. {
  7349. if (FDDTotalSize >= 0)
  7350. {
  7351. if (File->IsDirectory)
  7352. {
  7353. FDDTotalSize = -1;
  7354. }
  7355. else
  7356. {
  7357. FDDTotalSize += File->Size;
  7358. }
  7359. }
  7360. DebugAssert(!FDragTempDir.IsEmpty());
  7361. // TODO: this is quite ineffective
  7362. // TODO: what if invalid character replacement is disabled?
  7363. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7364. UnicodeString TransferFileName;
  7365. if (Sender == RemoteDriveView)
  7366. {
  7367. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7368. }
  7369. else
  7370. {
  7371. TransferFileName = File->FileName;
  7372. }
  7373. FDDFileList->AddObject(TransferFileName, File);
  7374. }
  7375. //---------------------------------------------------------------------------
  7376. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7377. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7378. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7379. {
  7380. // sometimes we do not get DDEnd so the list is not released
  7381. SAFE_DESTROY(FDDFileList);
  7382. FDDFileList = new TStringList();
  7383. FDragTempDir = WinConfiguration->TemporaryDir();
  7384. FDDTotalSize = 0;
  7385. FDragDropSshTerminate.reset(NULL);
  7386. }
  7387. //---------------------------------------------------------------------------
  7388. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7389. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7390. HRESULT & Result)
  7391. {
  7392. if (Result == DRAGDROP_S_DROP)
  7393. {
  7394. try
  7395. {
  7396. GlobalDragImageList->HideDragImage();
  7397. try
  7398. {
  7399. RemoteFileControlDDTargetDrop();
  7400. }
  7401. catch(ESshTerminate & E)
  7402. {
  7403. FDragDropSshTerminate.reset(E.Clone());
  7404. }
  7405. }
  7406. catch (Exception &E)
  7407. {
  7408. // If downloading fails we need to cancel drag&drop, otherwise
  7409. // Explorer shows error
  7410. // But by the way exception probably never reach this point as
  7411. // it's catched on way.
  7412. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7413. Result = DRAGDROP_S_CANCEL;
  7414. ShowExtendedException(Terminal, &E);
  7415. }
  7416. }
  7417. }
  7418. //---------------------------------------------------------------------------
  7419. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7420. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7421. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7422. {
  7423. TFileMasks::TParams MaskParams;
  7424. MaskParams.Size = Size;
  7425. MaskParams.Modification = Modification;
  7426. // this does not re-parse the mask if it is the same as the last time
  7427. FDirViewMatchMask = Masks;
  7428. bool ImplicitMatch;
  7429. Matches =
  7430. // RecurseInclude parameter has no effect as the Path is empty
  7431. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7432. (AllowImplicitMatches || !ImplicitMatch);
  7433. }
  7434. //---------------------------------------------------------------------------
  7435. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7436. TObject * Sender, TListItem * Item, WORD & Indexes)
  7437. {
  7438. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7439. UnicodeString Ext;
  7440. if (DirView == RemoteDirView)
  7441. {
  7442. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7443. }
  7444. else
  7445. {
  7446. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7447. }
  7448. if (SameText(Ext, Configuration->PartialExt))
  7449. {
  7450. Indexes |= oiPartial;
  7451. }
  7452. }
  7453. //---------------------------------------------------------------------------
  7454. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7455. {
  7456. return
  7457. DirViewEnabled(osCurrent) &&
  7458. DirView(osCurrent)->CanPasteFromClipBoard();
  7459. }
  7460. //---------------------------------------------------------------------------
  7461. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7462. {
  7463. bool Result = false;
  7464. if (CanPasteToDirViewFromClipBoard())
  7465. {
  7466. Result = true;
  7467. }
  7468. else
  7469. {
  7470. UnicodeString ClipboardText;
  7471. if (NonEmptyTextFromClipboard(ClipboardText))
  7472. {
  7473. if (StoredSessions->IsUrl(ClipboardText))
  7474. {
  7475. Result = true;
  7476. }
  7477. else
  7478. {
  7479. Result = DirViewEnabled(osCurrent);
  7480. }
  7481. }
  7482. }
  7483. return Result;
  7484. }
  7485. //---------------------------------------------------------------------------
  7486. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7487. {
  7488. if (DoesClipboardContainOurFiles())
  7489. {
  7490. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  7491. {
  7492. TTerminalManager * Manager = TTerminalManager::Instance();
  7493. TTerminal * TergetTerminal = Manager->ActiveTerminal;
  7494. Manager->ActiveTerminal = FClipboardTerminal;
  7495. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TergetTerminal);
  7496. }
  7497. }
  7498. else if (CanPasteToDirViewFromClipBoard())
  7499. {
  7500. DirView(osCurrent)->PasteFromClipBoard();
  7501. }
  7502. else
  7503. {
  7504. UnicodeString ClipboardText;
  7505. if (NonEmptyTextFromClipboard(ClipboardText))
  7506. {
  7507. if (StoredSessions->IsUrl(ClipboardText))
  7508. {
  7509. NewSession(false, ClipboardText);
  7510. }
  7511. else
  7512. {
  7513. DirView(osCurrent)->Path = ClipboardText;
  7514. }
  7515. }
  7516. }
  7517. }
  7518. //---------------------------------------------------------------------------
  7519. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  7520. {
  7521. // TBD
  7522. }
  7523. //---------------------------------------------------------------------------
  7524. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  7525. {
  7526. TCustomDirView * ADirView = DirView(Side);
  7527. ADirView->SelectAll(Mode);
  7528. ADirView->SetFocus();
  7529. }
  7530. //---------------------------------------------------------------------------
  7531. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  7532. {
  7533. TCustomDirView * ADirView = DirView(Side);
  7534. TFileFilter Filter;
  7535. DefaultFileFilter(Filter);
  7536. if (DoSelectMaskDialog(ADirView, Select, &Filter, Configuration))
  7537. {
  7538. Configuration->Usage->Inc(L"MaskSelections");
  7539. ADirView->SelectFiles(Filter, Select);
  7540. ADirView->SetFocus();
  7541. }
  7542. }
  7543. //---------------------------------------------------------------------------
  7544. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  7545. {
  7546. TCustomDirView * ADirView = DirView(Side);
  7547. ADirView->RestoreSelectedNames();
  7548. ADirView->SetFocus();
  7549. }
  7550. //---------------------------------------------------------------------------
  7551. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  7552. {
  7553. TCustomDirView * CurrentDirView = DirView(osCurrent);
  7554. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  7555. {
  7556. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  7557. UnicodeString Ext;
  7558. if (GetSide(osCurrent) == osRemote)
  7559. {
  7560. Ext = UnixExtractFileExt(FileName);
  7561. }
  7562. else
  7563. {
  7564. Ext = ExtractFileExt(FileName);
  7565. }
  7566. if (Ext.IsEmpty())
  7567. {
  7568. Ext = L".";
  7569. }
  7570. TFileFilter Filter;
  7571. Filter.Masks = FORMAT(L"*%s", (Ext));
  7572. Filter.Directories = false;
  7573. CurrentDirView->SelectFiles(Filter, Select);
  7574. }
  7575. }
  7576. //---------------------------------------------------------------------------
  7577. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  7578. const TStatusFileInfo & FileInfo, TOperationSide Side)
  7579. {
  7580. UnicodeString Result;
  7581. if ((Side == osRemote) && (Terminal == NULL))
  7582. {
  7583. // noop
  7584. }
  7585. else
  7586. {
  7587. Result =
  7588. FMTLOAD(FILE_INFO_FORMAT,
  7589. (FormatBytes(FileInfo.SelectedSize),
  7590. FormatBytes(FileInfo.FilesSize),
  7591. FormatNumber(FileInfo.SelectedCount),
  7592. FormatNumber(FileInfo.FilesCount)));
  7593. }
  7594. return Result;
  7595. }
  7596. //---------------------------------------------------------------------------
  7597. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  7598. TTBXStatusPanel * Panel, TOperationSide Side)
  7599. {
  7600. if (Panel->Index == 1)
  7601. {
  7602. ToggleShowHiddenFiles();
  7603. }
  7604. else if (Panel->Index == 2)
  7605. {
  7606. Filter(Side);
  7607. }
  7608. }
  7609. //---------------------------------------------------------------------------
  7610. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  7611. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  7612. {
  7613. DebugAssert(!StatusBar->SimplePanel);
  7614. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  7615. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  7616. }
  7617. //---------------------------------------------------------------------------
  7618. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  7619. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  7620. {
  7621. DebugAssert(StatusBar->Panels->Count >= 3);
  7622. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  7623. if (FileInfo.HiddenCount > 0)
  7624. {
  7625. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  7626. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7627. }
  7628. else
  7629. {
  7630. HiddenFilesPanel->ViewPriority = 0;
  7631. // not really necessary, just to cleanup no-longer-valid data
  7632. HiddenFilesPanel->Caption = L"";
  7633. }
  7634. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  7635. if (FileInfo.FilteredCount > 0)
  7636. {
  7637. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  7638. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7639. }
  7640. else
  7641. {
  7642. FilteredFilesPanel->ViewPriority = 0;
  7643. // not really necessary, just to cleanup no-longer-valid data
  7644. FilteredFilesPanel->Caption = L"";
  7645. }
  7646. }
  7647. //---------------------------------------------------------------------------
  7648. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  7649. TObject * /*Sender*/)
  7650. {
  7651. if (RemoteDirView->Enabled)
  7652. {
  7653. RemoteDirView->SetFocus();
  7654. }
  7655. }
  7656. //---------------------------------------------------------------------------
  7657. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  7658. {
  7659. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  7660. switch (Config.Show)
  7661. {
  7662. case qvShow:
  7663. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  7664. {
  7665. Config.Show = qvHide;
  7666. }
  7667. else
  7668. {
  7669. Config.Show = Config.LastHideShow;
  7670. }
  7671. break;
  7672. case qvHideWhenEmpty:
  7673. if (ComponentVisible[fcQueueView])
  7674. {
  7675. Config.Show = qvHide;
  7676. }
  7677. else
  7678. {
  7679. Config.LastHideShow = Config.Show;
  7680. Config.Show = qvShow;
  7681. }
  7682. break;
  7683. case qvHide:
  7684. Config.LastHideShow = Config.Show;
  7685. Config.Show = qvShow;
  7686. break;
  7687. }
  7688. WinConfiguration->QueueView = Config;
  7689. }
  7690. //---------------------------------------------------------------------------
  7691. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  7692. {
  7693. UnicodeString Result;
  7694. if (FTerminal != NULL)
  7695. {
  7696. switch (WinConfiguration->PathInCaption)
  7697. {
  7698. case picShort:
  7699. Result = TTerminalManager::Instance()->GetTerminalShortPath(FTerminal);
  7700. break;
  7701. case picFull:
  7702. Result = FTerminal->CurrentDirectory;
  7703. break;
  7704. }
  7705. }
  7706. return Result;
  7707. }
  7708. //---------------------------------------------------------------------------
  7709. void __fastcall TCustomScpExplorerForm::UpdateControls()
  7710. {
  7711. TTerminalManager::Instance()->UpdateAppTitle();
  7712. // WORAKRDOUND: Disabling list view when it is not showing yet does not set its
  7713. // background to gray on Windows 7 (works on Windows 10).
  7714. // See also EnableControl
  7715. if (Showing)
  7716. {
  7717. bool HasTerminal = (Terminal != NULL);
  7718. if (HasTerminal)
  7719. {
  7720. // Update path when it changes
  7721. if ((SessionsPageControl->ActivePage != NULL) && (GetSessionTabTerminal(SessionsPageControl->ActivePage) == Terminal))
  7722. {
  7723. UpdateSessionTab(SessionsPageControl->ActivePage);
  7724. }
  7725. if (!RemoteDirView->Enabled)
  7726. {
  7727. RemoteDirView->Enabled = true;
  7728. if (FRemoteDirViewWasFocused)
  7729. {
  7730. ActiveControl = RemoteDirView;
  7731. }
  7732. }
  7733. RemoteDriveView->Enabled = true;
  7734. RemoteDirView->Color = (FSessionColor != 0 ? FSessionColor : clWindow);
  7735. RemoteDriveView->Color = RemoteDirView->Color;
  7736. }
  7737. else
  7738. {
  7739. if (RemoteDirView->Enabled)
  7740. {
  7741. // This is first called when the form is being constructed
  7742. // (not anymore due to Showing test above)
  7743. // but the false is overriden in the constructor later.
  7744. // An even later in TScpCommanderForm::DoShow()
  7745. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  7746. EnableControl(RemoteDirView, false);
  7747. }
  7748. EnableControl(RemoteDriveView, false);
  7749. }
  7750. EnableControl(QueueView3, HasTerminal && Terminal->IsCapable[fsBackgroundTransfers]);
  7751. QueueLabelUpdateStatus();
  7752. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  7753. }
  7754. }
  7755. //---------------------------------------------------------------------------
  7756. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  7757. {
  7758. UpdateControls();
  7759. }
  7760. //---------------------------------------------------------------------------
  7761. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  7762. TObject * Sender)
  7763. {
  7764. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7765. DebugAssert(DirView != NULL);
  7766. DoDirViewLoaded(DirView);
  7767. TransferPresetAutoSelect();
  7768. }
  7769. //---------------------------------------------------------------------------
  7770. void __fastcall TCustomScpExplorerForm::StartUpdates()
  7771. {
  7772. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  7773. // first run after installation
  7774. if (double(Updates.LastCheck) == 0)
  7775. {
  7776. // make sure next time there will be an update (if enabled)
  7777. Updates.LastCheck = TDateTime(1);
  7778. WinConfiguration->Updates = Updates;
  7779. }
  7780. else if ((double(Updates.Period) > 0) &&
  7781. (Now() - Updates.LastCheck >= Updates.Period))
  7782. {
  7783. TThreadMethod OnUpdatesChecked = NULL;
  7784. if (!IsUWP())
  7785. {
  7786. OnUpdatesChecked = UpdatesChecked;
  7787. }
  7788. StartUpdateThread(OnUpdatesChecked);
  7789. }
  7790. }
  7791. //---------------------------------------------------------------------------
  7792. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  7793. {
  7794. UnicodeString Message;
  7795. bool New;
  7796. TQueryType Type;
  7797. GetUpdatesMessage(Message, New, Type, false);
  7798. if (!Message.IsEmpty())
  7799. {
  7800. if (New)
  7801. {
  7802. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  7803. }
  7804. if (!New && (Type != qtWarning))
  7805. {
  7806. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  7807. }
  7808. else
  7809. {
  7810. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  7811. }
  7812. if (New)
  7813. {
  7814. Configuration->Usage->Inc(L"UpdateNotifications");
  7815. }
  7816. }
  7817. }
  7818. //---------------------------------------------------------------------------
  7819. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  7820. {
  7821. RestoreApp();
  7822. bool CanDisplay = !NonVisualDataModule->Busy;
  7823. if (!CanDisplay && (Screen->ActiveForm != NULL))
  7824. {
  7825. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  7826. }
  7827. if (CanDisplay)
  7828. {
  7829. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  7830. CheckForUpdates(true);
  7831. }
  7832. else
  7833. {
  7834. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  7835. }
  7836. }
  7837. //---------------------------------------------------------------------------
  7838. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  7839. TCopyParamRuleData & Data)
  7840. {
  7841. DebugAssert(Terminal != NULL);
  7842. Data.HostName = Terminal->SessionData->HostNameExpanded;
  7843. Data.UserName = Terminal->SessionData->UserNameExpanded;
  7844. Data.RemoteDirectory = RemoteDirView->PathName;
  7845. }
  7846. //---------------------------------------------------------------------------
  7847. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  7848. {
  7849. // Terminal can be null when we are changing local directory implicitly,
  7850. // because it has been deleted, while no session is connected
  7851. // (Login dialog is open)
  7852. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  7853. {
  7854. TCopyParamRuleData Data;
  7855. GetTransferPresetAutoSelectData(Data);
  7856. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  7857. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  7858. if (CopyParamIndex < 0)
  7859. {
  7860. // there is no preset that matches autoselection
  7861. // set preset that we consider "default"
  7862. FCopyParamAutoSelected = L""; // forget last autoselected preset
  7863. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  7864. }
  7865. else
  7866. {
  7867. // there is preset matching autoselection
  7868. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  7869. if (CopyParamName == FCopyParamAutoSelected)
  7870. {
  7871. // autoselected the same preset as the last time
  7872. // make no change (i.e. preserve custom user preset, if any)
  7873. }
  7874. else
  7875. {
  7876. // autoselected the different preset then the last time (or there
  7877. // was default preset set)
  7878. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  7879. GUIConfiguration->CopyParamCurrent = CopyParamName;
  7880. }
  7881. }
  7882. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  7883. {
  7884. if (CopyParamIndex >= 0)
  7885. {
  7886. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  7887. }
  7888. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  7889. try
  7890. {
  7891. int Fmt =
  7892. (CopyParamIndex < 0) ?
  7893. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  7894. COPY_PARAM_AUTOSELECTED;
  7895. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  7896. Data->Message = MainInstructions(Message);
  7897. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  7898. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  7899. CopyParamAttrs);
  7900. if (CopyParamIndex >= 0)
  7901. {
  7902. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  7903. Info = FORMAT(L"%s\n \n%s", (Info,
  7904. FMTLOAD(COPY_PARAM_RULE,
  7905. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  7906. }
  7907. Data->Message += L"\n\n" + Info;
  7908. if (WinConfiguration->CopyParamAutoSelectNotice)
  7909. {
  7910. TransferPresetNoteMessage(Data, true);
  7911. }
  7912. else
  7913. {
  7914. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  7915. Data = NULL; // ownership passed
  7916. }
  7917. }
  7918. __finally
  7919. {
  7920. delete Data;
  7921. }
  7922. }
  7923. }
  7924. }
  7925. //---------------------------------------------------------------------------
  7926. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  7927. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  7928. {
  7929. DebugAssert(NoteData != NULL);
  7930. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  7931. TQueryButtonAlias Aliases[1];
  7932. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  7933. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  7934. Params.Aliases = Aliases;
  7935. Params.AliasesCount = LENOF(Aliases);
  7936. unsigned int Result =
  7937. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  7938. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  7939. switch (Result)
  7940. {
  7941. case qaNeverAskAgain:
  7942. DebugAssert(AllowNeverAskAgain);
  7943. WinConfiguration->CopyParamAutoSelectNotice = false;
  7944. break;
  7945. case qaIgnore:
  7946. PreferencesDialog(pmPresets);
  7947. break;
  7948. }
  7949. }
  7950. //---------------------------------------------------------------------------
  7951. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  7952. {
  7953. // as of now useless, as this is used for notes only, never for balloons, ...
  7954. RestoreApp();
  7955. // .. and we should never be busy here
  7956. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  7957. {
  7958. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  7959. }
  7960. }
  7961. //---------------------------------------------------------------------------
  7962. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  7963. TPreferencesMode APreferencesMode)
  7964. {
  7965. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  7966. TCopyParamRuleData Data;
  7967. if (Terminal != NULL)
  7968. {
  7969. PreferencesData.reset(new TPreferencesDialogData());
  7970. GetTransferPresetAutoSelectData(Data);
  7971. PreferencesData->CopyParamRuleData = &Data;
  7972. }
  7973. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  7974. }
  7975. //---------------------------------------------------------------------------
  7976. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  7977. const TCustomCommandType & Command)
  7978. {
  7979. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  7980. {
  7981. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (Command.Command)));
  7982. }
  7983. }
  7984. //---------------------------------------------------------------------------
  7985. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  7986. {
  7987. bool RemoteAllowed = CustomCommandRemoteAllowed();
  7988. TCustomCommandType Command;
  7989. // make sure we use local custom command when remote are not supported
  7990. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  7991. {
  7992. Command = FLastCustomCommand;
  7993. }
  7994. else
  7995. {
  7996. Command.Params = Command.Params | ccLocal;
  7997. }
  7998. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  7999. FEditingFocusedAdHocCommand = OnFocused;
  8000. bool LocalSide = (FCurrentSide == osLocal);
  8001. int Options =
  8002. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8003. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8004. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8005. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8006. {
  8007. FLastCustomCommand = Command;
  8008. UpdateCustomCommandsToolbar();
  8009. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8010. }
  8011. }
  8012. //---------------------------------------------------------------------------
  8013. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8014. {
  8015. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8016. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8017. DebugAssert(State > 0);
  8018. if (State <= 0)
  8019. {
  8020. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (FLastCustomCommand.Command)));
  8021. }
  8022. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8023. }
  8024. //---------------------------------------------------------------------------
  8025. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8026. TCustomCommandType & Command, int & State)
  8027. {
  8028. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8029. if (Result)
  8030. {
  8031. Command = FLastCustomCommand;
  8032. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8033. }
  8034. return Result;
  8035. }
  8036. //---------------------------------------------------------------------------
  8037. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8038. {
  8039. if (RemoteDirView->ItemFocused != NULL)
  8040. {
  8041. RemoteDirView->ItemFocused->CancelEdit();
  8042. }
  8043. }
  8044. //---------------------------------------------------------------------------
  8045. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8046. {
  8047. DebugAssert(ComponentVisible[Component]);
  8048. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8049. }
  8050. //---------------------------------------------------------------------------
  8051. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8052. {
  8053. // when queue panel is resized here directly, the status bar is stretched
  8054. // over whole space the panel occupied
  8055. PostComponentHide(fcQueueView);
  8056. }
  8057. //---------------------------------------------------------------------------
  8058. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8059. {
  8060. TMessage * M = static_cast<TMessage*>(Message);
  8061. switch (M->Msg)
  8062. {
  8063. case CM_DIALOGCHAR:
  8064. CMDialogChar(*M);
  8065. break;
  8066. case WM_APPCOMMAND:
  8067. WMAppCommand(*M);
  8068. break;
  8069. case WM_SYSCOMMAND:
  8070. WMSysCommand(*M);
  8071. break;
  8072. case WM_QUERYENDSESSION:
  8073. WMQueryEndSession(*M);
  8074. break;
  8075. case WM_ENDSESSION:
  8076. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8077. break;
  8078. #ifdef _DEBUG
  8079. case WM_WINDOWPOSCHANGED:
  8080. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8081. break;
  8082. case CM_CUSTOMSTYLECHANGED:
  8083. TForm::Dispatch(Message);
  8084. break;
  8085. #endif
  8086. case WM_COMPONENT_HIDE:
  8087. {
  8088. Byte Component = static_cast<Byte>(M->WParam);
  8089. // sanity check
  8090. if (ComponentVisible[Component])
  8091. {
  8092. // special treatment
  8093. if (Component == fcQueueView)
  8094. {
  8095. ToggleQueueVisibility();
  8096. DebugAssert(!ComponentVisible[fcQueueView]);
  8097. }
  8098. else
  8099. {
  8100. ComponentVisible[Component] = false;
  8101. }
  8102. }
  8103. }
  8104. break;
  8105. case WM_COPYDATA:
  8106. WMCopyData(*M);
  8107. break;
  8108. case WM_MANAGES_CAPTION:
  8109. // caption managed in TTerminalManager::UpdateAppTitle()
  8110. M->Result = 1;
  8111. break;
  8112. case WM_WANTS_MOUSEWHEEL:
  8113. M->Result = 1;
  8114. break;
  8115. case CM_SHOWINGCHANGED:
  8116. CMShowingChanged(*M);
  8117. break;
  8118. case WM_CLOSE:
  8119. WMClose(*M);
  8120. break;
  8121. case WM_DPICHANGED:
  8122. WMDpiChanged(*M);
  8123. break;
  8124. case CM_DPICHANGED:
  8125. CMDpiChanged(*M);
  8126. break;
  8127. default:
  8128. TForm::Dispatch(Message);
  8129. break;
  8130. }
  8131. }
  8132. //---------------------------------------------------------------------------
  8133. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8134. {
  8135. // noop
  8136. }
  8137. //---------------------------------------------------------------------------
  8138. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8139. {
  8140. TForm::Dispatch(&Message);
  8141. FSessionColors->Clear();
  8142. AddFixedSessionImages();
  8143. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8144. UpdateImages();
  8145. }
  8146. //---------------------------------------------------------------------------
  8147. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8148. {
  8149. TForm::Dispatch(&Message);
  8150. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8151. UpdatePixelsPerInchMainWindowCounter();
  8152. }
  8153. //---------------------------------------------------------------------------
  8154. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8155. {
  8156. // Cannot close window while we are busy.
  8157. // We cannot test this in FormCloseQuery as that is called also from
  8158. // Close(), which is called by CloseApplicationAction. So we can be busy
  8159. // there already even, when it is legitimate to close the application.
  8160. // Possibly a better place to handle this would be WMSysCommand.
  8161. if (NonVisualDataModule->Busy)
  8162. {
  8163. Message.Result = 1;
  8164. }
  8165. else
  8166. {
  8167. TForm::Dispatch(&Message);
  8168. }
  8169. }
  8170. //---------------------------------------------------------------------------
  8171. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8172. {
  8173. TForm::Dispatch(&Message);
  8174. if (Showing && (Terminal == NULL))
  8175. {
  8176. // When we are starting minimized (i.e. from an installer),
  8177. // postpone showing Login dialog until we get restored.
  8178. // Otherwise the Login dialog (and Authentication window) show restored
  8179. // over invidible (minimized) main window.
  8180. if (WindowState == wsMinimized)
  8181. {
  8182. FNeedSession = true;
  8183. }
  8184. else
  8185. {
  8186. // This happens before application ever goes idle, so the toolbars would
  8187. // stay enabled (initial state) until the Login dialog is dismissed.
  8188. UpdateActions();
  8189. NonVisualDataModule->StartBusy();
  8190. try
  8191. {
  8192. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8193. // otherwise the dialog does not receive focus.
  8194. // With Commander interface the ProcessMessages is called already
  8195. // by TDriveView, but with Explorer interface, we need to call it explicily
  8196. Application->ProcessMessages();
  8197. // do not reload sessions, they have been loaded just now (optimization)
  8198. NeedSession(false);
  8199. }
  8200. __finally
  8201. {
  8202. NonVisualDataModule->EndBusy();
  8203. }
  8204. }
  8205. }
  8206. }
  8207. //---------------------------------------------------------------------------
  8208. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8209. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8210. int & MaxHeight)
  8211. {
  8212. // workaround for bug in TWinControl.CalcConstraints
  8213. // Check for empty rect (restore from iconic state) is done there only after
  8214. // call to AdjustClientRect, which enlarges the rect (for forms).
  8215. TRect R = GetClientRect();
  8216. // when restoring from iconic state, place no restrictions
  8217. if (IsRectEmpty(R))
  8218. {
  8219. MinWidth = 0;
  8220. MinHeight = 0;
  8221. MaxWidth = 32000;
  8222. MaxHeight = 32000;
  8223. }
  8224. }
  8225. //---------------------------------------------------------------------------
  8226. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8227. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8228. {
  8229. // terminal can be already closed (e.g. dropped connection)
  8230. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8231. {
  8232. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8233. try
  8234. {
  8235. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8236. }
  8237. catch(...)
  8238. {
  8239. if (!Terminal->Active)
  8240. {
  8241. Close = true;
  8242. }
  8243. throw;
  8244. }
  8245. }
  8246. }
  8247. //---------------------------------------------------------------------------
  8248. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8249. {
  8250. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8251. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8252. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8253. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8254. {
  8255. OnGetSpaceAvailable = GetSpaceAvailable;
  8256. }
  8257. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8258. OnGetSpaceAvailable);
  8259. }
  8260. //---------------------------------------------------------------------------
  8261. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8262. {
  8263. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8264. Terminal->FillSessionDataForCode(Data.get());
  8265. return Data.release();
  8266. }
  8267. //---------------------------------------------------------------------------
  8268. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8269. {
  8270. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8271. DoGenerateUrlDialog(Data.get(), Paths);
  8272. }
  8273. //---------------------------------------------------------------------------
  8274. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8275. {
  8276. GenerateUrl(NULL);
  8277. }
  8278. //---------------------------------------------------------------------------
  8279. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8280. {
  8281. std::unique_ptr<TStrings> Paths(new TStringList());
  8282. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8283. GenerateUrl(Paths.get());
  8284. }
  8285. //---------------------------------------------------------------------------
  8286. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8287. {
  8288. if (value != FSessionColor)
  8289. {
  8290. FSessionColor = value;
  8291. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8292. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8293. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8294. DebugAssert(ColorItem != NULL);
  8295. ColorItem->Color = C;
  8296. NonVisualDataModule->ColorMenuItem->Color = C;
  8297. // Is null when called from LastTerminalClosed
  8298. if (Terminal != NULL)
  8299. {
  8300. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8301. }
  8302. UpdateControls();
  8303. }
  8304. }
  8305. //---------------------------------------------------------------------------
  8306. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8307. {
  8308. bool Result = FNoteTimer->Enabled;
  8309. if (Result)
  8310. {
  8311. // cannot cancel note too early
  8312. bool NotEarly =
  8313. (Now() - FNoteShown >
  8314. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8315. if (Force || NotEarly)
  8316. {
  8317. FNoteTimer->Enabled = false;
  8318. FNote = L"";
  8319. SAFE_DESTROY(FNoteData);
  8320. FOnNoteClick = NULL;
  8321. FNoteHints = FNotes->Text;
  8322. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8323. UpdateStatusBar();
  8324. }
  8325. }
  8326. return Result;
  8327. }
  8328. //---------------------------------------------------------------------------
  8329. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8330. {
  8331. DebugAssert(FNoteTimer->Enabled);
  8332. CancelNote(true);
  8333. }
  8334. //---------------------------------------------------------------------------
  8335. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8336. {
  8337. int P = Note.Pos(L"\n");
  8338. if (P > 0)
  8339. {
  8340. Note.SetLength(P - 1);
  8341. }
  8342. FNotes->Add(FORMAT(L"[%s] %s",
  8343. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8344. while (FNotes->Count > 10)
  8345. {
  8346. FNotes->Delete(0);
  8347. }
  8348. if (UpdateNow)
  8349. {
  8350. FNoteHints = FNotes->Text;
  8351. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8352. UpdateStatusBar();
  8353. }
  8354. }
  8355. //---------------------------------------------------------------------------
  8356. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8357. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8358. {
  8359. int P = Note.Pos(L"\n");
  8360. if (P > 0)
  8361. {
  8362. Note.SetLength(P - 1);
  8363. }
  8364. FNoteHints = FNotes->Text;
  8365. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8366. FNote = Note;
  8367. SAFE_DESTROY(FNoteData);
  8368. FNoteData = NoteData;
  8369. FOnNoteClick = OnNoteClick;
  8370. AddNote(Note, false);
  8371. UpdateStatusBar();
  8372. FNoteShown = Now();
  8373. FNoteTimer->Enabled = false;
  8374. if (Seconds == 0)
  8375. {
  8376. Seconds = WinConfiguration->NotificationsTimeout;
  8377. }
  8378. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8379. FNoteTimer->Enabled = true;
  8380. }
  8381. //---------------------------------------------------------------------------
  8382. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8383. {
  8384. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8385. }
  8386. //---------------------------------------------------------------------------
  8387. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8388. {
  8389. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  8390. {
  8391. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8392. }
  8393. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  8394. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  8395. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  8396. 0, NULL, NULL);
  8397. }
  8398. //---------------------------------------------------------------------------
  8399. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  8400. {
  8401. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  8402. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  8403. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  8404. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  8405. }
  8406. //---------------------------------------------------------------------------
  8407. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  8408. {
  8409. WinConfiguration->FormatSizeBytes = Style;
  8410. }
  8411. //---------------------------------------------------------------------------
  8412. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  8413. {
  8414. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  8415. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  8416. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  8417. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  8418. }
  8419. //---------------------------------------------------------------------------
  8420. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  8421. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  8422. {
  8423. if (Panel->Index == 0)
  8424. {
  8425. if (FOnNoteClick != NULL)
  8426. {
  8427. // prevent the user data from being freed by possible call
  8428. // to CancelNote or PostNote during call to OnNoteClick
  8429. std::unique_ptr<TObject> NoteData(FNoteData);
  8430. TNotifyEvent OnNoteClick = FOnNoteClick;
  8431. FNoteData = NULL;
  8432. // need to cancel the note as we are going to delete its user data
  8433. CancelNote(true);
  8434. OnNoteClick(NoteData.get());
  8435. }
  8436. }
  8437. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  8438. {
  8439. FileSystemInfo();
  8440. }
  8441. }
  8442. //---------------------------------------------------------------------------
  8443. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  8444. {
  8445. // workaround:
  8446. // 1) for unknown reason, disabling window, while minimized,
  8447. // prevents it from restoring, even if it was enabled again meanwhile
  8448. // 2) when disabling the main window, while another has focus
  8449. // minimize is no longer possible
  8450. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  8451. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  8452. // Shouldn't we use IsApplicationMinimized() here?
  8453. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  8454. {
  8455. Enabled = false;
  8456. }
  8457. FLockLevel++;
  8458. }
  8459. //---------------------------------------------------------------------------
  8460. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  8461. {
  8462. DebugAssert(FLockLevel > 0);
  8463. FLockLevel--;
  8464. if (FLockLevel == 0)
  8465. {
  8466. DebugAssert(FLockSuspendLevel == 0);
  8467. Enabled = true;
  8468. }
  8469. }
  8470. //---------------------------------------------------------------------------
  8471. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  8472. {
  8473. // We need to make sure that window is enabled when the last modal window closes
  8474. // otherwise focus is not restored correctly.
  8475. // So we re-enable the window when modal window opens and
  8476. // disable it back after is closes
  8477. if (FLockLevel > 0)
  8478. {
  8479. // while we have nesting counter, we know that we never be called
  8480. // recursivelly as Application->OnModalBegin is called only
  8481. // for the top-level modal window
  8482. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8483. {
  8484. // won't be disabled when conditions in LockWindow() were not satisfied
  8485. FDisabledOnLockSuspend = !Enabled;
  8486. // When minimized to tray (or actually when set to SW_HIDE),
  8487. // setting Enabled makes the window focusable even when there's
  8488. // modal window over it
  8489. if (!FTrayIcon->Visible)
  8490. {
  8491. Enabled = true;
  8492. }
  8493. }
  8494. FLockSuspendLevel++;
  8495. }
  8496. }
  8497. //---------------------------------------------------------------------------
  8498. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  8499. {
  8500. if (FLockSuspendLevel > 0)
  8501. {
  8502. DebugAssert(FLockLevel > 0);
  8503. FLockSuspendLevel--;
  8504. // see comment in SuspendWindowLock
  8505. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8506. {
  8507. // Note that window can be enabled here, when we were minized to tray when
  8508. // was SuspendWindowLock() called.
  8509. // We should possibly do the same check as in LockWindow(),
  8510. // if it is ever possible that the consitions change between
  8511. // SuspendWindowLock() and ResumeWindowLock()
  8512. Enabled = !FDisabledOnLockSuspend;
  8513. }
  8514. }
  8515. }
  8516. //---------------------------------------------------------------------------
  8517. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  8518. {
  8519. if (!TextOnly)
  8520. {
  8521. TTBXComboBoxItem * RemotePathComboBox =
  8522. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  8523. TStrings * Items = RemotePathComboBox->Strings;
  8524. Items->BeginUpdate();
  8525. try
  8526. {
  8527. Items->Clear();
  8528. if (Terminal != NULL)
  8529. {
  8530. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8531. while (!IsUnixRootPath(APath))
  8532. {
  8533. int P = APath.LastDelimiter(L'/');
  8534. DebugAssert(P >= 0);
  8535. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  8536. APath.SetLength(P - 1);
  8537. }
  8538. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  8539. }
  8540. }
  8541. __finally
  8542. {
  8543. RemotePathComboBox->ItemIndex = Items->Count - 1;
  8544. // Setting ItemIndex to -1 does not reset its text
  8545. if (Items->Count == 0)
  8546. {
  8547. RemotePathComboBox->Text = L"";
  8548. }
  8549. Items->EndUpdate();
  8550. }
  8551. }
  8552. }
  8553. //---------------------------------------------------------------------------
  8554. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  8555. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  8556. int & ImageIndex)
  8557. {
  8558. if (AIndex < 0)
  8559. {
  8560. AIndex = Sender->ItemIndex;
  8561. }
  8562. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  8563. }
  8564. //---------------------------------------------------------------------------
  8565. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  8566. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  8567. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  8568. {
  8569. ARect.Left += (10 * AIndex);
  8570. }
  8571. //---------------------------------------------------------------------------
  8572. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  8573. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  8574. {
  8575. AWidth += (10 * AIndex);
  8576. }
  8577. //---------------------------------------------------------------------------
  8578. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  8579. TObject * Sender)
  8580. {
  8581. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  8582. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8583. int Index = RemotePathComboBox->ItemIndex;
  8584. while (Index < RemotePathComboBox->Strings->Count - 1)
  8585. {
  8586. APath = UnixExtractFileDir(APath);
  8587. Index++;
  8588. }
  8589. // VanDyke style paths
  8590. if (APath.IsEmpty())
  8591. {
  8592. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  8593. APath = ROOTDIRECTORY;
  8594. }
  8595. if (RemoteDirView->Path != APath)
  8596. {
  8597. RemoteDirView->Path = APath;
  8598. }
  8599. }
  8600. //---------------------------------------------------------------------------
  8601. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  8602. {
  8603. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  8604. DebugUsedParam(Sender);
  8605. UpdateRemotePathComboBox(true);
  8606. }
  8607. //---------------------------------------------------------------------------
  8608. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  8609. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  8610. {
  8611. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8612. DebugAssert(DirView != NULL);
  8613. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  8614. {
  8615. HWND Edit = ListView_GetEditControl(DirView->Handle);
  8616. // OnEditing is called also from TCustomListView::CanEdit
  8617. if (Edit != NULL)
  8618. {
  8619. EditSelectBaseName(Edit);
  8620. }
  8621. }
  8622. }
  8623. //---------------------------------------------------------------------------
  8624. //---------------------------------------------------------------------------
  8625. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  8626. {
  8627. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  8628. Result->AutoDetectDnD = false;
  8629. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  8630. Result->RenderDataOn = rdoEnterAndDropSync;
  8631. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  8632. return Result;
  8633. }
  8634. //---------------------------------------------------------------------------
  8635. void __fastcall TCustomScpExplorerForm::CreateWnd()
  8636. {
  8637. TForm::CreateWnd();
  8638. if (FSessionsDragDropFilesEx == NULL)
  8639. {
  8640. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  8641. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  8642. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  8643. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  8644. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  8645. }
  8646. if (FQueueDragDropFilesEx == NULL)
  8647. {
  8648. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  8649. // No need to set OnDragOver as we do not have any restrictions
  8650. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  8651. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  8652. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  8653. }
  8654. }
  8655. //---------------------------------------------------------------------------
  8656. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  8657. {
  8658. TForm::DestroyWnd();
  8659. FSessionsDragDropFilesEx->DragDropControl = NULL;
  8660. FQueueDragDropFilesEx->DragDropControl = NULL;
  8661. }
  8662. //---------------------------------------------------------------------------
  8663. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  8664. {
  8665. SideEnter(FCurrentSide);
  8666. FEverShown = true;
  8667. }
  8668. //---------------------------------------------------------------------------
  8669. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  8670. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  8671. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  8672. {
  8673. if (!NonVisualDataModule->Busy)
  8674. {
  8675. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8676. Configuration->Usage->Inc(L"FileFinds");
  8677. LockWindow(true);
  8678. NonVisualDataModule->StartBusy();
  8679. WinConfiguration->LockedInterface = true;
  8680. try
  8681. {
  8682. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  8683. }
  8684. __finally
  8685. {
  8686. WinConfiguration->LockedInterface = false;
  8687. NonVisualDataModule->EndBusy();
  8688. UnlockWindow();
  8689. }
  8690. }
  8691. }
  8692. //---------------------------------------------------------------------------
  8693. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  8694. {
  8695. if (!NonVisualDataModule->Busy)
  8696. {
  8697. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8698. SetFocus();
  8699. RemoteDirView->Path = UnixExtractFilePath(Path);
  8700. TListItem * Item = RemoteDirView->FindFileItem(UnixExtractFileName(Path));
  8701. if (Item != NULL)
  8702. {
  8703. RemoteDirView->ItemFocused = Item;
  8704. RemoteDirView->ItemFocused->MakeVisible(false);
  8705. RemoteDirView->SetFocus();
  8706. }
  8707. }
  8708. }
  8709. //---------------------------------------------------------------------------
  8710. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  8711. {
  8712. bool Result = !NonVisualDataModule->Busy;
  8713. if (Result)
  8714. {
  8715. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8716. }
  8717. return Result;
  8718. }
  8719. //---------------------------------------------------------------------------
  8720. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  8721. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8722. {
  8723. if (CanOperateOnFoundFiles(ATerminal))
  8724. {
  8725. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  8726. FOnFileOperationFinished = OnFileOperationFinished;
  8727. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  8728. }
  8729. }
  8730. //---------------------------------------------------------------------------
  8731. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  8732. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8733. {
  8734. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  8735. }
  8736. //---------------------------------------------------------------------------
  8737. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  8738. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8739. {
  8740. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  8741. }
  8742. //---------------------------------------------------------------------------
  8743. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  8744. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8745. {
  8746. if (CanOperateOnFoundFiles(ATerminal))
  8747. {
  8748. for (int Index = 0; Index < FileList->Count; Index++)
  8749. {
  8750. UnicodeString FileName = FileList->Strings[Index];
  8751. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  8752. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  8753. OnFileOperationFinished(FileName, true);
  8754. }
  8755. }
  8756. }
  8757. //---------------------------------------------------------------------------
  8758. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  8759. {
  8760. FFileFindTerminal = Terminal;
  8761. ShowFileFindDialog(
  8762. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  8763. DoDownloadFoundFiles, DoEditFoundFiles);
  8764. }
  8765. //---------------------------------------------------------------------------
  8766. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  8767. {
  8768. FTaskbarList = TaskbarList;
  8769. }
  8770. //---------------------------------------------------------------------------
  8771. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  8772. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  8773. {
  8774. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8775. if (Index >= 0)
  8776. {
  8777. if (Button == mbLeft)
  8778. {
  8779. // "Mouse down" is raised only after tab is switched.
  8780. // If switching tab (switching session) takes long enough for user
  8781. // to actually release the button, "mouse down" is still raised,
  8782. // but we do not get "mouse up" event, so dragging is not cancelled,
  8783. // prevent that by not beginning dragging in the first place.
  8784. if (IsKeyPressed(VK_LBUTTON))
  8785. {
  8786. // when user clicks the "+", we get mouse down only after the session
  8787. // is closed, when new session tab is already on X:Y, so dragging
  8788. // starts, prevent that
  8789. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  8790. {
  8791. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8792. if (Terminal != NULL)
  8793. {
  8794. SessionsPageControl->BeginDrag(false);
  8795. }
  8796. }
  8797. }
  8798. }
  8799. else if (Button == mbMiddle)
  8800. {
  8801. // ignore middle-click for "New session tab"
  8802. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8803. if (Terminal != NULL)
  8804. {
  8805. SessionsPageControl->ActivePageIndex = Index;
  8806. // Switch to session tab (has to be session tab, due to previous check)
  8807. if (DebugAlwaysTrue(SessionTabSwitched()))
  8808. {
  8809. CloseSession();
  8810. }
  8811. }
  8812. }
  8813. }
  8814. }
  8815. //---------------------------------------------------------------------------
  8816. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  8817. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  8818. {
  8819. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8820. // do not allow dropping on the "+" tab
  8821. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8822. if ((TargetTerminal != NULL) &&
  8823. (SessionsPageControl->ActivePage->PageIndex != Index))
  8824. {
  8825. Configuration->Usage->Inc(L"SessionTabMoves");
  8826. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  8827. // but we want to actually prevent that to avoid flicker
  8828. SessionsPageControl->ActivePage->PageIndex = Index;
  8829. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  8830. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  8831. }
  8832. }
  8833. //---------------------------------------------------------------------------
  8834. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  8835. TObject * Sender, TObject * Source, int X, int Y,
  8836. TDragState /*State*/, bool & Accept)
  8837. {
  8838. Accept = (Sender == Source);
  8839. if (Accept)
  8840. {
  8841. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8842. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8843. // do not allow dragging to the "+" tab
  8844. Accept = (Terminal != NULL);
  8845. }
  8846. }
  8847. //---------------------------------------------------------------------------
  8848. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(int /*KeyState*/,
  8849. const TPoint & Point, int & Effect)
  8850. {
  8851. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  8852. if (Index < 0)
  8853. {
  8854. Effect = DROPEFFECT_None;
  8855. }
  8856. else
  8857. {
  8858. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8859. // do not allow dropping on the "+" tab
  8860. if (TargetTerminal == NULL)
  8861. {
  8862. Effect = DROPEFFECT_None;
  8863. }
  8864. else if ((TargetTerminal != Terminal) && (Effect == DROPEFFECT_Move))
  8865. {
  8866. Effect = DROPEFFECT_Copy;
  8867. }
  8868. }
  8869. }
  8870. //---------------------------------------------------------------------------
  8871. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  8872. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  8873. {
  8874. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  8875. // do not allow dropping on the "+" tab
  8876. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8877. if (TargetTerminal != NULL)
  8878. {
  8879. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  8880. if (!IsFileControl(DropSourceControl, osRemote))
  8881. {
  8882. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  8883. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  8884. // Why don't we use Terminal->CurrentDirectory directly?
  8885. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory, false);
  8886. }
  8887. }
  8888. }
  8889. //---------------------------------------------------------------------------
  8890. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  8891. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  8892. {
  8893. // Downloads are handled in RemoteFileControlDDEnd
  8894. if (!IsFileControl(DropSourceControl, osRemote))
  8895. {
  8896. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  8897. Terminal->CurrentDirectory,
  8898. // force queue
  8899. true);
  8900. }
  8901. }
  8902. //---------------------------------------------------------------------------
  8903. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  8904. {
  8905. FShowing = false;
  8906. // Do not save empty workspace
  8907. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  8908. {
  8909. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  8910. UnicodeString Name = WorkspaceName();
  8911. DoSaveWorkspace(
  8912. Name, DataList.get(),
  8913. !Configuration->DisablePasswordStoring &&
  8914. WinConfiguration->AutoSaveWorkspacePasswords, false);
  8915. WinConfiguration->LastStoredSession = Name;
  8916. }
  8917. }
  8918. //---------------------------------------------------------------------------
  8919. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  8920. {
  8921. TManagedTerminal * ManagedTerminal =
  8922. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  8923. if (ManagedTerminal != NULL)
  8924. {
  8925. ManagedTerminal->DirectoryLoaded = Now();
  8926. }
  8927. }
  8928. //---------------------------------------------------------------------------
  8929. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  8930. TListItem * /*Item*/, bool /*Selected*/)
  8931. {
  8932. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8933. switch (DirView->LastSelectMethod)
  8934. {
  8935. case smKeyboard:
  8936. Configuration->Usage->Inc(L"KeyboardSelections");
  8937. break;
  8938. case smMouse:
  8939. Configuration->Usage->Inc(L"MouseSelections");
  8940. break;
  8941. }
  8942. }
  8943. //---------------------------------------------------------------------------
  8944. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  8945. {
  8946. if (Color != 0)
  8947. {
  8948. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  8949. }
  8950. else
  8951. {
  8952. return FSessionTabImageIndex;
  8953. }
  8954. }
  8955. //---------------------------------------------------------------------------
  8956. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  8957. {
  8958. TPngImageCollectionItem * Item =
  8959. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  8960. FSessionColors->AddPng(Item->PngImage);
  8961. return FSessionColors->Count - 1;
  8962. }
  8963. //---------------------------------------------------------------------------
  8964. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  8965. {
  8966. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  8967. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  8968. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  8969. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  8970. }
  8971. //---------------------------------------------------------------------------
  8972. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  8973. {
  8974. for (int Index = 0; Index < Control->ControlCount; Index++)
  8975. {
  8976. TControl * AControl = Control->Controls[Index];
  8977. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  8978. if (WinControl != NULL)
  8979. {
  8980. CollectItemsWithTextDisplayMode(WinControl);
  8981. }
  8982. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  8983. if (Toolbar != NULL)
  8984. {
  8985. // we care for top-level items only
  8986. TTBCustomItem * Items = Toolbar->Items;
  8987. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  8988. {
  8989. TTBCustomItem * Item = Items->Items[ItemIndex];
  8990. if (((Item->DisplayMode == nbdmImageAndText) ||
  8991. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  8992. EligibleForImageDisplayMode(Item))
  8993. {
  8994. FItemsWithTextDisplayMode.insert(Item);
  8995. }
  8996. }
  8997. }
  8998. }
  8999. }
  9000. //---------------------------------------------------------------------------
  9001. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9002. {
  9003. return true;
  9004. }
  9005. //---------------------------------------------------------------------------
  9006. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9007. {
  9008. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9009. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9010. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9011. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9012. bool Result = true;
  9013. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9014. {
  9015. TTBCustomItem * Item = *i;
  9016. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9017. if (Label != NULL)
  9018. {
  9019. // optimization
  9020. if (Label->Visible == SelectiveToolbarText)
  9021. {
  9022. Result = false;
  9023. }
  9024. else
  9025. {
  9026. Label->Visible = SelectiveToolbarText;
  9027. }
  9028. }
  9029. else
  9030. {
  9031. // optimization
  9032. if (Item->DisplayMode == DisplayMode)
  9033. {
  9034. Result = false;
  9035. }
  9036. else
  9037. {
  9038. Item->DisplayMode = DisplayMode;
  9039. }
  9040. }
  9041. ++i;
  9042. }
  9043. if (Result)
  9044. {
  9045. UpdateNewSessionTab();
  9046. }
  9047. return Result;
  9048. }
  9049. //---------------------------------------------------------------------------
  9050. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9051. {
  9052. DebugFail();
  9053. }
  9054. //---------------------------------------------------------------------------
  9055. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9056. {
  9057. // Among other, a lock level is non zero, while directory is being loaded,
  9058. // even when it happens as a result of dirview navigation,
  9059. // i.e. when TNonVisualDataModule is NOT FBusy.
  9060. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9061. // Among other this prevents a panel auto update to occur while
  9062. // directory is changing.
  9063. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9064. }
  9065. //---------------------------------------------------------------------------
  9066. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  9067. {
  9068. // While the window is disabled, we still seem to process menu shortcuts at least,
  9069. // so stop it at least here.
  9070. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9071. return
  9072. (Allowed == aaUpdate) ||
  9073. !NonVisualDataModule->Busy;
  9074. }
  9075. //---------------------------------------------------------------------------
  9076. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9077. {
  9078. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9079. }
  9080. //---------------------------------------------------------------------------
  9081. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9082. {
  9083. // This is somewhat redundant to LockWindow() call from
  9084. // TTerminalManager::TerminalReadDirectoryProgress.
  9085. // But disabling window is known not to block keyboard shorcuts
  9086. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9087. if (Busy > 0)
  9088. {
  9089. if (NonVisualDataModule->Busy)
  9090. {
  9091. State = false;
  9092. }
  9093. else
  9094. {
  9095. NonVisualDataModule->StartBusy();
  9096. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9097. LockWindow();
  9098. }
  9099. }
  9100. else if (Busy < 0)
  9101. {
  9102. UnlockWindow();
  9103. NonVisualDataModule->EndBusy();
  9104. }
  9105. else
  9106. {
  9107. State = NonVisualDataModule->Busy;
  9108. }
  9109. }
  9110. //---------------------------------------------------------------------------
  9111. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9112. {
  9113. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9114. // no context menu for "New session tab"
  9115. if ((Index >= 0) && (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != NULL))
  9116. {
  9117. SessionsPageControl->ActivePageIndex = Index;
  9118. if (DebugAlwaysTrue(SessionTabSwitched()))
  9119. {
  9120. // copied from TControl.WMContextMenu
  9121. SendCancelMode(SessionsPageControl);
  9122. // explicit popup instead of using PopupMenu property
  9123. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9124. // while connecting yet not-connected session and hence
  9125. // allowing an access to commands over not-completelly connected session
  9126. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9127. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  9128. PopupMenu->PopupComponent = SessionsPageControl;
  9129. PopupMenu->Popup(Point.x, Point.y);
  9130. }
  9131. }
  9132. Handled = true;
  9133. }
  9134. //---------------------------------------------------------------------------
  9135. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9136. {
  9137. return (Terminal != NULL) && Terminal->Active && Terminal->IsCapable[fcChangePassword];
  9138. }
  9139. //---------------------------------------------------------------------------
  9140. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9141. {
  9142. Configuration->Usage->Inc(L"PasswordChanges");
  9143. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9144. ChangePasswordData->DisableAuthentationsExceptPassword();
  9145. ChangePasswordData->ChangePassword = true;
  9146. std::unique_ptr<TTerminal> ChangePasswordSession(
  9147. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9148. ChangePasswordSession->AutoReadDirectory = false;
  9149. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9150. {
  9151. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9152. }
  9153. else
  9154. {
  9155. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9156. }
  9157. }
  9158. //---------------------------------------------------------------------------
  9159. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9160. {
  9161. // No nice way to assert SSH2
  9162. return (Terminal != NULL) && Terminal->Active && (Terminal->FSProtocol == cfsSFTP);
  9163. }
  9164. //---------------------------------------------------------------------------
  9165. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9166. {
  9167. TOperationVisualizer OperationVisualizer;
  9168. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9169. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9170. }
  9171. //---------------------------------------------------------------------------
  9172. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9173. {
  9174. TForm::ChangeScale(M, D);
  9175. int APixelsPerInch = GetControlPixelsPerInch(this);
  9176. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9177. try
  9178. {
  9179. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9180. }
  9181. __finally
  9182. {
  9183. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9184. }
  9185. }
  9186. //---------------------------------------------------------------------------
  9187. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9188. {
  9189. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9190. }
  9191. //---------------------------------------------------------------------
  9192. class TPublicControl : public TControl
  9193. {
  9194. friend class TCustomScpExplorerForm;
  9195. };
  9196. //---------------------------------------------------------------------------
  9197. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9198. {
  9199. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9200. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9201. }
  9202. //---------------------------------------------------------------------------
  9203. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9204. {
  9205. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9206. OpenBookmark(Side, Bookmark);
  9207. }
  9208. //---------------------------------------------------------------------------
  9209. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9210. {
  9211. DoBookmarkClick(osLocal, Sender);
  9212. }
  9213. //---------------------------------------------------------------------------
  9214. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9215. {
  9216. DoBookmarkClick(osRemote, Sender);
  9217. }
  9218. //---------------------------------------------------------------------------
  9219. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9220. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9221. {
  9222. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9223. if (!WinConfiguration->UseLocationProfiles)
  9224. {
  9225. std::unique_ptr<TStringList> Directories(new TStringList());
  9226. Directories->CaseSensitive = (Side == osRemote);
  9227. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9228. {
  9229. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9230. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9231. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9232. {
  9233. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9234. Item->Caption = EscapeHotkey(Directory);
  9235. Item->ShortCut = Bookmark->ShortCut;
  9236. Item->OnClick = OnBookmarkClick;
  9237. Item->Tag = reinterpret_cast<int>(Bookmark);
  9238. Directories->Add(Directory);
  9239. Menu->Add(Item.release());
  9240. }
  9241. }
  9242. }
  9243. else
  9244. {
  9245. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9246. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9247. {
  9248. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9249. if (!Bookmark->Node.IsEmpty())
  9250. {
  9251. Folders->Add(Bookmark->Node);
  9252. }
  9253. }
  9254. for (int Index = 0; Index < Folders->Count; Index++)
  9255. {
  9256. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9257. Item->Caption = Folders->Strings[Index];
  9258. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction->ImageIndex;
  9259. Folders->Objects[Index] = Item.get();
  9260. Menu->Add(Item.release());
  9261. }
  9262. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9263. {
  9264. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9265. TTBCustomItem * Parent;
  9266. if (!Bookmark->Node.IsEmpty())
  9267. {
  9268. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9269. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9270. }
  9271. else
  9272. {
  9273. Parent = Menu;
  9274. }
  9275. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9276. Item->Caption = Bookmark->Name;
  9277. Item->ShortCut = Bookmark->ShortCut;
  9278. Item->OnClick = OnBookmarkClick;
  9279. Item->Tag = reinterpret_cast<int>(Bookmark);
  9280. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9281. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9282. {
  9283. // No hint for location profiles that are actually mere "bookmarks"
  9284. }
  9285. else
  9286. {
  9287. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  9288. UnicodeString LongHint;
  9289. if (!Bookmark->Local.IsEmpty())
  9290. {
  9291. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  9292. }
  9293. if (!Bookmark->Remote.IsEmpty())
  9294. {
  9295. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  9296. }
  9297. AddToList(Hint, LongHint, L"|");
  9298. Item->Hint = Hint;
  9299. }
  9300. Parent->Add(Item.release());
  9301. }
  9302. }
  9303. }
  9304. //---------------------------------------------------------------------------
  9305. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  9306. {
  9307. Menu->Clear();
  9308. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  9309. AddMenuSeparator(Menu);
  9310. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  9311. AddMenuSeparator(Menu);
  9312. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9313. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction : NonVisualDataModule->RemoteAddBookmarkAction;
  9314. Menu->Add(Item.release());
  9315. }
  9316. //---------------------------------------------------------------------------
  9317. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  9318. {
  9319. bool Result = true;
  9320. try
  9321. {
  9322. if (Side == osRemote)
  9323. {
  9324. Terminal->ExceptionOnFail = true;
  9325. }
  9326. try
  9327. {
  9328. DirView(Side)->Path = Path;
  9329. }
  9330. __finally
  9331. {
  9332. if (Side == osRemote)
  9333. {
  9334. Terminal->ExceptionOnFail = false;
  9335. }
  9336. }
  9337. }
  9338. catch (Exception & E)
  9339. {
  9340. if ((Side != osRemote) || Terminal->Active)
  9341. {
  9342. ShowExtendedExceptionEx(Terminal, &E);
  9343. Result = false;
  9344. }
  9345. else
  9346. {
  9347. throw;
  9348. }
  9349. }
  9350. return Result;
  9351. }
  9352. //---------------------------------------------------------------------------
  9353. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  9354. {
  9355. // Make sure there some feedback even when loading tine local folders
  9356. TInstantOperationVisualizer Visualizer;
  9357. DirView(Side)->ReloadDirectory();
  9358. }
  9359. //---------------------------------------------------------------------------
  9360. void __fastcall TCustomScpExplorerForm::SessionsPageControlCloseButtonClick(TPageControl * /*Sender*/, int Index)
  9361. {
  9362. if (Index == SessionsPageControl->TabIndex)
  9363. {
  9364. CloseSession();
  9365. }
  9366. else
  9367. {
  9368. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  9369. TTerminal * Terminal = GetSessionTabTerminal(TabSheet);
  9370. TTerminalManager * Manager = TTerminalManager::Instance();
  9371. if (!Terminal->Active)
  9372. {
  9373. Manager->FreeTerminal(Terminal);
  9374. }
  9375. else
  9376. {
  9377. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  9378. if (CanCloseQueue(Queue))
  9379. {
  9380. Manager->FreeTerminal(Terminal);
  9381. }
  9382. }
  9383. }
  9384. }
  9385. //---------------------------------------------------------------------------
  9386. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side)
  9387. {
  9388. if (DebugAlwaysTrue(GetSide(Side) == osRemote))
  9389. {
  9390. TInstantOperationVisualizer Visualizer;
  9391. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  9392. // before we create a new data object
  9393. ClipboardClear();
  9394. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  9395. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  9396. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  9397. FClipboardFakeMonitors.reset(StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated));
  9398. if (FClipboardDragDropFilesEx.get() == NULL)
  9399. {
  9400. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  9401. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  9402. }
  9403. FClipboardDragDropFilesEx->FileList->Clear();
  9404. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  9405. FClipboardDragDropFilesEx->CopyToClipboard();
  9406. FClipboardTerminal = FTerminal;
  9407. // Need full paths, as cwd can be different once files are pasted
  9408. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(false, true)));
  9409. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  9410. {
  9411. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  9412. }
  9413. }
  9414. }
  9415. //---------------------------------------------------------------------------
  9416. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  9417. {
  9418. if (OpenClipboard(0))
  9419. {
  9420. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  9421. CloseClipboard();
  9422. }
  9423. }
  9424. //---------------------------------------------------------------------------
  9425. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  9426. {
  9427. return !FClipboardFakeDirectory.IsEmpty();
  9428. }
  9429. //---------------------------------------------------------------------------
  9430. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  9431. {
  9432. RemoveDir(ApiPath(FClipboardFakeDirectory));
  9433. FClipboardFakeDirectory = UnicodeString();
  9434. FClipboardTerminal = NULL;
  9435. // Also called in Idle()
  9436. FClipboardFakeMonitors.reset(NULL);
  9437. }
  9438. //---------------------------------------------------------------------------
  9439. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  9440. {
  9441. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  9442. {
  9443. ClipboardStop();
  9444. }
  9445. }
  9446. //---------------------------------------------------------------------------
  9447. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  9448. {
  9449. TTerminalManager * Manager = TTerminalManager::Instance();
  9450. Manager->ActiveTerminal = FClipboardTerminal;
  9451. TTransferOperationParam Params;
  9452. Params.TargetDirectory = TargetDirectory;
  9453. Params.DragDrop = DragDrop;
  9454. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  9455. }
  9456. //---------------------------------------------------------------------------
  9457. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  9458. {
  9459. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  9460. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  9461. {
  9462. RemoveDir(ApiPath(FileName));
  9463. if (!NonVisualDataModule->Busy)
  9464. {
  9465. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  9466. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  9467. }
  9468. }
  9469. }
  9470. //---------------------------------------------------------------------------