CustomScpExplorer.cpp 329 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097
  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. delete FCustomCommandLocalFileList;
  279. delete FCustomCommandRemoteFileList;
  280. delete FCustomCommandMenu;
  281. delete FTrayIcon;
  282. FTrayIcon = NULL;
  283. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  284. delete FEditorManager;
  285. ForceCloseLocalEditors();
  286. delete FLocalEditors;
  287. HideFileFindDialog();
  288. if (FDelayedDeletionTimer)
  289. {
  290. DoDelayedDeletion(NULL);
  291. SAFE_DESTROY(FDelayedDeletionTimer);
  292. }
  293. SAFE_DESTROY(FDelayedDeletionList);
  294. // sometimes we do not get DDEnd so the list is not released
  295. SAFE_DESTROY(FDDFileList);
  296. DebugAssert(FSynchronizeController == NULL);
  297. CloseHandle(FDDExtMutex);
  298. FDDExtMutex = NULL;
  299. FreeLibrary(FOle32Library);
  300. FOle32Library = NULL;
  301. FDragMoveCursor = NULL;
  302. DebugAssert(!FErrorList);
  303. if (FEverShown)
  304. {
  305. // when window is never shown (like when running command-line operation),
  306. // particularly window site is not restored correctly (BoundsRect value set
  307. // in RestoreForm gets lost during handle allocation), so we do not want
  308. // it to be stored
  309. StoreParams();
  310. }
  311. Terminal = NULL;
  312. Queue = NULL;
  313. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  314. NonVisualDataModule->ScpExplorer = NULL;
  315. delete FQueueController;
  316. FQueueController = NULL;
  317. delete FQueueStatusSection;
  318. FQueueStatusSection = NULL;
  319. delete FQueueStatus;
  320. FQueueStatus = NULL;
  321. delete FUserActionTimer;
  322. FUserActionTimer = NULL;
  323. delete FNoteTimer;
  324. delete FNotes;
  325. delete FNoteData;
  326. SAFE_DESTROY(FDocks);
  327. SessionsPageControl->Images = NULL;
  328. // only after clearing Terminal (after DoTerminalListChanged => ... => AddSessionColor is called)
  329. SAFE_DESTROY(FSessionColors);
  330. SAFE_DESTROY(FSessionsDragDropFilesEx);
  331. SAFE_DESTROY(FHistoryMenu[0][0]);
  332. SAFE_DESTROY(FHistoryMenu[0][1]);
  333. SAFE_DESTROY(FHistoryMenu[1][0]);
  334. SAFE_DESTROY(FHistoryMenu[1][1]);
  335. if (FHiddenWindow != NULL)
  336. {
  337. DestroyWindow(FHiddenWindow);
  338. FHiddenWindow = NULL;
  339. }
  340. }
  341. //---------------------------------------------------------------------------
  342. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  343. {
  344. std::unique_ptr<TSessionData> Data;
  345. if (!Session.IsEmpty())
  346. {
  347. bool DefaultsOnly;
  348. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  349. }
  350. TTerminalManager * Manager = TTerminalManager::Instance();
  351. for (int Index = 0; Index < Manager->Count; Index++)
  352. {
  353. TTerminal * Terminal = Manager->Terminals[Index];
  354. if (Session.IsEmpty() ||
  355. Terminal->SessionData->IsSameSite(Data.get()))
  356. {
  357. if (Path.IsEmpty())
  358. {
  359. Terminal->ClearCaches();
  360. }
  361. else
  362. {
  363. Terminal->DirectoryModified(Path, true);
  364. }
  365. }
  366. }
  367. // We should flag a pending refresh for the background terminals or busy foreground terminals
  368. if ((Terminal != NULL) && Terminal->Active &&
  369. CanCommandLineFromAnotherInstance() &&
  370. (Session.IsEmpty() || Terminal->SessionData->IsSameSite(Data.get())) &&
  371. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  372. {
  373. Terminal->ReloadDirectory();
  374. }
  375. }
  376. //---------------------------------------------------------------------------
  377. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  378. {
  379. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  380. size_t MessageSize = sizeof(TCopyDataMessage);
  381. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  382. if (Result)
  383. {
  384. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  385. Result = (Message.Version == TCopyDataMessage::Version1);
  386. if (Result)
  387. {
  388. switch (Message.Command)
  389. {
  390. case TCopyDataMessage::CommandCanCommandLine:
  391. Result = CanCommandLineFromAnotherInstance();
  392. break;
  393. case TCopyDataMessage::CommandCommandLine:
  394. {
  395. UnicodeString CommandLine(Message.CommandLine);
  396. Result = CommandLineFromAnotherInstance(CommandLine);
  397. }
  398. break;
  399. case TCopyDataMessage::RefreshPanel:
  400. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  401. break;
  402. case TCopyDataMessage::MainWindowCheck:
  403. Result = true;
  404. break;
  405. default:
  406. Result = false;
  407. break;
  408. }
  409. }
  410. }
  411. Message.Result = Result ? 1 : 0;
  412. }
  413. //---------------------------------------------------------------------------
  414. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  415. {
  416. WNDCLASS WindowClass = {0};
  417. WindowClass.lpfnWndProc = DefWindowProc;
  418. WindowClass.hInstance = HInstance;
  419. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  420. FHiddenWindow = NULL;
  421. if (RegisterClass(&WindowClass))
  422. {
  423. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  424. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  425. }
  426. }
  427. //---------------------------------------------------------------------------
  428. bool __fastcall TCustomScpExplorerForm::CanConsole()
  429. {
  430. return (Terminal != NULL) && (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  431. }
  432. //---------------------------------------------------------------------------
  433. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  434. {
  435. bool Result = Visible && !NonVisualDataModule->Busy;
  436. return Result;
  437. }
  438. //---------------------------------------------------------------------------
  439. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  440. const UnicodeString & CommandLine)
  441. {
  442. TProgramParams Params(CommandLine);
  443. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  444. if (Result)
  445. {
  446. NonVisualDataModule->StartBusy();
  447. try
  448. {
  449. // this action is initiated from another process,
  450. // so it's likely that our window is not visible,
  451. // and user won't see what is going on
  452. Application->BringToFront();
  453. // reload sessions as we may be asked to open a session
  454. // just stored by another instance
  455. StoredSessions->Reload();
  456. UnicodeString SessionName = Params.Param[1];
  457. std::unique_ptr<TObjectList> DataList(new TObjectList());
  458. UnicodeString DownloadFile; // unused
  459. try
  460. {
  461. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this);
  462. }
  463. catch (EAbort &)
  464. {
  465. // Happens when opening session in PuTTY and in other situations,
  466. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  467. // to process the command-line on its own (or to pass it to yet another instance)
  468. }
  469. if (DataList->Count > 0)
  470. {
  471. TTerminalManager * Manager = TTerminalManager::Instance();
  472. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  473. }
  474. }
  475. __finally
  476. {
  477. NonVisualDataModule->EndBusy();
  478. }
  479. }
  480. return Result;
  481. }
  482. //---------------------------------------------------------------------------
  483. void __fastcall TCustomScpExplorerForm::SetTerminal(TTerminal * value)
  484. {
  485. if (FTerminal != value)
  486. {
  487. TerminalChanging();
  488. DoSetTerminal(value, false);
  489. }
  490. }
  491. //---------------------------------------------------------------------------
  492. void __fastcall TCustomScpExplorerForm::DoSetTerminal(TTerminal * value, bool Replace)
  493. {
  494. FTerminal = value;
  495. bool PrevAllowTransferPresetAutoSelect = FAllowTransferPresetAutoSelect;
  496. FAllowTransferPresetAutoSelect = false;
  497. try
  498. {
  499. TerminalChanged(Replace);
  500. }
  501. __finally
  502. {
  503. FAllowTransferPresetAutoSelect = PrevAllowTransferPresetAutoSelect;
  504. }
  505. if (Terminal != NULL)
  506. {
  507. TransferPresetAutoSelect();
  508. }
  509. }
  510. //---------------------------------------------------------------------------
  511. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TTerminal * value)
  512. {
  513. DoSetTerminal(value, true);
  514. }
  515. //---------------------------------------------------------------------------
  516. void __fastcall TCustomScpExplorerForm::TerminalChanging()
  517. {
  518. if (FTerminal != NULL)
  519. {
  520. UpdateTerminal(Terminal);
  521. }
  522. }
  523. //---------------------------------------------------------------------------
  524. void __fastcall TCustomScpExplorerForm::TerminalChanged(bool Replaced)
  525. {
  526. if (Replaced)
  527. {
  528. RemoteDirView->ReplaceTerminal(Terminal);
  529. }
  530. else
  531. {
  532. RemoteDirView->Terminal = Terminal;
  533. }
  534. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  535. TManagedTerminal * ManagedTerminal = NULL;
  536. if (Terminal != NULL)
  537. {
  538. if (Terminal->Active)
  539. {
  540. Terminal->RefreshDirectory();
  541. }
  542. ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  543. DebugAssert(ManagedTerminal != NULL);
  544. if (WinConfiguration->PreservePanelState)
  545. {
  546. if (ManagedTerminal->RemoteExplorerState != NULL)
  547. {
  548. RemoteDirView->RestoreState(ManagedTerminal->RemoteExplorerState);
  549. }
  550. else
  551. {
  552. RemoteDirView->ClearState();
  553. }
  554. }
  555. if (!Replaced)
  556. {
  557. InitStatusBar();
  558. }
  559. }
  560. DoTerminalListChanged();
  561. if (ManagedTerminal != NULL)
  562. {
  563. // this has to be set only after the tab is switched from DoTerminalListChanged,
  564. // otherwise we are changing color of wrong tab
  565. SessionColor = (TColor)ManagedTerminal->StateData->Color;
  566. }
  567. UpdateTransferList();
  568. // Update panels Enable state before refreshing the labels
  569. UpdateControls();
  570. }
  571. //---------------------------------------------------------------------------
  572. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  573. {
  574. if (Queue != value)
  575. {
  576. if (FQueue != NULL)
  577. {
  578. FQueue->OnListUpdate = NULL;
  579. FQueue->OnQueueItemUpdate = NULL;
  580. }
  581. FQueue = value;
  582. if (FQueue != NULL)
  583. {
  584. DebugAssert(FQueue->OnListUpdate == NULL);
  585. FQueue->OnListUpdate = QueueListUpdate;
  586. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  587. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  588. }
  589. QueueChanged();
  590. }
  591. }
  592. //---------------------------------------------------------------------------
  593. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  594. TListItem * Item)
  595. {
  596. if (FQueueActedItem == Item)
  597. {
  598. FQueueActedItem = NULL;
  599. if ((QueueView3->PopupMenu != NULL) &&
  600. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  601. {
  602. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  603. ReleaseCapture();
  604. }
  605. }
  606. if (Item->Data == FPendingQueueActionItem)
  607. {
  608. FPendingQueueActionItem = NULL;
  609. }
  610. }
  611. //---------------------------------------------------------------------------
  612. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  613. {
  614. {
  615. TGuard Guard(FQueueStatusSection);
  616. FQueueStatusInvalidated = false;
  617. if (FQueue != NULL)
  618. {
  619. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  620. }
  621. }
  622. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  623. {
  624. FMaxQueueLength = FQueueStatus->Count;
  625. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  626. }
  627. FQueueController->UpdateQueueStatus(FQueueStatus);
  628. SetQueueProgress();
  629. UpdateQueueView();
  630. bool IsEmpty = (FQueueStatus == NULL) || (FQueueStatus->Count == 0);
  631. if (IsEmpty && (Terminal != NULL))
  632. {
  633. TOnceDoneOperation OnceDoneOperation =
  634. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  635. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  636. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  637. {
  638. FQueue->Enabled = false;
  639. }
  640. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  641. {
  642. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  643. }
  644. }
  645. }
  646. //---------------------------------------------------------------------------
  647. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  648. {
  649. UnicodeString Result;
  650. if (FQueueStatus != NULL)
  651. {
  652. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  653. {
  654. TFileOperationProgressType * ProgressData =
  655. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  656. if ((ProgressData != NULL) && ProgressData->InProgress)
  657. {
  658. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  659. }
  660. }
  661. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  662. {
  663. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  664. }
  665. }
  666. return Result;
  667. }
  668. //---------------------------------------------------------------------------
  669. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  670. {
  671. ComponentVisible[fcQueueView] =
  672. (WinConfiguration->QueueView.Show == qvShow) ||
  673. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  674. (FQueueStatus != NULL) && (FQueueStatus->Count > 0));
  675. }
  676. //---------------------------------------------------------------------------
  677. void __fastcall TCustomScpExplorerForm::QueueChanged()
  678. {
  679. if (FQueueStatus != NULL)
  680. {
  681. delete FQueueStatus;
  682. FQueueStatus = NULL;
  683. }
  684. UpdateQueueStatus(true);
  685. }
  686. //---------------------------------------------------------------------------
  687. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  688. {
  689. if (FQueue == Queue)
  690. {
  691. FQueueStatusInvalidated = true;
  692. }
  693. }
  694. //---------------------------------------------------------------------------
  695. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  696. TQueueItem * Item)
  697. {
  698. if (FQueue == Queue)
  699. {
  700. TGuard Guard(FQueueStatusSection);
  701. // this may be running in parallel with QueueChanged
  702. if (FQueueStatus != NULL)
  703. {
  704. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  705. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  706. {
  707. FRefreshLocalDirectory = (QueueItem == NULL) ||
  708. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  709. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  710. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  711. }
  712. if (QueueItem != NULL)
  713. {
  714. QueueItem->UserData = (void*)true;
  715. FQueueItemInvalidated = true;
  716. }
  717. }
  718. }
  719. }
  720. //---------------------------------------------------------------------------
  721. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  722. {
  723. // during standalone editing, we have no way to see/control queue,
  724. // so we have to always popup prompts automatically
  725. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  726. }
  727. //---------------------------------------------------------------------------
  728. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  729. {
  730. if (FQueueStatus != NULL)
  731. {
  732. bool QueueAutoPopup = IsQueueAutoPopup();
  733. bool NeedRefresh = FQueueController->NeedRefresh();
  734. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  735. FQueueItemInvalidated = false;
  736. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  737. bool Updated = false;
  738. TQueueItemProxy * QueueItem;
  739. bool WasUserAction;
  740. for (int Index = 0; Index < Limit; Index++)
  741. {
  742. bool Update = false;
  743. QueueItem = FQueueStatus->Items[Index];
  744. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  745. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  746. (FPendingQueueActionItem == NULL))
  747. {
  748. FPendingQueueActionItem = QueueItem;
  749. FUserActionTimer->Enabled = true;
  750. }
  751. if ((bool)QueueItem->UserData)
  752. {
  753. QueueItem->UserData = (void*)false;
  754. QueueItem->Update();
  755. Updated = true;
  756. Update = true;
  757. }
  758. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  759. {
  760. Update = true;
  761. }
  762. if (Update)
  763. {
  764. FQueueController->RefreshQueueItem(QueueItem);
  765. }
  766. }
  767. if (Updated)
  768. {
  769. NonVisualDataModule->UpdateNonVisibleActions();
  770. SetQueueProgress();
  771. }
  772. }
  773. }
  774. //---------------------------------------------------------------------------
  775. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  776. {
  777. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  778. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  779. }
  780. //---------------------------------------------------------------------------
  781. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  782. {
  783. if (ProgressData->Operation != foCalculateSize)
  784. {
  785. int OverallProgress;
  786. // FProgressForm is null when this is called from SetQueueProgress
  787. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  788. {
  789. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  790. }
  791. else
  792. {
  793. OverallProgress = ProgressData->OverallProgress();
  794. }
  795. // implies TBPF_NORMAL
  796. FTaskbarList->SetProgressValue(GetMainForm()->Handle, OverallProgress, 100);
  797. }
  798. else
  799. {
  800. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  801. }
  802. }
  803. //---------------------------------------------------------------------------
  804. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  805. {
  806. TTerminalManager::Instance()->QueueStatusUpdated();
  807. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  808. {
  809. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  810. {
  811. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  812. {
  813. TFileOperationProgressType * ProgressData = NULL;
  814. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  815. {
  816. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  817. }
  818. if ((ProgressData != NULL) &&
  819. ProgressData->InProgress)
  820. {
  821. SetTaskbarListProgressValue(ProgressData);
  822. }
  823. else
  824. {
  825. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  826. }
  827. }
  828. else
  829. {
  830. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  831. }
  832. }
  833. else
  834. {
  835. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  836. }
  837. }
  838. UpdateQueueLabel();
  839. }
  840. //---------------------------------------------------------------------------
  841. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  842. {
  843. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  844. if (FQueueStatus != NULL)
  845. {
  846. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  847. if (ActiveAndPendingPrimaryCount > 0)
  848. {
  849. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  850. }
  851. }
  852. QueueLabel->Caption = Caption;
  853. }
  854. //---------------------------------------------------------------------------
  855. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  856. {
  857. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  858. static_cast<TComponent*>(GetComponent(fcTransferList)));
  859. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  860. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  861. TransferList->Strings->BeginUpdate();
  862. try
  863. {
  864. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  865. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  866. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  867. if (FTransferDropDownHint.IsEmpty())
  868. {
  869. FTransferDropDownHint = TransferDropDown->Hint;
  870. }
  871. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  872. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  873. TransferDropDown->Text = StripHotkey(Name);
  874. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  875. (FTransferDropDownHint, StripHotkey(Name),
  876. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  877. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General))));
  878. // update the label, otherwise when it is updated only on the first draw
  879. // of the list, it is drawn "bold" for some reason
  880. FTransferListHoverIndex = TransferList->ItemIndex;
  881. UpdateTransferLabel();
  882. }
  883. __finally
  884. {
  885. TransferList->Strings->EndUpdate();
  886. }
  887. }
  888. //---------------------------------------------------------------------------
  889. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  890. {
  891. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  892. DebugAssert(Toolbar != NULL);
  893. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  894. }
  895. //---------------------------------------------------------------------------
  896. void __fastcall TCustomScpExplorerForm::UpdateActions()
  897. {
  898. TForm::UpdateActions();
  899. if (ComponentVisible[fcCustomCommandsBand])
  900. {
  901. UpdateCustomCommandsToolbar();
  902. }
  903. }
  904. //---------------------------------------------------------------------------
  905. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  906. {
  907. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  908. }
  909. //---------------------------------------------------------------------------
  910. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  911. {
  912. DebugAssert(Configuration && RemoteDirView);
  913. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  914. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  915. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  916. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  917. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  918. if (RemoteDirView->RowSelect != WinConfiguration->FullRowSelect)
  919. {
  920. RemoteDirView->RowSelect = WinConfiguration->FullRowSelect;
  921. // selection is not redrawn automatically when RowSelect changes
  922. RemoteDirView->Invalidate();
  923. }
  924. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  925. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  926. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  927. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  928. UpdateSessionsPageControlHeight();
  929. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  930. UpdateToolbarDisplayMode();
  931. UpdateTransferList();
  932. if (Terminal != NULL)
  933. {
  934. TransferPresetAutoSelect();
  935. }
  936. UpdateQueueView();
  937. UpdateControls();
  938. // this can be called even before constuctor finishes.
  939. if (FEditorManager != NULL)
  940. {
  941. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  942. }
  943. // this can be called even before constuctor finishes.
  944. if (FLocalEditors != NULL)
  945. {
  946. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  947. {
  948. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  949. }
  950. }
  951. if (ComponentVisible[fcCustomCommandsBand])
  952. {
  953. UpdateCustomCommandsToolbar();
  954. }
  955. // show only when keeping queue items forever,
  956. // otherwise, its enough to have in in the context menu
  957. QueueDeleteAllDoneQueueToolbarItem->Visible =
  958. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  959. }
  960. //---------------------------------------------------------------------------
  961. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  962. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  963. void * /*Arg*/)
  964. {
  965. if (Token != NULL)
  966. {
  967. TForm * Editor = dynamic_cast<TForm*>(Token);
  968. DebugAssert(Editor != NULL);
  969. ReconfigureEditorForm(Editor);
  970. }
  971. }
  972. //---------------------------------------------------------------------------
  973. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  974. {
  975. WinConfiguration->DDTransferConfirmation = asOn;
  976. }
  977. //---------------------------------------------------------------------------
  978. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  979. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  980. {
  981. // noop
  982. }
  983. //---------------------------------------------------------------------------
  984. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  985. TTransferDirection Direction, TTransferType Type, bool Temp,
  986. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  987. bool Confirm, bool DragDrop, int Options)
  988. {
  989. bool Result = true;
  990. DebugAssert(Terminal && Terminal->Active);
  991. // these parameters are known in advance
  992. int Params =
  993. FLAGMASK(Type == ttMove, cpDelete);
  994. bool ToTemp = (Temp && (Direction == tdToLocal));
  995. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  996. {
  997. bool DisableNewerOnly =
  998. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  999. ToTemp;
  1000. Options |=
  1001. FLAGMASK(ToTemp, coTemp) |
  1002. FLAGMASK(!Terminal->IsCapable[fsBackgroundTransfers], coDisableQueue) |
  1003. coDoNotShowAgain;
  1004. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1005. int CopyParamAttrs = (Direction == tdToRemote ?
  1006. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1007. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1008. int OutputOptions =
  1009. FLAGMASK(DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto),
  1010. cooDoNotShowAgain);
  1011. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1012. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1013. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1014. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions);
  1015. if (Result)
  1016. {
  1017. if (FLAGSET(OutputOptions, cooDoNotShowAgain))
  1018. {
  1019. if (DragDrop)
  1020. {
  1021. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1022. {
  1023. PopupTrayBalloon(NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation,
  1024. NULL, 0, EnableDDTransferConfirmation, NULL);
  1025. }
  1026. WinConfiguration->DDTransferConfirmation = asOff;
  1027. }
  1028. else
  1029. {
  1030. WinConfiguration->ConfirmTransferring = false;
  1031. }
  1032. }
  1033. else
  1034. {
  1035. // User explicitly unchecked "do not show again",
  1036. // so show him the dialog the next time
  1037. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1038. {
  1039. WinConfiguration->DDTransferConfirmation = asOn;
  1040. }
  1041. }
  1042. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1043. }
  1044. }
  1045. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fsBackgroundTransfers])
  1046. {
  1047. Configuration->Usage->Inc(L"TransfersOnBackground");
  1048. // these parameter are known only after transfer dialog
  1049. Params |=
  1050. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1051. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1052. Result = false;
  1053. ClearTransferSourceSelection(Direction);
  1054. }
  1055. return Result;
  1056. }
  1057. //---------------------------------------------------------------------------
  1058. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1059. {
  1060. if (FOnFileOperationFinished != NULL)
  1061. {
  1062. FOnFileOperationFinished(UnicodeString(), true);
  1063. }
  1064. else
  1065. {
  1066. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1067. if (HasDirView[Side])
  1068. {
  1069. DirView(Side)->SelectAll(smNone);
  1070. }
  1071. }
  1072. }
  1073. //---------------------------------------------------------------------------
  1074. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1075. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1076. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1077. int Params)
  1078. {
  1079. DebugAssert(Queue != NULL);
  1080. bool SingleFile = false;
  1081. if (FileList->Count == 1)
  1082. {
  1083. if (Direction == tdToRemote)
  1084. {
  1085. UnicodeString FileName = FileList->Strings[0];
  1086. SingleFile = FileExists(ApiPath(FileName));
  1087. }
  1088. else
  1089. {
  1090. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1091. SingleFile = !File->IsDirectory;
  1092. }
  1093. }
  1094. TQueueItem * QueueItem;
  1095. if (Direction == tdToRemote)
  1096. {
  1097. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1098. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1099. }
  1100. else
  1101. {
  1102. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1103. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1104. }
  1105. AddQueueItem(Queue, QueueItem, Terminal);
  1106. }
  1107. //---------------------------------------------------------------------------
  1108. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TTerminal * Terminal)
  1109. {
  1110. if (Queue->IsEmpty)
  1111. {
  1112. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  1113. ManagedTerminal->QueueOperationStart = Now();
  1114. }
  1115. Queue->AddItem(QueueItem);
  1116. }
  1117. //---------------------------------------------------------------------------
  1118. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1119. {
  1120. }
  1121. //---------------------------------------------------------------------------
  1122. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1123. {
  1124. DebugAssert(FDocks != NULL);
  1125. for (int Index = 0; Index < ComponentCount; Index++)
  1126. {
  1127. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1128. if ((Dock != NULL) && (Dock->Tag == 0))
  1129. {
  1130. FDocks->Add(Dock);
  1131. }
  1132. }
  1133. CollectItemsWithTextDisplayMode(this);
  1134. ConfigurationChanged();
  1135. QueuePanel->Height =
  1136. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1137. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1138. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1139. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1140. }
  1141. //---------------------------------------------------------------------------
  1142. void __fastcall TCustomScpExplorerForm::StoreParams()
  1143. {
  1144. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1145. WinConfiguration->QueueView.HeightPixelsPerInch = GetControlPixelsPerInch(this);
  1146. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1147. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1148. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1149. }
  1150. //---------------------------------------------------------------------------
  1151. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1152. {
  1153. if (!FFormRestored)
  1154. {
  1155. FFormRestored = true;
  1156. RestoreFormParams();
  1157. }
  1158. TForm::CreateParams(Params);
  1159. }
  1160. //---------------------------------------------------------------------------
  1161. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1162. {
  1163. DebugAssert(FDocks != NULL);
  1164. for (int Index = 0; Index < FDocks->Count; Index++)
  1165. {
  1166. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1167. }
  1168. }
  1169. //---------------------------------------------------------------------------
  1170. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1171. {
  1172. SetDockAllowDrag(true);
  1173. while (!ButtonsStr.IsEmpty())
  1174. {
  1175. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1176. UnicodeString Name = CutToChar(S, L'=', true);
  1177. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1178. {
  1179. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1180. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1181. {
  1182. while (!S.IsEmpty())
  1183. {
  1184. UnicodeString S2 = CutToChar(S, L',', true);
  1185. UnicodeString Name = CutToChar(S2, L':', true);
  1186. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1187. {
  1188. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1189. if (SameText(GetToolbarItemName(Item), Name))
  1190. {
  1191. // forward compatibility
  1192. UnicodeString S3 = CutToChar(S2, L':', true);
  1193. bool Visible = StrToIntDef(S3, 0);
  1194. Item->Visible = Visible;
  1195. }
  1196. }
  1197. }
  1198. }
  1199. }
  1200. }
  1201. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1202. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1203. }
  1204. //---------------------------------------------------------------------------
  1205. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1206. {
  1207. UnicodeString Result;
  1208. SetDockAllowDrag(true);
  1209. Result = ::GetToolbarsLayoutStr(this);
  1210. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1211. return Result;
  1212. }
  1213. //---------------------------------------------------------------------------
  1214. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1215. {
  1216. UnicodeString Result;
  1217. if (Item->Action != NULL)
  1218. {
  1219. Result = Item->Action->Name;
  1220. Result = RemoveSuffix(Result, L"Action");
  1221. }
  1222. else
  1223. {
  1224. Result = Item->Name;
  1225. Result = RemoveSuffix(Result, L"SubmenuItem");
  1226. Result = RemoveSuffix(Result, L"Item");
  1227. }
  1228. return Result;
  1229. }
  1230. //---------------------------------------------------------------------------
  1231. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1232. {
  1233. UnicodeString Result;
  1234. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1235. {
  1236. UnicodeString ToolbarStr;
  1237. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1238. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1239. {
  1240. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1241. {
  1242. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1243. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1244. // Once we introduce any buttons that aer hidden by default, we would have to remember their initial state
  1245. // and save the changes here only.
  1246. if (!Item->Visible)
  1247. {
  1248. UnicodeString Name = GetToolbarItemName(Item);
  1249. DebugAssert(Name.Pos(L"TBX") == 0);
  1250. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1251. }
  1252. }
  1253. }
  1254. if (!ToolbarStr.IsEmpty())
  1255. {
  1256. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1257. }
  1258. }
  1259. return Result;
  1260. }
  1261. //---------------------------------------------------------------------------
  1262. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1263. {
  1264. DebugAssert(FProgressForm == NULL);
  1265. FProgressForm =
  1266. new TProgressForm(Application, (FTransferResumeList != NULL), Terminal->IsCapable[fsSkipTransfer], SynchronizeProgress);
  1267. FProgressForm->DeleteLocalToRecycleBin =
  1268. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1269. FProgressForm->DeleteRemoteToRecycleBin =
  1270. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1271. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1272. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1273. LockWindow();
  1274. if (FTaskbarList != NULL)
  1275. {
  1276. // Actually, do not know what hides the progress once the operation finishes
  1277. SetTaskbarListProgressState(TBPF_NORMAL);
  1278. }
  1279. }
  1280. //---------------------------------------------------------------------------
  1281. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1282. {
  1283. UnlockWindow();
  1284. SAFE_DESTROY(FProgressForm);
  1285. SetQueueProgress();
  1286. }
  1287. //---------------------------------------------------------------------------
  1288. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1289. TFileOperationProgressType & ProgressData)
  1290. {
  1291. // Download to temporary local directory
  1292. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1293. {
  1294. bool Continue = true;
  1295. FPendingTempSpaceWarn = false;
  1296. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1297. if (!Continue)
  1298. {
  1299. Abort();
  1300. }
  1301. }
  1302. // operation is being executed and we still didn't show up progress form
  1303. if (ProgressData.InProgress && !FProgressForm)
  1304. {
  1305. CreateProgressForm(NULL);
  1306. // When main window is hidden, synchronisation form does not exist (we do not get here during synchronization - only comparing),
  1307. // we suppose "/upload" or URL download mode
  1308. if (!Visible && (ProgressData.Operation != foCalculateSize) &&
  1309. (ProgressData.Operation != foCalculateChecksum) &&
  1310. // Hack to prevent renames from DoSynchronizeMove (synchronize checklist) closing the connection
  1311. (ProgressData.Operation != foRemoteMove) &&
  1312. (FSynchronizeProgressForm == NULL) &&
  1313. !FStandaloneEditing)
  1314. {
  1315. FProgressForm->OnceDoneOperation = odoDisconnect;
  1316. }
  1317. }
  1318. // operation is finished (or terminated), so we hide progress form
  1319. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1320. {
  1321. DestroyProgressForm();
  1322. if (ProgressData.Operation == foCalculateSize)
  1323. {
  1324. // When calculating size before transfer, the abort caused by
  1325. // cancel flag set due to "MoveToQueue" below,
  1326. // is not propagated back to us in ExecuteFileOperation,
  1327. // as it is silently swallowed.
  1328. // So we explicitly (re)throw it here.
  1329. if (FMoveToQueue)
  1330. {
  1331. Abort();
  1332. }
  1333. }
  1334. else
  1335. {
  1336. if ((ProgressData.Cancel == csContinue) &&
  1337. !FAutoOperation)
  1338. {
  1339. OperationComplete(ProgressData.StartTime);
  1340. }
  1341. }
  1342. }
  1343. if (FProgressForm &&
  1344. // Can happen only during synchronization, when ending one of the operations
  1345. ProgressData.InProgress)
  1346. {
  1347. FProgressForm->SetProgressData(ProgressData);
  1348. if (FTaskbarList != NULL)
  1349. {
  1350. DebugAssert(ProgressData.InProgress);
  1351. SetTaskbarListProgressValue(&ProgressData);
  1352. }
  1353. if (FProgressForm->Cancel > csContinue)
  1354. {
  1355. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1356. if (FProgressForm->Cancel == csCancelFile)
  1357. {
  1358. FProgressForm->ClearCancel();
  1359. }
  1360. // cancel cancels even the move
  1361. FMoveToQueue = false;
  1362. }
  1363. else if (FProgressForm->MoveToQueue)
  1364. {
  1365. FMoveToQueue = true;
  1366. ProgressData.SetCancelAtLeast(csCancel);
  1367. }
  1368. if ((FTransferResumeList != NULL) &&
  1369. ProgressData.InProgress &&
  1370. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1371. !ProgressData.FullFileName.IsEmpty())
  1372. {
  1373. if ((FTransferResumeList->Count == 0) ||
  1374. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1375. {
  1376. // note that we do not recognize directories from files here
  1377. FTransferResumeList->Add(ProgressData.FullFileName);
  1378. }
  1379. }
  1380. }
  1381. }
  1382. //---------------------------------------------------------------------------
  1383. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1384. const TDateTime & StartTime)
  1385. {
  1386. if (GUIConfiguration->BeepOnFinish &&
  1387. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1388. {
  1389. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1390. DWORD Sound;
  1391. if (!ExtractFileExt(BeepSound).IsEmpty())
  1392. {
  1393. Sound = SND_FILENAME;
  1394. }
  1395. else
  1396. {
  1397. Sound = SND_ALIAS;
  1398. }
  1399. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1400. }
  1401. }
  1402. //---------------------------------------------------------------------------
  1403. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1404. TFileOperationProgressType & ProgressData)
  1405. {
  1406. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1407. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1408. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1409. FileOperationProgress(ProgressData);
  1410. }
  1411. //---------------------------------------------------------------------------
  1412. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1413. {
  1414. UnicodeString Result;
  1415. if (FProgressForm != NULL)
  1416. {
  1417. Result = FProgressForm->ProgressStr();
  1418. }
  1419. return Result;
  1420. }
  1421. //---------------------------------------------------------------------------
  1422. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1423. bool DragDrop)
  1424. {
  1425. return (!DragDrop && (DropSourceControl == NULL)) ||
  1426. (DropSourceControl == RemoteDirView);
  1427. }
  1428. //---------------------------------------------------------------------------
  1429. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1430. TFileOperation Operation, TOperationSide Side,
  1431. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1432. TOnceDoneOperation & OnceDoneOperation)
  1433. {
  1434. if (!FAutoOperation)
  1435. {
  1436. // no selection on "/upload", form servers only as event handler
  1437. // (it is not displayed)
  1438. if (PanelOperation(Side, FDragDropOperation) &&
  1439. Visible && (Operation != foCalculateSize) &&
  1440. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1441. {
  1442. if (FOnFileOperationFinished != NULL)
  1443. {
  1444. FOnFileOperationFinished(FileName, Success);
  1445. }
  1446. else
  1447. {
  1448. TCustomDirView * DView = DirView(Side);
  1449. UnicodeString FileNameOnly = ExtractFileName(FileName, (Side == osRemote));
  1450. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1451. // this can happen when local drive is unplugged in the middle of the operation
  1452. if (Item != NULL)
  1453. {
  1454. if (Success) Item->Selected = false;
  1455. DView->MakeProgressVisible(Item);
  1456. }
  1457. }
  1458. }
  1459. if ((Operation == foCopy) || (Operation == foMove))
  1460. {
  1461. if (Side == osLocal)
  1462. {
  1463. Configuration->Usage->Inc(L"UploadedFiles");
  1464. }
  1465. else
  1466. {
  1467. Configuration->Usage->Inc(L"DownloadedFiles");
  1468. }
  1469. }
  1470. }
  1471. if (Success && (FSynchronizeController != NULL))
  1472. {
  1473. if (Operation == foCopy)
  1474. {
  1475. DebugAssert(Side == osLocal);
  1476. FSynchronizeController->LogOperation(soUpload, FileName);
  1477. }
  1478. else if (Operation == foDelete)
  1479. {
  1480. DebugAssert(Side == osRemote);
  1481. FSynchronizeController->LogOperation(soDelete, FileName);
  1482. }
  1483. }
  1484. if (FProgressForm)
  1485. {
  1486. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1487. }
  1488. }
  1489. //---------------------------------------------------------------------------
  1490. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1491. TFileOperation Operation, TOperationSide Side,
  1492. bool Temp, const UnicodeString & FileName, Boolean Success,
  1493. TOnceDoneOperation & OnceDoneOperation)
  1494. {
  1495. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1496. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1497. OnceDoneOperation);
  1498. }
  1499. //---------------------------------------------------------------------------
  1500. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1501. {
  1502. DebugAssert(GetSide(Side) == osRemote);
  1503. DebugUsedParam(Side);
  1504. return RemoteDirView;
  1505. }
  1506. //---------------------------------------------------------------------------
  1507. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1508. {
  1509. DebugAssert(GetSide(Side) == osRemote);
  1510. DebugUsedParam(Side);
  1511. return (Terminal != NULL);
  1512. }
  1513. //---------------------------------------------------------------------------
  1514. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1515. TOperationSide Side, int FilesOnly)
  1516. {
  1517. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1518. }
  1519. //---------------------------------------------------------------------------
  1520. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1521. TOperationSide Side, int FilesOnly)
  1522. {
  1523. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1524. }
  1525. //---------------------------------------------------------------------------
  1526. struct THistoryItemData
  1527. {
  1528. short int Side;
  1529. short int Index;
  1530. };
  1531. //---------------------------------------------------------------------------
  1532. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1533. {
  1534. DirView(Side)->HistoryGo(Index);
  1535. }
  1536. //---------------------------------------------------------------------------
  1537. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1538. {
  1539. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1540. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1541. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1542. }
  1543. //---------------------------------------------------------------------------
  1544. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1545. bool Back)
  1546. {
  1547. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1548. {
  1549. TCustomDirView * DView = DirView(Side);
  1550. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1551. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1552. if (ICount > 10)
  1553. {
  1554. ICount = 10;
  1555. }
  1556. Menu->Items->Clear();
  1557. THistoryItemData Data;
  1558. Data.Side = Side;
  1559. for (short int i = 1; i <= ICount; i++)
  1560. {
  1561. TTBCustomItem * Item = new TTBXItem(Menu);
  1562. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1563. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, (Side == osRemote));
  1564. Item->Hint = DView->HistoryPath[Data.Index];
  1565. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1566. Item->Tag = *reinterpret_cast<int*>(&Data);
  1567. Item->OnClick = HistoryItemClick;
  1568. Menu->Items->Add(Item);
  1569. }
  1570. }
  1571. }
  1572. //---------------------------------------------------------------------------
  1573. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1574. TOperationSide Side, bool Back)
  1575. {
  1576. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1577. {
  1578. // workaround
  1579. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1580. // operator new allocates memory in Pascal code, but calls inline
  1581. // contructor in C++, leading in problems, the function does
  1582. // both in Pascal code
  1583. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1584. UpdateHistoryMenu(Side, Back);
  1585. }
  1586. return FHistoryMenu[Side == osLocal][Back];
  1587. }
  1588. //---------------------------------------------------------------------------
  1589. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1590. TCustomDirView *Sender)
  1591. {
  1592. TOperationSide Side = (Sender == DirView(osRemote) ? osRemote : osLocal);
  1593. UpdateHistoryMenu(Side, true);
  1594. UpdateHistoryMenu(Side, false);
  1595. }
  1596. //---------------------------------------------------------------------------
  1597. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1598. {
  1599. // remote custom commands can be executed only if the server supports shell commands
  1600. // or have secondary shell
  1601. return (FTerminal != NULL) && (FTerminal->IsCapable[fcSecondaryShell] || FTerminal->IsCapable[fcShellAnyCommand]);
  1602. }
  1603. //---------------------------------------------------------------------------
  1604. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1605. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1606. {
  1607. int Result;
  1608. TFileCustomCommand RemoteCustomCommand;
  1609. TLocalCustomCommand LocalCustomCommand;
  1610. TFileCustomCommand * NonInteractiveCustomCommand =
  1611. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1612. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1613. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1614. if (FLAGCLEAR(Command.Params, ccLocal))
  1615. {
  1616. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1617. // custom command that does not operate with files can be executed anytime ...
  1618. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1619. {
  1620. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1621. {
  1622. // ... but do not show such command in remote file menu (TODO)
  1623. Result = AllowedState;
  1624. }
  1625. else
  1626. {
  1627. Result = -1;
  1628. }
  1629. }
  1630. else
  1631. {
  1632. if ((ListType == ccltAll) || (ListType == ccltFile))
  1633. {
  1634. Result = ((FCurrentSide == osRemote) && DirView(osRemote)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1635. }
  1636. else
  1637. {
  1638. Result = -1;
  1639. }
  1640. }
  1641. }
  1642. else
  1643. {
  1644. // custom command that does not operate with files can be executed anytime
  1645. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1646. {
  1647. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1648. }
  1649. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1650. {
  1651. if ((ListType == ccltAll) || (ListType == ccltFile))
  1652. {
  1653. // special case is "diff"-style command that can be executed over any side,
  1654. // if we have both sides
  1655. Result =
  1656. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1657. // directly (instead of EnableSelectedOperation) to pass
  1658. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1659. ((HasDirView[osLocal] && DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1660. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1661. }
  1662. else if (ListType == ccltBoth)
  1663. {
  1664. Result = 1;
  1665. }
  1666. else
  1667. {
  1668. Result = -1;
  1669. }
  1670. }
  1671. else
  1672. {
  1673. if ((ListType == ccltAll) || (ListType == ccltFile))
  1674. {
  1675. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1676. }
  1677. else
  1678. {
  1679. Result = -1;
  1680. }
  1681. }
  1682. }
  1683. return Result;
  1684. }
  1685. //---------------------------------------------------------------------------
  1686. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1687. TStrings * FileList, const TCustomCommandType & ACommand,
  1688. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1689. {
  1690. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1691. {
  1692. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1693. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1694. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1695. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1696. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1697. bool Capture =
  1698. FLAGSET(ACommand.Params, ccShowResults) ||
  1699. FLAGSET(ACommand.Params, ccCopyResults) ||
  1700. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1701. TCaptureOutputEvent OutputEvent = NULL;
  1702. DebugAssert(FCapturedLog == NULL);
  1703. if (Capture)
  1704. {
  1705. FCapturedLog = new TStringList();
  1706. OutputEvent = TerminalCaptureLog;
  1707. }
  1708. try
  1709. {
  1710. if (!RemoteCustomCommand.IsFileCommand(Command))
  1711. {
  1712. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1713. OutputEvent);
  1714. }
  1715. else
  1716. {
  1717. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1718. }
  1719. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1720. {
  1721. if (FLAGSET(ACommand.Params, ccCopyResults))
  1722. {
  1723. CopyToClipboard(FCapturedLog);
  1724. }
  1725. if (FLAGSET(ACommand.Params, ccShowResults))
  1726. {
  1727. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1728. }
  1729. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1730. {
  1731. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1732. }
  1733. }
  1734. }
  1735. __finally
  1736. {
  1737. SAFE_DESTROY(FCapturedLog);
  1738. }
  1739. }
  1740. }
  1741. //---------------------------------------------------------------------------
  1742. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1743. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1744. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1745. {
  1746. TStrings * LocalFileList = NULL;
  1747. TStrings * RemoteFileList = NULL;
  1748. TStrings * RemoteFileListFull = NULL;
  1749. try
  1750. {
  1751. if (LocalFileCommand)
  1752. {
  1753. if (ALocalFileList == NULL)
  1754. {
  1755. DebugAssert(HasDirView[osLocal]);
  1756. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1757. // directly (instead of EnableSelectedOperation) to pass
  1758. // false to FocusedFileOnlyWhenFocused
  1759. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1760. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1761. }
  1762. else
  1763. {
  1764. LocalFileList = ALocalFileList;
  1765. }
  1766. if (FileListCommand)
  1767. {
  1768. if (LocalFileList->Count != 1)
  1769. {
  1770. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1771. }
  1772. }
  1773. else
  1774. {
  1775. if ((LocalFileList->Count != 1) &&
  1776. (FileList->Count != 1) &&
  1777. (LocalFileList->Count != FileList->Count))
  1778. {
  1779. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1780. }
  1781. }
  1782. }
  1783. UnicodeString RootTempDir;
  1784. UnicodeString TempDir;
  1785. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1786. if (!RemoteFiles)
  1787. {
  1788. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1789. }
  1790. try
  1791. {
  1792. TDateTimes RemoteFileTimes;
  1793. if (RemoteFiles)
  1794. {
  1795. RemoteFileList = FileList;
  1796. }
  1797. else
  1798. {
  1799. RemoteFileList = new TStringList();
  1800. TMakeLocalFileListParams MakeFileListParam;
  1801. MakeFileListParam.FileList = RemoteFileList;
  1802. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1803. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1804. MakeFileListParam.Recursive =
  1805. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1806. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1807. if (!MakeFileListParam.Recursive)
  1808. {
  1809. RemoteFileTimes.clear();
  1810. RemoteFileListFull = new TStringList();
  1811. MakeFileListParam.FileList = RemoteFileListFull;
  1812. MakeFileListParam.Recursive = true;
  1813. MakeFileListParam.IncludeDirs = false;
  1814. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1815. }
  1816. else
  1817. {
  1818. RemoteFileListFull = RemoteFileList;
  1819. }
  1820. }
  1821. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1822. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1823. if (!NonBlocking)
  1824. {
  1825. Progress.Start(foCustomCommand, osRemote, FileListCommand ? 1 : FileList->Count);
  1826. DebugAssert(FProgressForm != NULL);
  1827. FProgressForm->ReadOnly = true;
  1828. }
  1829. try
  1830. {
  1831. if (FileListCommand)
  1832. {
  1833. UnicodeString LocalFile;
  1834. // MakeFileList does not delimit filenames
  1835. UnicodeString FileList = MakeFileList(RemoteFileList);
  1836. if (LocalFileCommand)
  1837. {
  1838. DebugAssert(LocalFileList->Count == 1);
  1839. LocalFile = LocalFileList->Strings[0];
  1840. }
  1841. TLocalCustomCommand CustomCommand(Data,
  1842. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1843. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1844. if (NonBlocking)
  1845. {
  1846. DebugAssert(!POutput);
  1847. ExecuteShellChecked(ShellCommand);
  1848. }
  1849. else
  1850. {
  1851. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1852. }
  1853. }
  1854. else if (LocalFileCommand)
  1855. {
  1856. if (LocalFileList->Count == 1)
  1857. {
  1858. UnicodeString LocalFile = LocalFileList->Strings[0];
  1859. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1860. {
  1861. UnicodeString FileName = RemoteFileList->Strings[Index];
  1862. TLocalCustomCommand CustomCommand(Data,
  1863. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  1864. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1865. }
  1866. }
  1867. else if (RemoteFileList->Count == 1)
  1868. {
  1869. UnicodeString FileName = RemoteFileList->Strings[0];
  1870. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1871. {
  1872. TLocalCustomCommand CustomCommand(
  1873. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1874. FileName, LocalFileList->Strings[Index], L"");
  1875. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1876. }
  1877. }
  1878. else
  1879. {
  1880. if (LocalFileList->Count != RemoteFileList->Count)
  1881. {
  1882. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  1883. }
  1884. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1885. {
  1886. UnicodeString FileName = RemoteFileList->Strings[Index];
  1887. TLocalCustomCommand CustomCommand(
  1888. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1889. FileName, LocalFileList->Strings[Index], L"");
  1890. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1891. }
  1892. }
  1893. }
  1894. else
  1895. {
  1896. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1897. {
  1898. TLocalCustomCommand CustomCommand(Data,
  1899. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1900. RemoteFileList->Strings[Index], L"", L"");
  1901. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1902. }
  1903. }
  1904. }
  1905. __finally
  1906. {
  1907. if (!NonBlocking)
  1908. {
  1909. Progress.Stop();
  1910. }
  1911. }
  1912. DebugAssert(!FAutoOperation);
  1913. if (!RemoteFiles)
  1914. {
  1915. TempDir = IncludeTrailingBackslash(TempDir);
  1916. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  1917. {
  1918. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  1919. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  1920. // Skip directories, process only nested files.
  1921. // The check is redundant as FileAge fails for directories anyway.
  1922. !DirectoryExists(FileName))
  1923. {
  1924. UnicodeString RemoteDir =
  1925. UnixExtractFileDir(
  1926. UnixIncludeTrailingBackslash(FTerminal->CurrentDirectory) +
  1927. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  1928. TDateTime NewTime;
  1929. if (FileAge(FileName, NewTime) &&
  1930. (NewTime != RemoteFileTimes[Index]))
  1931. {
  1932. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  1933. TemporaryFileCopyParam(CopyParam);
  1934. CopyParam.FileMask = L"";
  1935. FAutoOperation = true;
  1936. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  1937. TemporaryFilesList->Add(FileName);
  1938. FTerminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  1939. }
  1940. }
  1941. }
  1942. }
  1943. }
  1944. __finally
  1945. {
  1946. FAutoOperation = false;
  1947. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  1948. {
  1949. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  1950. }
  1951. }
  1952. }
  1953. __finally
  1954. {
  1955. if (RemoteFileList != FileList)
  1956. {
  1957. delete RemoteFileList;
  1958. }
  1959. if (LocalFileList != ALocalFileList)
  1960. {
  1961. delete LocalFileList;
  1962. }
  1963. if (RemoteFileListFull != RemoteFileList)
  1964. {
  1965. delete RemoteFileListFull;
  1966. }
  1967. }
  1968. }
  1969. //---------------------------------------------------------------------------
  1970. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithRemoteFiles(
  1971. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  1972. bool FileListCommand, UnicodeString * POutput)
  1973. {
  1974. std::unique_ptr<TStrings> SelectedFileList(DirView(osLocal)->CreateFileList(false, true, NULL));
  1975. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  1976. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  1977. {
  1978. UnicodeString FileName = SelectedFileList->Strings[Index];
  1979. if (DirectoryExists(FileName))
  1980. {
  1981. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  1982. {
  1983. LocalFileList->Add(FileName);
  1984. }
  1985. if (FLAGSET(ACommand.Params, ccRecursive))
  1986. {
  1987. TMakeLocalFileListParams MakeFileListParam;
  1988. MakeFileListParam.FileList = LocalFileList.get();
  1989. MakeFileListParam.FileTimes = NULL;
  1990. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1991. MakeFileListParam.Recursive = true;
  1992. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  1993. }
  1994. }
  1995. else
  1996. {
  1997. LocalFileList->Add(FileName);
  1998. }
  1999. }
  2000. if (FileListCommand)
  2001. {
  2002. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2003. TLocalCustomCommand CustomCommand(
  2004. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2005. L"", L"", FileList);
  2006. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2007. }
  2008. else
  2009. {
  2010. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2011. Progress.Start(foCustomCommand, osRemote, FileListCommand ? 1 : LocalFileList->Count);
  2012. DebugAssert(FProgressForm != NULL);
  2013. FProgressForm->ReadOnly = true;
  2014. try
  2015. {
  2016. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2017. {
  2018. UnicodeString FileName = LocalFileList->Strings[Index];
  2019. TLocalCustomCommand CustomCommand(
  2020. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2021. FileName, L"", L"");
  2022. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2023. }
  2024. }
  2025. __finally
  2026. {
  2027. Progress.Stop();
  2028. }
  2029. }
  2030. }
  2031. //---------------------------------------------------------------------------
  2032. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2033. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2034. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2035. {
  2036. TLocalCustomCommand LocalCustomCommand(Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory());
  2037. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2038. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2039. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2040. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2041. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2042. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2043. std::unique_ptr<UnicodeString> POutput;
  2044. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2045. FLAGSET(ACommand.Params, ccCopyResults))
  2046. {
  2047. POutput.reset(new UnicodeString());
  2048. }
  2049. if (!LocalCustomCommand.IsFileCommand(Command))
  2050. {
  2051. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2052. }
  2053. // remote files?
  2054. else if ((FCurrentSide == osRemote) || LocalFileCommand)
  2055. {
  2056. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2057. }
  2058. // local files
  2059. else
  2060. {
  2061. LocalCustomCommandWithRemoteFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2062. }
  2063. if (POutput.get() != NULL)
  2064. {
  2065. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2066. int P = POutput->Pos(sLineBreak);
  2067. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2068. {
  2069. POutput->SetLength(P - 1);
  2070. }
  2071. // Copy even empty output to clipboard
  2072. if (FLAGSET(ACommand.Params, ccCopyResults))
  2073. {
  2074. CopyToClipboard(*POutput);
  2075. }
  2076. // But do not show an empty message box.
  2077. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2078. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2079. !POutput->IsEmpty())
  2080. {
  2081. TClipboardHandler ClipboardHandler;
  2082. ClipboardHandler.Text = *POutput;
  2083. TMessageParams Params;
  2084. TQueryButtonAlias Aliases[1];
  2085. Aliases[0].Button = qaRetry;
  2086. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2087. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2088. Params.Aliases = Aliases;
  2089. Params.AliasesCount = LENOF(Aliases);
  2090. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2091. }
  2092. }
  2093. }
  2094. //---------------------------------------------------------------------------
  2095. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2096. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2097. {
  2098. TCustomCommandData Data(Terminal);
  2099. UnicodeString Site = Terminal->SessionData->SessionKey;
  2100. UnicodeString HelpKeyword = ACommand.HomePage;
  2101. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2102. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2103. {
  2104. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2105. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2106. {
  2107. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, Terminal->CurrentDirectory));
  2108. }
  2109. else
  2110. {
  2111. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory()));
  2112. }
  2113. if (!DoCustomCommandOptionsDialog(
  2114. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2115. {
  2116. Abort();
  2117. }
  2118. }
  2119. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2120. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2121. {
  2122. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2123. }
  2124. else
  2125. {
  2126. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2127. }
  2128. }
  2129. //---------------------------------------------------------------------------
  2130. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2131. const TCustomCommandType & Command)
  2132. {
  2133. DebugAssert(FCustomCommandLocalFileList != NULL);
  2134. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2135. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2136. TStrings * LocalFileList = new TStringList();
  2137. TStrings * RemoteFileList = new TStringList();
  2138. try
  2139. {
  2140. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2141. {
  2142. LocalFileList->Clear();
  2143. LocalFileList->AddObject(
  2144. FCustomCommandLocalFileList->Strings[Index],
  2145. FCustomCommandLocalFileList->Objects[Index]);
  2146. RemoteFileList->Clear();
  2147. RemoteFileList->AddObject(
  2148. FCustomCommandRemoteFileList->Strings[Index],
  2149. FCustomCommandRemoteFileList->Objects[Index]);
  2150. CustomCommand(RemoteFileList, Command, LocalFileList);
  2151. }
  2152. }
  2153. __finally
  2154. {
  2155. delete LocalFileList;
  2156. delete RemoteFileList;
  2157. }
  2158. }
  2159. //---------------------------------------------------------------------------
  2160. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2161. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2162. {
  2163. delete FCustomCommandLocalFileList;
  2164. delete FCustomCommandRemoteFileList;
  2165. // takeover ownership,
  2166. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2167. FCustomCommandLocalFileList = LocalFileList;
  2168. FCustomCommandRemoteFileList = RemoteFileList;
  2169. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2170. if (Button != NULL)
  2171. {
  2172. FCustomCommandMenu->Items->Clear();
  2173. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth);
  2174. MenuPopup(FCustomCommandMenu, Button);
  2175. }
  2176. else
  2177. {
  2178. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2179. }
  2180. }
  2181. //---------------------------------------------------------------------------
  2182. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2183. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2184. {
  2185. DebugAssert(FCapturedLog != NULL);
  2186. if ((OutputType == cotOutput) || (OutputType == cotError))
  2187. {
  2188. FCapturedLog->Add(AddedLine);
  2189. }
  2190. }
  2191. //---------------------------------------------------------------------------
  2192. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2193. TOperationSide Side)
  2194. {
  2195. return (Side == osRemote) &&
  2196. ((Control == RemoteDirView) || (Control == RemoteDriveView));
  2197. }
  2198. //---------------------------------------------------------------------------
  2199. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2200. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2201. {
  2202. TTBItemOptions O;
  2203. O = Item->Options;
  2204. TCustomDirView * DView = DirView(Side);
  2205. if ((DView->ItemFocused != NULL) &&
  2206. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2207. // when resolving links is disabled, default action is to enter the directory,
  2208. // no matter what DoubleClickAction is configured to
  2209. ((Side != osRemote) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2210. // Can only Edit files, but can Open/Copy even directories
  2211. ((DoubleClickAction != dcaEdit) ||
  2212. !DView->ItemIsDirectory(DView->ItemFocused)))
  2213. {
  2214. Item->Options = O << tboDefault;
  2215. }
  2216. else
  2217. {
  2218. Item->Options = O >> tboDefault;
  2219. }
  2220. }
  2221. //---------------------------------------------------------------------------
  2222. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2223. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2224. {
  2225. TCustomDirView * DView = DirView(Side);
  2226. TListItem * Item = DView->ItemFocused;
  2227. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2228. EnableFocusedOperation[Side])
  2229. {
  2230. TPoint ClientPoint;
  2231. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2232. TPoint(0, 0) : MousePos;
  2233. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2234. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2235. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2236. }
  2237. }
  2238. //---------------------------------------------------------------------------
  2239. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2240. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2241. {
  2242. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2243. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2244. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2245. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2246. Handled = true;
  2247. }
  2248. //---------------------------------------------------------------------------
  2249. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2250. {
  2251. }
  2252. //---------------------------------------------------------------------------
  2253. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2254. {
  2255. DebugAssert(FErrorList == NULL);
  2256. if (WinConfiguration->ContinueOnError)
  2257. {
  2258. FErrorList = new TStringList();
  2259. Configuration->Usage->Inc(L"ContinuationsOnError");
  2260. }
  2261. NonVisualDataModule->StartBusy();
  2262. }
  2263. //---------------------------------------------------------------------------
  2264. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2265. {
  2266. NonVisualDataModule->EndBusy();
  2267. if (FErrorList)
  2268. {
  2269. HandleErrorList(FErrorList);
  2270. }
  2271. }
  2272. //---------------------------------------------------------------------------
  2273. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2274. const TCopyParamType & CopyParam)
  2275. {
  2276. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2277. {
  2278. Configuration->Usage->Inc(L"FileMaskUses");
  2279. }
  2280. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2281. {
  2282. Configuration->Usage->Inc(L"OperationMaskUses");
  2283. }
  2284. }
  2285. //---------------------------------------------------------------------------
  2286. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2287. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2288. {
  2289. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2290. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2291. TTransferOperationParam DefaultParam;
  2292. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2293. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2294. switch (Param.Queue)
  2295. {
  2296. case asOn:
  2297. CopyParam.Queue = true;
  2298. break;
  2299. case asOff:
  2300. CopyParam.Queue = false;
  2301. break;
  2302. case asAuto:
  2303. default:
  2304. // keep default
  2305. break;
  2306. }
  2307. bool Result =
  2308. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2309. if (Result)
  2310. {
  2311. DebugAssert(Terminal);
  2312. bool SelectionRestored = false;
  2313. TCustomDirView * DView = NULL;
  2314. if (HasDirView[Side])
  2315. {
  2316. DView = DirView(Side);
  2317. DView->SaveSelection();
  2318. DView->SaveSelectedNames();
  2319. }
  2320. UpdateCopyParamCounters(CopyParam);
  2321. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2322. DebugAssert(FTransferResumeList == NULL);
  2323. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2324. FMoveToQueue = false;
  2325. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2326. try
  2327. {
  2328. TStrings * PermanentFileList;
  2329. std::unique_ptr<TStrings> PermanentFileListOwner;
  2330. try
  2331. {
  2332. if (Side == osLocal)
  2333. {
  2334. PermanentFileList = FileList;
  2335. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2336. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2337. if (Operation == foMove)
  2338. {
  2339. ReloadLocalDirectory();
  2340. if (DView != NULL)
  2341. {
  2342. DView->RestoreSelection();
  2343. }
  2344. SelectionRestored = true;
  2345. }
  2346. }
  2347. else
  2348. {
  2349. // Clone the file list as it may refer to current directory files,
  2350. // which get destroyed, when the source directory is reloaded after foMove operation.
  2351. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2352. // But for a hotfix, we are not going to do such a big change.
  2353. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2354. PermanentFileList = PermanentFileListOwner.get();
  2355. try
  2356. {
  2357. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2358. }
  2359. __finally
  2360. {
  2361. if (Operation == foMove)
  2362. {
  2363. if (DView != NULL)
  2364. {
  2365. DView->RestoreSelection();
  2366. }
  2367. SelectionRestored = true;
  2368. }
  2369. ReloadLocalDirectory(Param.TargetDirectory);
  2370. }
  2371. }
  2372. }
  2373. catch (EAbort &)
  2374. {
  2375. if (FMoveToQueue)
  2376. {
  2377. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2378. DebugAssert(CopyParam.TransferSkipList == NULL);
  2379. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2380. if (TransferResumeList->Count > 0)
  2381. {
  2382. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2383. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2384. }
  2385. CopyParam.TransferSkipList = TransferResumeList.release();
  2386. // not really needed, just to keep it consistent with TransferResumeList
  2387. FTransferResumeList = NULL;
  2388. FMoveToQueue = false;
  2389. Configuration->Usage->Inc("MovesToBackground");
  2390. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2391. ClearTransferSourceSelection(Direction);
  2392. }
  2393. throw;
  2394. }
  2395. }
  2396. __finally
  2397. {
  2398. if (!SelectionRestored && (DView != NULL))
  2399. {
  2400. DView->DiscardSavedSelection();
  2401. }
  2402. FTransferResumeList = NULL;
  2403. }
  2404. }
  2405. return Result;
  2406. }
  2407. //---------------------------------------------------------------------------
  2408. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2409. TOperationSide Side, TStrings * FileList, void * Param)
  2410. {
  2411. DebugAssert(FileList->Count);
  2412. // We deliberately do not toggle alternative flag (Param), but use OR,
  2413. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2414. // shortcut of CurrentDeleteAlternativeAction
  2415. bool Alternative =
  2416. bool(Param) || UseAlternativeFunction();
  2417. bool Recycle;
  2418. if (Side == osLocal)
  2419. {
  2420. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2421. }
  2422. else
  2423. {
  2424. Recycle =
  2425. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2426. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2427. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2428. }
  2429. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2430. if (!Result)
  2431. {
  2432. UnicodeString Query;
  2433. if (FileList->Count == 1)
  2434. {
  2435. if (Side == osLocal)
  2436. {
  2437. Query = ExtractFileName(FileList->Strings[0]);
  2438. }
  2439. else
  2440. {
  2441. Query = UnixExtractFileName(FileList->Strings[0]);
  2442. }
  2443. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2444. }
  2445. else
  2446. {
  2447. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2448. }
  2449. TMessageParams Params(mpNeverAskAgainCheck);
  2450. Params.ImageName = L"Delete file";
  2451. unsigned int Answer =
  2452. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2453. if (Answer == qaNeverAskAgain)
  2454. {
  2455. Result = true;
  2456. if (Recycle)
  2457. {
  2458. WinConfiguration->ConfirmRecycling = false;
  2459. }
  2460. else
  2461. {
  2462. WinConfiguration->ConfirmDeleting = false;
  2463. }
  2464. }
  2465. else
  2466. {
  2467. Result = (Answer == qaOK);
  2468. }
  2469. }
  2470. if (Result)
  2471. {
  2472. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2473. }
  2474. return Result;
  2475. }
  2476. //---------------------------------------------------------------------------
  2477. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2478. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2479. {
  2480. TAutoBatch AutoBatch(this);
  2481. bool Result;
  2482. if ((Operation == foCopy) || (Operation == foMove))
  2483. {
  2484. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2485. }
  2486. else if (Operation == foRename)
  2487. {
  2488. DebugAssert(DirView(Side)->ItemFocused);
  2489. DirView(Side)->ItemFocused->EditCaption();
  2490. Result = true;
  2491. }
  2492. else if (Operation == foDelete)
  2493. {
  2494. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2495. }
  2496. else if (Operation == foSetProperties)
  2497. {
  2498. RemoteDirView->SaveSelectedNames();
  2499. Result = SetProperties(Side, FileList);
  2500. }
  2501. else if (Operation == foCustomCommand)
  2502. {
  2503. DebugAssert(Param);
  2504. DebugAssert(Side == osRemote);
  2505. RemoteDirView->SaveSelectedNames();
  2506. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2507. CustomCommand(FileList, *Command, NULL);
  2508. Result = true;
  2509. }
  2510. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2511. {
  2512. DebugAssert(Side == osRemote);
  2513. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2514. (Operation == foRemoteMove), reinterpret_cast<TTerminal *>(Param));
  2515. }
  2516. else if (Operation == foLock)
  2517. {
  2518. DebugAssert(Side == osRemote);
  2519. LockFiles(FileList, true);
  2520. Result = true;
  2521. }
  2522. else if (Operation == foUnlock)
  2523. {
  2524. DebugAssert(Side == osRemote);
  2525. LockFiles(FileList, false);
  2526. Result = true;
  2527. }
  2528. else
  2529. {
  2530. DebugFail();
  2531. }
  2532. return Result;
  2533. }
  2534. //---------------------------------------------------------------------------
  2535. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2536. {
  2537. if (Side == osCurrent)
  2538. {
  2539. Side = FCurrentSide;
  2540. }
  2541. return Side;
  2542. }
  2543. //---------------------------------------------------------------------------
  2544. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2545. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2546. {
  2547. Side = GetSide(Side);
  2548. bool Result;
  2549. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, (Side == osLocal), NULL);
  2550. try
  2551. {
  2552. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2553. }
  2554. __finally
  2555. {
  2556. delete FileList;
  2557. }
  2558. return Result;
  2559. }
  2560. //---------------------------------------------------------------------------
  2561. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2562. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2563. bool NoConfirmation, void * Param)
  2564. {
  2565. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2566. {
  2567. if ((Operation == foCopy) || (Operation == foMove))
  2568. {
  2569. if (GetSide(Side) == osLocal)
  2570. {
  2571. Configuration->Usage->Inc(L"UploadsCommand");
  2572. }
  2573. else
  2574. {
  2575. Configuration->Usage->Inc(L"DownloadsCommand");
  2576. }
  2577. }
  2578. }
  2579. }
  2580. //---------------------------------------------------------------------------
  2581. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2582. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2583. {
  2584. TTransferOperationParam Param;
  2585. if ((WinConfiguration->Interface != ifCommander) ||
  2586. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2587. {
  2588. Flags &= ~cocShortCutHint;
  2589. }
  2590. TCustomDirView * DView = DirView(Side);
  2591. Param.Options =
  2592. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2593. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2594. if (FLAGSET(Flags, cocQueue))
  2595. {
  2596. Param.Queue = asOn;
  2597. }
  2598. else if (FLAGSET(Flags, cocNonQueue))
  2599. {
  2600. Param.Queue = asOff;
  2601. }
  2602. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2603. }
  2604. //---------------------------------------------------------------------------
  2605. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2606. {
  2607. try
  2608. {
  2609. if (ErrorList->Count)
  2610. {
  2611. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2612. if (MessageDialog(Message, qtError,
  2613. qaOK | qaCancel, HELP_NONE) == qaOK)
  2614. {
  2615. unsigned int Answer;
  2616. int Index = 0;
  2617. do
  2618. {
  2619. DebugAssert(Index >= 0 && Index < ErrorList->Count);
  2620. TQueryButtonAlias Aliases[2];
  2621. Aliases[0].Button = qaYes;
  2622. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2623. Aliases[1].Button = qaNo;
  2624. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2625. TMessageParams Params;
  2626. Params.Aliases = Aliases;
  2627. Params.AliasesCount = LENOF(Aliases);
  2628. Answer = MoreMessageDialog(
  2629. FMTLOAD(ERROR_LIST_NUMBER, (Index+1, ErrorList->Count, ErrorList->Strings[Index])),
  2630. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2631. (Index ? qaYes : 0) | (Index < ErrorList->Count - 1 ? qaNo : 0) |
  2632. qaOK, HELP_NONE, &Params);
  2633. if (Answer == qaNo)
  2634. {
  2635. Index++;
  2636. }
  2637. if (Answer == qaYes)
  2638. {
  2639. Index--;
  2640. }
  2641. }
  2642. while (Answer != qaOK);
  2643. }
  2644. }
  2645. }
  2646. __finally
  2647. {
  2648. TStrings * List = ErrorList;
  2649. ErrorList = NULL;
  2650. for (int i = 0; i < List->Count; i++)
  2651. {
  2652. delete List->Objects[i];
  2653. }
  2654. delete List;
  2655. }
  2656. }
  2657. //---------------------------------------------------------------------------
  2658. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2659. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2660. {
  2661. // needed for checking filemasks, as there's no directory object
  2662. // associated with the file object
  2663. File->FullFileName = FullFileName;
  2664. TFileMasks::TParams MaskParams;
  2665. MaskParams.Size = File->Size;
  2666. MaskParams.Modification = File->Modification;
  2667. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2668. }
  2669. //---------------------------------------------------------------------------
  2670. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2671. {
  2672. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2673. Side = GetSide(Side);
  2674. TCustomDirView * DView = DirView(Side);
  2675. TListItem * FocusedItem = DView->ItemFocused;
  2676. UnicodeString Name;
  2677. if ((FocusedItem != NULL) && !DView->ItemIsParentDirectory(FocusedItem))
  2678. {
  2679. Name = DView->ItemFileName(FocusedItem);
  2680. }
  2681. else
  2682. {
  2683. Name = LoadStr(NEW_FILE);
  2684. }
  2685. UnicodeString Names = Name;
  2686. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2687. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2688. HELP_EDIT_NEW, History.get(), true))
  2689. {
  2690. while (!Names.IsEmpty())
  2691. {
  2692. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2693. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2694. UnicodeString TargetFileName;
  2695. UnicodeString LocalFileName;
  2696. UnicodeString RootTempDir;
  2697. UnicodeString TempDir;
  2698. UnicodeString RemoteDirectory;
  2699. bool ExistingFile = false;
  2700. if (Side == osRemote)
  2701. {
  2702. Name = AbsolutePath(FTerminal->CurrentDirectory, Name);
  2703. TRemoteFile * File = NULL;
  2704. if (FTerminal->FileExists(Name, &File))
  2705. {
  2706. try
  2707. {
  2708. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2709. ExistingFile = true;
  2710. }
  2711. __finally
  2712. {
  2713. delete File;
  2714. }
  2715. }
  2716. if (!ExistingFile)
  2717. {
  2718. RemoteDirectory = UnixExtractFilePath(Name);
  2719. TemporaryDirectoryForRemoteFiles(
  2720. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2721. TargetFileName = UnixExtractFileName(Name);
  2722. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2723. LocalFileName = TempDir +
  2724. // We probably do not want to trim the VMS version here
  2725. FTerminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2726. }
  2727. }
  2728. else
  2729. {
  2730. if (ExtractFilePath(Name).IsEmpty())
  2731. {
  2732. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2733. }
  2734. else
  2735. {
  2736. LocalFileName = ExpandFileName(Name);
  2737. }
  2738. TargetFileName = ExtractFileName(Name);
  2739. }
  2740. if (!ExistingFile)
  2741. {
  2742. if (!FileExists(ApiPath(LocalFileName)))
  2743. {
  2744. int File = FileCreate(ApiPath(LocalFileName));
  2745. if (File < 0)
  2746. {
  2747. if (!RootTempDir.IsEmpty())
  2748. {
  2749. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2750. }
  2751. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2752. }
  2753. else
  2754. {
  2755. FileClose(File);
  2756. }
  2757. }
  2758. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2759. const TEditorData * ExternalEditor = NULL;
  2760. TFileMasks::TParams MaskParams; // size not known
  2761. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2762. false, MaskParams);
  2763. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2764. ExternalEditor, RootTempDir, RemoteDirectory);
  2765. }
  2766. }
  2767. }
  2768. }
  2769. //---------------------------------------------------------------------------
  2770. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2771. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2772. {
  2773. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2774. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2775. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2776. return
  2777. ((ExecuteFileBy == efInternalEditor)) ||
  2778. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2779. }
  2780. //---------------------------------------------------------------------------
  2781. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2782. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2783. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2784. UnicodeString RemoteDirectory)
  2785. {
  2786. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2787. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2788. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2789. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2790. Side = GetSide(Side);
  2791. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2792. if (Side == osRemote)
  2793. {
  2794. Data->Terminal = Terminal;
  2795. Data->Queue = Queue;
  2796. Data->SessionData = CloneCurrentSessionData();
  2797. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2798. Data->RemoteDirectory = RemoteDirectory;
  2799. Data->SessionName = Terminal->SessionData->SessionName;
  2800. Data->LocalRootDirectory = LocalRootDirectory;
  2801. Data->OriginalFileName = OriginalFileName;
  2802. Data->Command = L""; // will be changed later for external editor
  2803. }
  2804. if (ExecuteFileBy == efInternalEditor)
  2805. {
  2806. if (Side == osRemote)
  2807. {
  2808. UnicodeString Caption = UnixIncludeTrailingBackslash(RemoteDirectory) + OriginalFileName +
  2809. L" - " + Terminal->SessionData->SessionName;
  2810. TForm * Editor;
  2811. try
  2812. {
  2813. TNotifyEvent OnFileChanged = NULL;
  2814. TNotifyEvent OnSaveAll = NULL;
  2815. TAnyModifiedEvent OnAnyModified = NULL;
  2816. // Edited files are uploaded in background queue, what we do not support with encrypted files
  2817. if (Terminal->IsCapable[fsBackgroundTransfers])
  2818. {
  2819. OnFileChanged = FEditorManager->FileChanged;
  2820. OnSaveAll = SaveAllInternalEditors;
  2821. OnAnyModified = AnyInternalEditorModified;
  2822. }
  2823. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  2824. FEditorManager->FileReload, FEditorManager->FileClosed,
  2825. OnSaveAll, OnAnyModified,
  2826. Caption, FStandaloneEditing, SessionColor, Terminal->SessionData->InternalEditorEncoding);
  2827. }
  2828. catch(...)
  2829. {
  2830. if (!LocalRootDirectory.IsEmpty())
  2831. {
  2832. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  2833. }
  2834. throw;
  2835. }
  2836. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  2837. }
  2838. else
  2839. {
  2840. DebugAssert(!FStandaloneEditing);
  2841. TForm * Editor =
  2842. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  2843. SaveAllInternalEditors, AnyInternalEditorModified,
  2844. L"", false, SessionColor, -1);
  2845. FLocalEditors->Add(Editor);
  2846. }
  2847. }
  2848. else
  2849. {
  2850. HANDLE Process;
  2851. if (ExecuteFileBy == efExternalEditor)
  2852. {
  2853. UnicodeString Program, Params, Dir;
  2854. Data->Command = ExternalEditor->ExternalEditor;
  2855. ReformatFileNameCommand(Data->Command);
  2856. SplitCommand(Data->Command, Program, Params, Dir);
  2857. Params = ExpandFileNameCommand(Params, FileName);
  2858. Program = ExpandEnvironmentVariables(Program);
  2859. if (!ExecuteShell(Program, Params, Process))
  2860. {
  2861. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  2862. }
  2863. }
  2864. else
  2865. {
  2866. DebugAssert(Side == osRemote);
  2867. if (!ExecuteShell(FileName, L"", Process))
  2868. {
  2869. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  2870. }
  2871. }
  2872. if ((Side == osLocal) ||
  2873. ((ExecuteFileBy == efShell) &&
  2874. !WinConfiguration->Editor.SDIShellEditor) ||
  2875. ((ExecuteFileBy == efExternalEditor) &&
  2876. !ExternalEditor->SDIExternalEditor))
  2877. {
  2878. // no need for handle
  2879. if (Process != NULL)
  2880. {
  2881. DebugCheck(CloseHandle(Process));
  2882. }
  2883. Process = INVALID_HANDLE_VALUE;
  2884. }
  2885. else
  2886. {
  2887. if (Process == NULL)
  2888. {
  2889. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  2890. }
  2891. }
  2892. if (Side == osRemote)
  2893. {
  2894. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  2895. }
  2896. }
  2897. }
  2898. //---------------------------------------------------------------------------
  2899. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  2900. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2901. void * /*Arg*/)
  2902. {
  2903. if (Token != NULL)
  2904. {
  2905. EditorFormFileSave(static_cast<TForm *>(Token));
  2906. }
  2907. }
  2908. //---------------------------------------------------------------------------
  2909. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  2910. {
  2911. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  2912. {
  2913. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  2914. }
  2915. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  2916. }
  2917. //---------------------------------------------------------------------------
  2918. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  2919. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2920. void * Arg)
  2921. {
  2922. if ((Token != NULL) &&
  2923. IsEditorFormModified(static_cast<TForm *>(Token)))
  2924. {
  2925. *static_cast<bool *>(Arg) = true;
  2926. }
  2927. }
  2928. //---------------------------------------------------------------------------
  2929. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  2930. TObject * /*Sender*/, bool & Modified)
  2931. {
  2932. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  2933. {
  2934. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  2935. {
  2936. Modified = true;
  2937. }
  2938. }
  2939. if (!Modified)
  2940. {
  2941. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  2942. }
  2943. }
  2944. //---------------------------------------------------------------------------
  2945. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  2946. {
  2947. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  2948. }
  2949. //---------------------------------------------------------------------------
  2950. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  2951. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  2952. UnicodeString & Result, UnicodeString & RootDirectory)
  2953. {
  2954. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  2955. {
  2956. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  2957. Result = RootDirectory;
  2958. }
  2959. else
  2960. {
  2961. RootDirectory = L"";
  2962. Result = WinConfiguration->ExpandedTemporaryDirectory();
  2963. Result = IncludeTrailingBackslash(Result);
  2964. }
  2965. if (WinConfiguration->TemporaryDirectoryAppendSession)
  2966. {
  2967. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  2968. }
  2969. if (WinConfiguration->TemporaryDirectoryAppendPath)
  2970. {
  2971. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  2972. {
  2973. RemoteDirectory.Delete(1, 1);
  2974. }
  2975. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  2976. }
  2977. if (!ForceDirectories(ApiPath(Result)))
  2978. {
  2979. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  2980. }
  2981. }
  2982. //---------------------------------------------------------------------------
  2983. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  2984. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  2985. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  2986. {
  2987. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2988. if (ForceText)
  2989. {
  2990. CopyParam.TransferMode = tmAscii;
  2991. }
  2992. TemporaryFileCopyParam(CopyParam);
  2993. if (AllFiles)
  2994. {
  2995. CopyParam.IncludeFileMask = TFileMasks();
  2996. }
  2997. if (TempDir.IsEmpty())
  2998. {
  2999. TemporaryDirectoryForRemoteFiles(FTerminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3000. }
  3001. DebugAssert(!FAutoOperation);
  3002. FAutoOperation = AutoOperation;
  3003. Terminal->ExceptionOnFail = true;
  3004. try
  3005. {
  3006. try
  3007. {
  3008. // turn off confirmations, as for MDI editors we may possibly download
  3009. // the same file over
  3010. Terminal->CopyToLocal(
  3011. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3012. if (GetTargetNames)
  3013. {
  3014. for (int i = 0; i < FileList->Count; i++)
  3015. {
  3016. FileList->Strings[i] =
  3017. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3018. }
  3019. }
  3020. }
  3021. catch(...)
  3022. {
  3023. if (!RootTempDir.IsEmpty())
  3024. {
  3025. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3026. }
  3027. throw;
  3028. }
  3029. }
  3030. __finally
  3031. {
  3032. FAutoOperation = false;
  3033. Terminal->ExceptionOnFail = false;
  3034. }
  3035. }
  3036. //---------------------------------------------------------------------------
  3037. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3038. {
  3039. // Do not waste time checking for default editor list the next time,
  3040. // or testing if default editor is not notepad.
  3041. bool TryNextTime = false;
  3042. UnicodeString UsageState;
  3043. if (!WinConfiguration->EditorList->IsDefaultList())
  3044. {
  3045. UsageState = "H";
  3046. }
  3047. else
  3048. {
  3049. UnicodeString Executable;
  3050. UnicodeString ExecutableDescription;
  3051. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3052. {
  3053. UnicodeString Message =
  3054. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3055. unsigned int Answer =
  3056. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3057. if (Answer != qaOK)
  3058. {
  3059. UsageState = "R";
  3060. }
  3061. else
  3062. {
  3063. UsageState = "A";
  3064. TEditorData EditorData;
  3065. EditorData.Editor = edExternal;
  3066. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3067. EditorData.DecideExternalEditorText();
  3068. TEditorList EditorList;
  3069. EditorList = *WinConfiguration->EditorList;
  3070. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3071. WinConfiguration->EditorList = &EditorList;
  3072. }
  3073. }
  3074. }
  3075. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3076. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3077. }
  3078. //---------------------------------------------------------------------------
  3079. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3080. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3081. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3082. {
  3083. if (ExecuteFileBy == efDefaultEditor)
  3084. {
  3085. if (!WinConfiguration->OfferedEditorAutoConfig)
  3086. {
  3087. EditorAutoConfig();
  3088. }
  3089. const TEditorPreferences * Editor =
  3090. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3091. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3092. {
  3093. ExecuteFileBy = efInternalEditor;
  3094. ExternalEditor = NULL;
  3095. }
  3096. else if (Editor->Data->Editor == edOpen)
  3097. {
  3098. ExecuteFileBy = efShell;
  3099. ExternalEditor = NULL;
  3100. }
  3101. else
  3102. {
  3103. ExecuteFileBy = efExternalEditor;
  3104. ExternalEditor = Editor->Data;
  3105. }
  3106. }
  3107. }
  3108. //---------------------------------------------------------------------------
  3109. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3110. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3111. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3112. {
  3113. UnicodeString OriginalFileName;
  3114. UnicodeString LocalRootDirectory;
  3115. UnicodeString RemoteDirectory;
  3116. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3117. (Side == osLocal), MaskParams);
  3118. UnicodeString Counter;
  3119. UnicodeString LocalFileName;
  3120. bool Handled = false;
  3121. if (Side == osRemote)
  3122. {
  3123. // We need to trim VMS version here, so that we use name without version
  3124. // when uploading back to create a new version of the file
  3125. OriginalFileName = FTerminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3126. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3127. TObject * Token = NULL;
  3128. UnicodeString LocalDirectory;
  3129. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3130. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3131. LocalDirectory))
  3132. {
  3133. if (Token != NULL)
  3134. {
  3135. TForm * Form = dynamic_cast<TForm *>(Token);
  3136. if (Form->WindowState == wsMinimized)
  3137. {
  3138. ShowWindow(Form->Handle, SW_RESTORE);
  3139. }
  3140. else
  3141. {
  3142. Form->SetFocus();
  3143. }
  3144. Handled = true;
  3145. }
  3146. else
  3147. {
  3148. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3149. }
  3150. }
  3151. if (!Handled)
  3152. {
  3153. TStringList * FileList1 = new TStringList();
  3154. try
  3155. {
  3156. FileList1->AddObject(FullFileName, Object);
  3157. TemporarilyDownloadFiles(FileList1,
  3158. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3159. LocalRootDirectory, LocalDirectory, true, true, true);
  3160. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3161. }
  3162. __finally
  3163. {
  3164. delete FileList1;
  3165. }
  3166. switch (ExecuteFileBy)
  3167. {
  3168. case efShell:
  3169. Counter = "RemoteFilesExecuted";
  3170. break;
  3171. case efInternalEditor:
  3172. Counter = "RemoteFilesOpenedInInternalEditor";
  3173. break;
  3174. case efExternalEditor:
  3175. Counter = "RemoteFilesOpenedInExternalEditor";
  3176. break;
  3177. default:
  3178. DebugFail();
  3179. }
  3180. }
  3181. }
  3182. else
  3183. {
  3184. LocalFileName = FullFileName;
  3185. OriginalFileName = ExtractFileName(FullFileName);
  3186. switch (ExecuteFileBy)
  3187. {
  3188. case efShell:
  3189. Counter = "LocalFilesExecuted";
  3190. break;
  3191. case efInternalEditor:
  3192. Counter = "LocalFilesOpenedInInternalEditor";
  3193. break;
  3194. case efExternalEditor:
  3195. Counter = "LocalFilesOpenedInExternalEditor";
  3196. break;
  3197. default:
  3198. DebugFail();
  3199. }
  3200. }
  3201. if (!Handled)
  3202. {
  3203. Configuration->Usage->Inc(Counter);
  3204. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3205. ExternalEditor, LocalRootDirectory, RemoteDirectory);
  3206. }
  3207. }
  3208. //---------------------------------------------------------------------------
  3209. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3210. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3211. bool AllSelected, bool OnFocused)
  3212. {
  3213. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3214. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3215. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3216. Side = GetSide(Side);
  3217. TCustomDirView * DView = DirView(Side);
  3218. TStrings * FileList = AllSelected ?
  3219. DView->CreateFileList(OnFocused, Side == osLocal) :
  3220. DView->CreateFocusedFileList(Side == osLocal);
  3221. try
  3222. {
  3223. DebugAssert(AllSelected || (FileList->Count == 1));
  3224. for (int i = 0; i < FileList->Count; i++)
  3225. {
  3226. UnicodeString ListFileName = FileList->Strings[i];
  3227. UnicodeString FileNameOnly = (Side == osRemote) ?
  3228. UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3229. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3230. if (!DView->ItemIsDirectory(Item))
  3231. {
  3232. UnicodeString FullFileName;
  3233. if (Side == osRemote)
  3234. {
  3235. FullFileName = RemoteDirView->Path + ListFileName;
  3236. }
  3237. else
  3238. {
  3239. FullFileName = ListFileName;
  3240. }
  3241. TFileMasks::TParams MaskParams;
  3242. MaskParams.Size = DView->ItemFileSize(Item);
  3243. TDateTimePrecision Precision;
  3244. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3245. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3246. FileList->Objects[i], MaskParams);
  3247. }
  3248. }
  3249. }
  3250. __finally
  3251. {
  3252. delete FileList;
  3253. }
  3254. }
  3255. //---------------------------------------------------------------------------
  3256. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3257. {
  3258. // do not forget to add additional options to TemporarilyDownloadFiles, and AS
  3259. CopyParam.FileNameCase = ncNoChange;
  3260. CopyParam.PreserveRights = false;
  3261. CopyParam.PreserveReadOnly = false;
  3262. CopyParam.ReplaceInvalidChars = true;
  3263. CopyParam.IncludeFileMask = TFileMasks();
  3264. CopyParam.NewerOnly = false;
  3265. CopyParam.FileMask = L"";
  3266. }
  3267. //---------------------------------------------------------------------------
  3268. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3269. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3270. {
  3271. TTerminalManager * Manager = TTerminalManager::Instance();
  3272. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3273. {
  3274. if (!NonVisualDataModule->Busy)
  3275. {
  3276. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3277. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3278. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3279. if (Data->Terminal == NULL)
  3280. {
  3281. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3282. if (SameSiteTerminal != NULL)
  3283. {
  3284. UnicodeString Message =
  3285. FMTLOAD(EDIT_SESSION_REATTACH,
  3286. (FileNameOnly, Data->SessionName, FileNameOnly));
  3287. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3288. {
  3289. Data->Terminal = Terminal;
  3290. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3291. Data->SessionName = Terminal->SessionData->SessionName;
  3292. // We might also overwrite session data
  3293. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3294. }
  3295. else
  3296. {
  3297. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3298. Abort();
  3299. }
  3300. }
  3301. }
  3302. // foreground session should reconnect itself
  3303. else if (Terminal != Data->Terminal)
  3304. {
  3305. UnicodeString Message =
  3306. MainInstructions(
  3307. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3308. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3309. {
  3310. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3311. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3312. }
  3313. else
  3314. {
  3315. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3316. Abort();
  3317. }
  3318. }
  3319. }
  3320. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3321. {
  3322. Configuration->Usage->Inc(L"EditInactiveSession");
  3323. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3324. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3325. (ExtractFileName(FileName), Data->SessionName)));
  3326. }
  3327. }
  3328. TStrings * FileList = new TStringList();
  3329. try
  3330. {
  3331. FileList->Add(FileName);
  3332. // consider using the same settings (preset) as when the file was downloaded
  3333. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3334. TemporaryFileCopyParam(CopyParam);
  3335. if (Data->ForceText)
  3336. {
  3337. CopyParam.TransferMode = tmAscii;
  3338. }
  3339. // so i do not need to worry if masking algorithm works in all cases
  3340. // ("" means "copy file name", no masking is actually done)
  3341. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3342. {
  3343. CopyParam.FileMask = L"";
  3344. }
  3345. else
  3346. {
  3347. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3348. }
  3349. DebugAssert(Data->Queue != NULL);
  3350. int Params = cpNoConfirmation | cpTemporary;
  3351. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3352. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3353. QueueItem->CompleteEvent = UploadCompleteEvent;
  3354. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3355. }
  3356. __finally
  3357. {
  3358. delete FileList;
  3359. }
  3360. }
  3361. //---------------------------------------------------------------------------
  3362. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3363. const UnicodeString FileName, const TEditedFileData * Data)
  3364. {
  3365. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3366. DebugAssert(!NonVisualDataModule->Busy);
  3367. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3368. {
  3369. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3370. (ExtractFileName(FileName), Data->SessionName)));
  3371. }
  3372. TTerminal * PrevTerminal = TTerminalManager::Instance()->ActiveTerminal;
  3373. TTerminalManager::Instance()->ActiveTerminal = Data->Terminal;
  3374. NonVisualDataModule->StartBusy();
  3375. try
  3376. {
  3377. std::unique_ptr<TRemoteFile> File;
  3378. UnicodeString RemoteFileName =
  3379. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3380. FTerminal->ExceptionOnFail = true;
  3381. try
  3382. {
  3383. TRemoteFile * AFile = NULL;
  3384. FTerminal->ReadFile(RemoteFileName, AFile);
  3385. File.reset(AFile);
  3386. if (!File->HaveFullFileName)
  3387. {
  3388. File->FullFileName = RemoteFileName;
  3389. }
  3390. }
  3391. __finally
  3392. {
  3393. FTerminal->ExceptionOnFail = false;
  3394. }
  3395. std::unique_ptr<TStrings> FileList(new TStringList());
  3396. FileList->AddObject(RemoteFileName, File.get());
  3397. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3398. UnicodeString TempDir = ExtractFilePath(FileName);
  3399. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3400. TempDir, true, true, true);
  3401. // sanity check, the target file name should be still the same
  3402. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3403. }
  3404. __finally
  3405. {
  3406. NonVisualDataModule->EndBusy();
  3407. // it actually may not exist anymore...
  3408. TTerminalManager::Instance()->ActiveTerminal = PrevTerminal;
  3409. }
  3410. }
  3411. //---------------------------------------------------------------------------
  3412. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3413. const TEditedFileData * Data, bool & KeepOpen)
  3414. {
  3415. // Command is set for external editors only (not for "shell" open).
  3416. if (!Data->Command.IsEmpty())
  3417. {
  3418. bool AnyFound = false;
  3419. bool AnyMDI = false;
  3420. bool AnyNonMDI = false;
  3421. bool AnyDetect = false;
  3422. TEditorList * EditorList = new TEditorList();
  3423. try
  3424. {
  3425. *EditorList = *WinConfiguration->EditorList;
  3426. for (int i = 0; i < EditorList->Count; i++)
  3427. {
  3428. const TEditorPreferences * Editor = EditorList->Editors[i];
  3429. if ((Editor->Data->Editor == edExternal) &&
  3430. (Editor->Data->ExternalEditor == Data->Command))
  3431. {
  3432. AnyFound = true;
  3433. if (Editor->Data->SDIExternalEditor)
  3434. {
  3435. AnyNonMDI = true;
  3436. if (Editor->Data->DetectMDIExternalEditor)
  3437. {
  3438. AnyDetect = true;
  3439. }
  3440. }
  3441. else
  3442. {
  3443. AnyMDI = true;
  3444. }
  3445. }
  3446. }
  3447. bool EnableMDI = false;
  3448. bool DisableDetect = false;
  3449. if (AnyMDI)
  3450. {
  3451. KeepOpen = true;
  3452. if (AnyNonMDI)
  3453. {
  3454. // there is at least one instance of the editor with MDI support enabled,
  3455. // and one with disabled, enable it for all instances
  3456. EnableMDI = true;
  3457. }
  3458. }
  3459. else if (AnyFound && !AnyDetect)
  3460. {
  3461. // at least once instance found but all have MDI autodetection disabled
  3462. // => close the file (default action)
  3463. }
  3464. else
  3465. {
  3466. // no instance of the editor has MDI support enabled
  3467. TMessageParams Params;
  3468. if (AnyFound)
  3469. {
  3470. // there is at least one instance of the editor
  3471. Params.Params |= mpNeverAskAgainCheck;
  3472. }
  3473. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3474. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3475. switch (Answer)
  3476. {
  3477. case qaNeverAskAgain:
  3478. DisableDetect = true;
  3479. break;
  3480. case qaNo:
  3481. EnableMDI = true;
  3482. KeepOpen = true;
  3483. break;
  3484. }
  3485. }
  3486. if (AnyFound && (EnableMDI || DisableDetect))
  3487. {
  3488. bool Changed = false;
  3489. for (int i = 0; i < EditorList->Count; i++)
  3490. {
  3491. const TEditorPreferences * Editor = EditorList->Editors[i];
  3492. if ((Editor->Data->Editor == edExternal) &&
  3493. (Editor->Data->ExternalEditor == Data->Command) &&
  3494. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3495. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3496. {
  3497. Changed = true;
  3498. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3499. if (EnableMDI)
  3500. {
  3501. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3502. }
  3503. if (DisableDetect)
  3504. {
  3505. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3506. }
  3507. EditorList->Change(i, UpdatedEditor);
  3508. }
  3509. }
  3510. if (Changed)
  3511. {
  3512. WinConfiguration->EditorList = EditorList;
  3513. }
  3514. }
  3515. }
  3516. __finally
  3517. {
  3518. delete EditorList;
  3519. }
  3520. }
  3521. else
  3522. {
  3523. // "open" case
  3524. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3525. qaOK, HELP_APP_EARLY_CLOSED);
  3526. }
  3527. }
  3528. //---------------------------------------------------------------------------
  3529. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3530. {
  3531. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3532. }
  3533. //---------------------------------------------------------------------------
  3534. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3535. {
  3536. SideEnter(osRemote);
  3537. }
  3538. //---------------------------------------------------------------------------
  3539. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3540. {
  3541. MakeNextInTabOrder(RemoteDirView, RemoteDriveView);
  3542. SideEnter(osRemote);
  3543. }
  3544. //---------------------------------------------------------------------------
  3545. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3546. {
  3547. FCurrentSide = Side;
  3548. if (Visible)
  3549. {
  3550. UpdateControls();
  3551. }
  3552. }
  3553. //---------------------------------------------------------------------------
  3554. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3555. TStrings * FileList, bool Alternative)
  3556. {
  3557. TCustomDirView * DView = DirView(Side);
  3558. DView->SaveSelection();
  3559. DView->SaveSelectedNames();
  3560. DebugAssert(!FAlternativeDelete);
  3561. FAlternativeDelete = Alternative;
  3562. try
  3563. {
  3564. if (Side == osRemote)
  3565. {
  3566. DebugAssert(Terminal != NULL);
  3567. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3568. }
  3569. else
  3570. {
  3571. try
  3572. {
  3573. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3574. }
  3575. __finally
  3576. {
  3577. ReloadLocalDirectory();
  3578. }
  3579. }
  3580. FAlternativeDelete = false;
  3581. }
  3582. catch(...)
  3583. {
  3584. FAlternativeDelete = false;
  3585. DView->DiscardSavedSelection();
  3586. throw;
  3587. }
  3588. DView->RestoreSelection();
  3589. }
  3590. //---------------------------------------------------------------------------
  3591. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3592. {
  3593. DebugAssert(Terminal);
  3594. RemoteDirView->SaveSelection();
  3595. RemoteDirView->SaveSelectedNames();
  3596. try
  3597. {
  3598. if (Lock)
  3599. {
  3600. Terminal->LockFiles(FileList);
  3601. }
  3602. else
  3603. {
  3604. Terminal->UnlockFiles(FileList);
  3605. }
  3606. }
  3607. catch(...)
  3608. {
  3609. RemoteDirView->DiscardSavedSelection();
  3610. throw;
  3611. }
  3612. RemoteDirView->RestoreSelection();
  3613. }
  3614. //---------------------------------------------------------------------------
  3615. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TTerminal *& Session,
  3616. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3617. bool NoConfirmation, bool Move)
  3618. {
  3619. DebugAssert(Terminal != NULL);
  3620. // update Terminal->StateData->RemoteDirectory
  3621. UpdateTerminal(Terminal);
  3622. if (Session == NULL)
  3623. {
  3624. Session = TTerminalManager::Instance()->ActiveTerminal;
  3625. }
  3626. if (Session == TTerminalManager::Instance()->ActiveTerminal)
  3627. {
  3628. if (RemoteDriveView->DropTarget != NULL)
  3629. {
  3630. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3631. }
  3632. else if (RemoteDirView->DropTarget != NULL)
  3633. {
  3634. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3635. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3636. }
  3637. else
  3638. {
  3639. Target = RemoteDirView->Path;
  3640. }
  3641. }
  3642. else
  3643. {
  3644. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(Session));
  3645. Target = ManagedTerminal->StateData->RemoteDirectory;
  3646. }
  3647. Target = UnixIncludeTrailingBackslash(Target);
  3648. if (FileList->Count == 1)
  3649. {
  3650. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3651. }
  3652. else
  3653. {
  3654. FileMask = AnyMask;
  3655. }
  3656. DirectCopy = FTerminal->IsCapable[fcRemoteCopy] || FTerminal->IsCapable[fcSecondaryShell];
  3657. bool Result = true;
  3658. if (!NoConfirmation)
  3659. {
  3660. bool Multi = (FileList->Count > 1);
  3661. if (Move)
  3662. {
  3663. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3664. }
  3665. else
  3666. {
  3667. TStrings * Sessions = TTerminalManager::Instance()->TerminalList;
  3668. TStrings * Directories = new TStringList;
  3669. try
  3670. {
  3671. for (int Index = 0; Index < Sessions->Count; Index++)
  3672. {
  3673. TManagedTerminal * Terminal =
  3674. dynamic_cast<TManagedTerminal *>(Sessions->Objects[Index]);
  3675. Directories->Add(Terminal->StateData->RemoteDirectory);
  3676. }
  3677. TDirectRemoteCopy AllowDirectCopy;
  3678. if (FTerminal->IsCapable[fcRemoteCopy] || FTerminal->CommandSessionOpened)
  3679. {
  3680. DebugAssert(DirectCopy);
  3681. AllowDirectCopy = drcAllow;
  3682. }
  3683. else if (FTerminal->IsCapable[fcSecondaryShell])
  3684. {
  3685. DebugAssert(DirectCopy);
  3686. AllowDirectCopy = drcConfirmCommandSession;
  3687. }
  3688. else
  3689. {
  3690. DebugAssert(!DirectCopy);
  3691. AllowDirectCopy = drcDisallow;
  3692. }
  3693. void * ASession = Session;
  3694. Result = DoRemoteCopyDialog(Sessions, Directories, AllowDirectCopy,
  3695. Multi, ASession, Target, FileMask, DirectCopy, TTerminalManager::Instance()->ActiveTerminal);
  3696. Session = static_cast<TTerminal *>(ASession);
  3697. }
  3698. __finally
  3699. {
  3700. delete Directories;
  3701. }
  3702. }
  3703. }
  3704. return Result;
  3705. }
  3706. //---------------------------------------------------------------------------
  3707. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3708. TStrings * FileList, bool NoConfirmation, bool Move, TTerminal * Session)
  3709. {
  3710. bool DirectCopy;
  3711. UnicodeString Target, FileMask;
  3712. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3713. if (Result)
  3714. {
  3715. if (!Move && !DirectCopy)
  3716. {
  3717. Configuration->Usage->Inc("RemoteCopyTemp");
  3718. UnicodeString RootTempDir;
  3719. UnicodeString TempDir;
  3720. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3721. TStrings * TemporaryFilesList = new TStringList();
  3722. try
  3723. {
  3724. TMakeLocalFileListParams MakeFileListParam;
  3725. MakeFileListParam.FileList = TemporaryFilesList;
  3726. MakeFileListParam.FileTimes = NULL;
  3727. MakeFileListParam.IncludeDirs = true;
  3728. MakeFileListParam.Recursive = false;
  3729. ProcessLocalDirectory(TempDir, FTerminal->MakeLocalFileList, &MakeFileListParam);
  3730. TTerminalManager::Instance()->ActiveTerminal = Session;
  3731. if (TemporaryFilesList->Count > 0)
  3732. {
  3733. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3734. CopyParam.FileMask = FileMask;
  3735. DebugAssert(!FAutoOperation);
  3736. FAutoOperation = true;
  3737. FTerminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3738. }
  3739. }
  3740. __finally
  3741. {
  3742. delete TemporaryFilesList;
  3743. FAutoOperation = false;
  3744. if (!RootTempDir.IsEmpty())
  3745. {
  3746. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3747. }
  3748. }
  3749. }
  3750. else
  3751. {
  3752. RemoteDirView->SaveSelection();
  3753. RemoteDirView->SaveSelectedNames();
  3754. try
  3755. {
  3756. if (Move)
  3757. {
  3758. Configuration->Usage->Inc("RemoteMove");
  3759. Terminal->MoveFiles(FileList, Target, FileMask);
  3760. }
  3761. else
  3762. {
  3763. Configuration->Usage->Inc("RemoteCopyDirect");
  3764. DebugAssert(DirectCopy);
  3765. DebugAssert(Session == FTerminal);
  3766. if (FTerminal->IsCapable[fcRemoteCopy] ||
  3767. FTerminal->CommandSessionOpened ||
  3768. CommandSessionFallback())
  3769. {
  3770. Terminal->CopyFiles(FileList, Target, FileMask);
  3771. }
  3772. }
  3773. }
  3774. catch(...)
  3775. {
  3776. RemoteDirView->DiscardSavedSelection();
  3777. throw;
  3778. }
  3779. RemoteDirView->RestoreSelection();
  3780. }
  3781. }
  3782. return Result;
  3783. }
  3784. //---------------------------------------------------------------------------
  3785. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3786. const UnicodeString & Path, TRemoteProperties & Properties)
  3787. {
  3788. Properties.Valid = Properties.Valid << vpEncrypt;
  3789. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  3790. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  3791. }
  3792. //---------------------------------------------------------------------------
  3793. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3794. {
  3795. Side = GetSide(Side);
  3796. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3797. TRemoteProperties * AProperties = (Side == osRemote ? &Properties : NULL);
  3798. UnicodeString Name = LoadStr(NEW_FOLDER);
  3799. int AllowedChanges =
  3800. FLAGMASK((Side == osRemote) && Terminal->IsCapable[fcModeChanging], cpMode);
  3801. bool SaveSettings = false;
  3802. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  3803. {
  3804. TWindowLock Lock(this);
  3805. if (Side == osRemote)
  3806. {
  3807. if (SaveSettings)
  3808. {
  3809. GUIConfiguration->NewDirectoryProperties = Properties;
  3810. }
  3811. CreateRemoteDirectory(Name, Properties);
  3812. }
  3813. else
  3814. {
  3815. DirView(Side)->CreateDirectory(Name);
  3816. }
  3817. }
  3818. }
  3819. //---------------------------------------------------------------------------
  3820. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  3821. {
  3822. TWindowLock Lock(this);
  3823. DirView(Side)->ExecuteHomeDirectory();
  3824. }
  3825. //---------------------------------------------------------------------------
  3826. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  3827. {
  3828. DoOpenDirectoryDialog(odBrowse, Side);
  3829. }
  3830. //---------------------------------------------------------------------------
  3831. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  3832. {
  3833. UnicodeString Path = Bookmark->GetSideDirectory(Side);
  3834. bool Result = !Path.IsEmpty();
  3835. if (Result)
  3836. {
  3837. // While we might get here when the session is closed (from location profiles),
  3838. // it's not a problem as the Path setter is noop then
  3839. DirView(Side)->Path = Path;
  3840. }
  3841. return Result;
  3842. }
  3843. //---------------------------------------------------------------------------
  3844. void __fastcall TCustomScpExplorerForm::CalculateSize(
  3845. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  3846. bool & Close)
  3847. {
  3848. // terminal can be already closed (e.g. dropped connection)
  3849. if (Terminal != NULL)
  3850. {
  3851. try
  3852. {
  3853. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  3854. }
  3855. catch(...)
  3856. {
  3857. if (!Terminal->Active)
  3858. {
  3859. Close = true;
  3860. }
  3861. throw;
  3862. }
  3863. }
  3864. }
  3865. //---------------------------------------------------------------------------
  3866. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  3867. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  3868. bool & Close)
  3869. {
  3870. // terminal can be already closed (e.g. dropped connection)
  3871. if (Terminal != NULL)
  3872. {
  3873. Configuration->Usage->Inc(L"ChecksumCalculated");
  3874. try
  3875. {
  3876. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  3877. }
  3878. catch(...)
  3879. {
  3880. if (!Terminal->Active)
  3881. {
  3882. Close = true;
  3883. }
  3884. throw;
  3885. }
  3886. }
  3887. }
  3888. //---------------------------------------------------------------------------
  3889. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  3890. {
  3891. bool Result;
  3892. if (Side == osRemote)
  3893. {
  3894. TRemoteTokenList * GroupList = NULL;
  3895. TRemoteTokenList * UserList = NULL;
  3896. try
  3897. {
  3898. TRemoteProperties CurrentProperties;
  3899. if (Terminal->LoadFilesProperties(FileList))
  3900. {
  3901. RemoteDirView->Invalidate();
  3902. }
  3903. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  3904. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  3905. if (CapableGroupChanging || CapableOwnerChanging)
  3906. {
  3907. if (CapableGroupChanging)
  3908. {
  3909. GroupList = Terminal->Groups->Duplicate();
  3910. }
  3911. if (CapableOwnerChanging)
  3912. {
  3913. UserList = Terminal->Users->Duplicate();
  3914. }
  3915. TRemoteDirectory * Files = Terminal->Files;
  3916. int Count = Files->Count;
  3917. if (Count > 100)
  3918. {
  3919. Count = 100;
  3920. }
  3921. for (int Index = 0; Index < Count; Index++)
  3922. {
  3923. TRemoteFile * File = Files->Files[Index];
  3924. if (CapableGroupChanging)
  3925. {
  3926. GroupList->AddUnique(File->Group);
  3927. }
  3928. if (CapableOwnerChanging)
  3929. {
  3930. UserList->AddUnique(File->Owner);
  3931. }
  3932. }
  3933. // if we haven't collected tokens for all files in current directory,
  3934. // make sure we collect them at least for all selected files.
  3935. // (note that so far the files in FileList has to be from current direcotry)
  3936. if (Count < Files->Count)
  3937. {
  3938. for (int Index = 0; Index < FileList->Count; Index++)
  3939. {
  3940. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  3941. if (CapableGroupChanging)
  3942. {
  3943. GroupList->AddUnique(File->Group);
  3944. }
  3945. if (CapableOwnerChanging)
  3946. {
  3947. UserList->AddUnique(File->Owner);
  3948. }
  3949. }
  3950. }
  3951. }
  3952. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  3953. int Flags = 0;
  3954. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  3955. if (CapableOwnerChanging) Flags |= cpOwner;
  3956. if (CapableGroupChanging) Flags |= cpGroup;
  3957. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  3958. if (Terminal->IsCapable[fcCalculatingChecksum])
  3959. {
  3960. CalculateChecksumEvent = CalculateChecksum;
  3961. }
  3962. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  3963. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  3964. TRemoteProperties NewProperties = CurrentProperties;
  3965. Result =
  3966. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  3967. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  3968. Terminal->IsCapable[fcGroupOwnerChangingByID],
  3969. CalculateSize, CalculateChecksumEvent);
  3970. if (Result)
  3971. {
  3972. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  3973. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  3974. }
  3975. }
  3976. __finally
  3977. {
  3978. delete GroupList;
  3979. delete UserList;
  3980. }
  3981. }
  3982. else
  3983. {
  3984. DirView(Side)->DisplayPropertiesMenu();
  3985. Result = true;
  3986. }
  3987. return Result;
  3988. }
  3989. //---------------------------------------------------------------------------
  3990. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  3991. {
  3992. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  3993. {
  3994. FIgnoreNextDialogChar = Key;
  3995. }
  3996. Key = 0;
  3997. }
  3998. //---------------------------------------------------------------------------
  3999. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4000. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4001. {
  4002. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4003. if (Command != NULL)
  4004. {
  4005. KeyProcessed(Key, Shift);
  4006. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4007. {
  4008. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4009. false, false, const_cast<TCustomCommandType *>(Command));
  4010. }
  4011. }
  4012. }
  4013. //---------------------------------------------------------------------------
  4014. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4015. {
  4016. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4017. {
  4018. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4019. }
  4020. if (!DirView(osCurrent)->IsEditing())
  4021. {
  4022. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4023. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4024. {
  4025. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4026. if (((Action->ShortCut == KeyShortCut) ||
  4027. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4028. AllowedAction(Action, aaShortCut))
  4029. {
  4030. // Has to be called before the action as the dialog char is already in queue.
  4031. // So when the action consumes message queue, we already need to have the
  4032. // FIgnoreNextDialogChar set
  4033. KeyProcessed(Key, Shift);
  4034. // Reset reference to previous component (when menu/toolbar was clicked).
  4035. // Needed to detect that action was invoked by keyboard shortcut
  4036. // in TNonVisualDataModule::ExplorerActionsExecute
  4037. Action->ActionComponent = NULL;
  4038. Action->Execute();
  4039. return;
  4040. }
  4041. }
  4042. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4043. {
  4044. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4045. {
  4046. KeyProcessed(Key, Shift);
  4047. TTerminalManager::Instance()->ActiveTerminalIndex = i;
  4048. return;
  4049. }
  4050. }
  4051. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4052. {
  4053. KeyProcessed(Key, Shift);
  4054. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4055. }
  4056. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4057. if (IsCustomShortCut(CustomShortCut))
  4058. {
  4059. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4060. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4061. if (WinConfiguration->SharedBookmarks != NULL)
  4062. {
  4063. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4064. if ((Bookmark != NULL) &&
  4065. OpenBookmark(FCurrentSide, Bookmark))
  4066. {
  4067. KeyProcessed(Key, Shift);
  4068. }
  4069. }
  4070. }
  4071. }
  4072. TForm::KeyDown(Key, Shift);
  4073. }
  4074. //---------------------------------------------------------------------------
  4075. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4076. {
  4077. DebugAssert(Terminal != NULL);
  4078. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4079. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  4080. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4081. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4082. bool SecurityEnabled = !SessionInfo.SecurityProtocolName.IsEmpty();
  4083. SessionStatusBar->Panels->Items[Offset + 0]->Enabled = SecurityEnabled;
  4084. // expanded from ?: to avoid memory leaks
  4085. if (SecurityEnabled)
  4086. {
  4087. SessionStatusBar->Panels->Items[Offset + 0]->Hint =
  4088. FMTLOAD(STATUS_SECURE, (SessionInfo.SecurityProtocolName));
  4089. }
  4090. else
  4091. {
  4092. SessionStatusBar->Panels->Items[Offset + 0]->Hint = LoadStr(STATUS_INSECURE);
  4093. }
  4094. if (FileSystemInfo.ProtocolName.IsEmpty())
  4095. {
  4096. SessionStatusBar->Panels->Items[Offset + 1]->Caption = SessionInfo.ProtocolName;
  4097. }
  4098. else
  4099. {
  4100. SessionStatusBar->Panels->Items[Offset + 1]->Caption = FileSystemInfo.ProtocolName;
  4101. }
  4102. SessionStatusBar->Panels->Items[Offset + 1]->Hint = LoadStr(STATUS_PROTOCOL_HINT);
  4103. SessionStatusBar->Panels->Items[Offset + 2]->Enabled =
  4104. (!SessionInfo.CSCompression.IsEmpty() || !SessionInfo.SCCompression.IsEmpty());
  4105. UnicodeString CSCompressionStr = DefaultStr(SessionInfo.CSCompression, LoadStr(NO_STR));
  4106. UnicodeString SCCompressionStr = DefaultStr(SessionInfo.SCCompression, LoadStr(NO_STR));
  4107. UnicodeString CompressionStr = CSCompressionStr;
  4108. if (CSCompressionStr != SCCompressionStr)
  4109. {
  4110. CompressionStr += UnicodeString(L"/") + SCCompressionStr;
  4111. }
  4112. SessionStatusBar->Panels->Items[Offset + 2]->Hint = FMTLOAD(STATUS_COMPRESSION_HINT, (CompressionStr));
  4113. SessionStatusBar->Panels->Items[Offset + 3]->Hint = LoadStr(STATUS_DURATION_HINT);
  4114. UpdateStatusBar();
  4115. }
  4116. //---------------------------------------------------------------------------
  4117. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4118. {
  4119. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4120. DebugAssert(SessionStatusBar != NULL);
  4121. if (!Terminal || !Terminal->Active || Terminal->Status < ssOpened)
  4122. {
  4123. // note: (Terminal->Status < sshReady) currently never happens here,
  4124. // so STATUS_CONNECTING is never used
  4125. SessionStatusBar->SimplePanel = true;
  4126. SessionStatusBar->SimpleText = LoadStr(
  4127. !Terminal || !Terminal->Active ? STATUS_NOT_CONNECTED : STATUS_CONNECTING);
  4128. }
  4129. else
  4130. {
  4131. DebugAssert(Terminal);
  4132. SessionStatusBar->SimplePanel = false;
  4133. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4134. if (!FNote.IsEmpty())
  4135. {
  4136. SessionStatusBar->Panels->Items[0]->Caption = FNote;
  4137. }
  4138. else
  4139. {
  4140. UpdateStatusPanelText(SessionStatusBar->Panels->Items[0]);
  4141. }
  4142. SessionStatusBar->Panels->Items[0]->Hint = FNoteHints;
  4143. SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1]->Caption =
  4144. FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4145. }
  4146. }
  4147. //---------------------------------------------------------------------------
  4148. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4149. {
  4150. Panel->Caption = L"";
  4151. }
  4152. //---------------------------------------------------------------------------
  4153. void __fastcall TCustomScpExplorerForm::Idle()
  4154. {
  4155. if (FShowing || FStandaloneEditing)
  4156. {
  4157. FEditorManager->Check();
  4158. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4159. // that needs to know if queue view is visible (and it may be closed after queue update)
  4160. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4161. }
  4162. if (FShowing)
  4163. {
  4164. if (!NonVisualDataModule->Busy &&
  4165. // Menu is opened or drag&drop is going on
  4166. (Mouse->Capture == NULL))
  4167. {
  4168. if (FRefreshRemoteDirectory)
  4169. {
  4170. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4171. {
  4172. Terminal->RefreshDirectory();
  4173. }
  4174. FRefreshRemoteDirectory = false;
  4175. }
  4176. if (FRefreshLocalDirectory)
  4177. {
  4178. ReloadLocalDirectory();
  4179. FRefreshLocalDirectory = false;
  4180. }
  4181. if (WinConfiguration->RefreshRemotePanel)
  4182. {
  4183. TManagedTerminal * ManagedTerminal =
  4184. dynamic_cast<TManagedTerminal *>(Terminal);
  4185. if ((ManagedTerminal != NULL) && (Terminal->Status == ssOpened) &&
  4186. (Now() - ManagedTerminal->DirectoryLoaded >
  4187. WinConfiguration->RefreshRemotePanelInterval))
  4188. {
  4189. RemoteDirView->ReloadDirectory();
  4190. }
  4191. }
  4192. }
  4193. }
  4194. if (FShowing || FStandaloneEditing)
  4195. {
  4196. if (FQueueStatusInvalidated)
  4197. {
  4198. UpdateQueueStatus(false);
  4199. }
  4200. RefreshQueueItems();
  4201. }
  4202. if (FShowing)
  4203. {
  4204. UpdateStatusBar();
  4205. }
  4206. FIgnoreNextDialogChar = 0;
  4207. }
  4208. //---------------------------------------------------------------------------
  4209. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4210. {
  4211. try
  4212. {
  4213. FUserActionTimer->Enabled = false;
  4214. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4215. {
  4216. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4217. {
  4218. FPendingQueueActionItem->ProcessUserAction();
  4219. }
  4220. }
  4221. }
  4222. __finally
  4223. {
  4224. FPendingQueueActionItem = NULL;
  4225. }
  4226. }
  4227. //---------------------------------------------------------------------------
  4228. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4229. {
  4230. if (WinConfiguration->MinimizeToTray)
  4231. {
  4232. UpdateTrayIcon();
  4233. FTrayIcon->Visible = true;
  4234. if (Visible)
  4235. {
  4236. ShowWindow(Handle, SW_HIDE);
  4237. }
  4238. }
  4239. }
  4240. //---------------------------------------------------------------------------
  4241. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4242. {
  4243. // WORKAROUND
  4244. // When restoring maximized window from minimization,
  4245. // rarely some controls do not align properly.
  4246. // Two instances seen (both for Commander):
  4247. // - When restoring, window is temporarily narrower (not maximized),
  4248. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4249. // Once maximized already, top dock shinks back, but the session PageControl,
  4250. // do not align up, leaving space between TopDock and PageControl.
  4251. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4252. for (int Index = 0; Index < ControlCount; Index++)
  4253. {
  4254. RealignControl(Controls[Index]);
  4255. }
  4256. if (FTrayIcon->Visible)
  4257. {
  4258. FTrayIcon->Visible = false;
  4259. if (Visible)
  4260. {
  4261. ShowWindow(Handle, SW_SHOW);
  4262. }
  4263. }
  4264. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4265. {
  4266. FNeedSession = false;
  4267. NonVisualDataModule->StartBusy();
  4268. try
  4269. {
  4270. NeedSession(false);
  4271. }
  4272. __finally
  4273. {
  4274. NonVisualDataModule->EndBusy();
  4275. }
  4276. }
  4277. }
  4278. //---------------------------------------------------------------------------
  4279. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4280. {
  4281. FTrayIcon->Hint = GetMainForm()->Caption;
  4282. }
  4283. //---------------------------------------------------------------------------
  4284. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4285. {
  4286. UpdateTrayIcon();
  4287. }
  4288. //---------------------------------------------------------------------------
  4289. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4290. {
  4291. // workaround
  4292. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4293. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4294. // But WM_ACTIVATEAPP message can be received even when
  4295. // the main window is minimized to the tray and internal editor window is focused
  4296. // (after another application was previously active)
  4297. if (::IsIconic(Handle))
  4298. {
  4299. if (!IsAppIconic())
  4300. {
  4301. SetAppIconic(true);
  4302. }
  4303. }
  4304. ::ApplicationRestore();
  4305. Application->BringToFront();
  4306. }
  4307. //---------------------------------------------------------------------------
  4308. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4309. {
  4310. RestoreApp();
  4311. }
  4312. //---------------------------------------------------------------------------
  4313. void __fastcall TCustomScpExplorerForm::NewSession(bool FromSite, const UnicodeString & SessionUrl)
  4314. {
  4315. if (OpenInNewWindow())
  4316. {
  4317. // todo: Pass FromSite
  4318. ExecuteNewInstance(SessionUrl);
  4319. }
  4320. else
  4321. {
  4322. TTerminalManager::Instance()->NewSession(FromSite, SessionUrl);
  4323. }
  4324. }
  4325. //---------------------------------------------------------------------------
  4326. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4327. {
  4328. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->SessionName;
  4329. // current working directories become defaults here, what is not right
  4330. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4331. StoredSessions->NewSession(SessionName, SessionData.get());
  4332. // modified only, explicit
  4333. StoredSessions->Save(false, true);
  4334. // encode session name because of slashes in hierarchical sessions
  4335. return EncodeUrlString(SessionName);
  4336. }
  4337. //---------------------------------------------------------------------------
  4338. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4339. {
  4340. if (OpenInNewWindow())
  4341. {
  4342. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4343. }
  4344. else
  4345. {
  4346. // current working directories become defaults here, what is not right
  4347. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4348. TTerminalManager * Manager = TTerminalManager::Instance();
  4349. TTerminal * Terminal = Manager->NewTerminal(SessionData.get());
  4350. Manager->ActiveTerminal = Terminal;
  4351. }
  4352. }
  4353. //---------------------------------------------------------------------------
  4354. void __fastcall TCustomScpExplorerForm::RenameSession()
  4355. {
  4356. UnicodeString Name = Terminal->SessionData->Name;
  4357. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME))
  4358. {
  4359. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4360. TSessionData::ValidateName(Name);
  4361. // This is inconsistent with how color (for example) is handled.
  4362. Terminal->SessionData->Name = Name;
  4363. UpdateControls();
  4364. // Add/Remove distinguishing paths from sessions of the same name.
  4365. DoTerminalListChanged();
  4366. }
  4367. }
  4368. //---------------------------------------------------------------------------
  4369. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4370. {
  4371. DebugAssert(Queue != NULL);
  4372. bool Result = Queue->IsEmpty;
  4373. if (!Result)
  4374. {
  4375. SetFocus();
  4376. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4377. }
  4378. return Result;
  4379. }
  4380. //---------------------------------------------------------------------------
  4381. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4382. {
  4383. return CanCloseQueue(FQueue);
  4384. }
  4385. //---------------------------------------------------------------------------
  4386. void __fastcall TCustomScpExplorerForm::CloseSession()
  4387. {
  4388. if (CanCloseQueue())
  4389. {
  4390. TTerminalManager::Instance()->FreeActiveTerminal();
  4391. }
  4392. }
  4393. //---------------------------------------------------------------------------
  4394. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4395. {
  4396. if (OpenInNewWindow())
  4397. {
  4398. // encode session name because of slashes in hierarchical sessions
  4399. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4400. }
  4401. else
  4402. {
  4403. TTerminalManager * Manager = TTerminalManager::Instance();
  4404. TTerminal * Terminal = Manager->NewTerminal(Data);
  4405. Manager->ActiveTerminal = Terminal;
  4406. }
  4407. }
  4408. //---------------------------------------------------------------------------
  4409. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4410. {
  4411. if (OpenInNewWindow())
  4412. {
  4413. ExecuteNewInstance(Name);
  4414. }
  4415. else
  4416. {
  4417. TTerminalManager * Manager = TTerminalManager::Instance();
  4418. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4419. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4420. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  4421. }
  4422. }
  4423. //---------------------------------------------------------------------------
  4424. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4425. bool &CanClose)
  4426. {
  4427. if (Terminal != NULL)
  4428. {
  4429. if (Terminal->Active && WinConfiguration->ConfirmClosingSession)
  4430. {
  4431. unsigned int Result;
  4432. TMessageParams Params(mpNeverAskAgainCheck);
  4433. UnicodeString Message;
  4434. int Answers = qaOK | qaCancel;
  4435. if (TTerminalManager::Instance()->Count > 1)
  4436. {
  4437. if (!WinConfiguration->AutoSaveWorkspace)
  4438. {
  4439. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE2);
  4440. Answers = qaYes | qaNo | qaCancel;
  4441. }
  4442. else
  4443. {
  4444. Message = MainInstructions(LoadStr(CLOSE_SESSIONS));
  4445. }
  4446. }
  4447. else
  4448. {
  4449. Message = MainInstructions(FMTLOAD(CLOSE_SESSION, (Terminal->SessionData->SessionName)));
  4450. }
  4451. if (WinConfiguration->AutoSaveWorkspace)
  4452. {
  4453. Message =
  4454. FORMAT("%s\n\n%s", (Message,
  4455. FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()))));
  4456. }
  4457. SetFocus();
  4458. Result = MessageDialog(Message, qtConfirmation,
  4459. Answers, HELP_NONE, &Params);
  4460. if (Result == qaNeverAskAgain)
  4461. {
  4462. WinConfiguration->ConfirmClosingSession = false;
  4463. }
  4464. if (Result == qaNo)
  4465. {
  4466. CanClose = SaveWorkspace(true);
  4467. // note that the workspace will be saved redundatly again from FormClose
  4468. }
  4469. else
  4470. {
  4471. CanClose =
  4472. (Result == qaOK) ||
  4473. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4474. (Result == qaNeverAskAgain);
  4475. }
  4476. }
  4477. if (CanClose)
  4478. {
  4479. CanClose = CanCloseQueue();
  4480. }
  4481. }
  4482. if (CanClose)
  4483. {
  4484. CanClose =
  4485. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4486. FEditorManager->CloseExternalFilesWithoutProcess();
  4487. if (CanClose)
  4488. {
  4489. while (CanClose && (FLocalEditors->Count > 0))
  4490. {
  4491. int PrevCount = FLocalEditors->Count;
  4492. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4493. CanClose = (FLocalEditors->Count < PrevCount);
  4494. }
  4495. if (CanClose)
  4496. {
  4497. CanClose = FEditorManager->Empty(true);
  4498. if (!CanClose)
  4499. {
  4500. SetFocus();
  4501. CanClose =
  4502. (MessageDialog(
  4503. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4504. }
  4505. }
  4506. }
  4507. }
  4508. }
  4509. //---------------------------------------------------------------------------
  4510. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4511. {
  4512. TForm * Form = dynamic_cast<TForm *>(Sender);
  4513. DebugAssert(Form != NULL);
  4514. Form->Close();
  4515. }
  4516. //---------------------------------------------------------------------------
  4517. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4518. {
  4519. TForm * Form = dynamic_cast<TForm *>(Sender);
  4520. delete Form;
  4521. }
  4522. //---------------------------------------------------------------------------
  4523. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4524. {
  4525. while (FLocalEditors->Count > 0)
  4526. {
  4527. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4528. }
  4529. }
  4530. //---------------------------------------------------------------------------
  4531. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4532. TObject *Sender)
  4533. {
  4534. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4535. try
  4536. {
  4537. SetProperties(osRemote, FileList);
  4538. }
  4539. __finally
  4540. {
  4541. delete FileList;
  4542. }
  4543. }
  4544. //---------------------------------------------------------------------------
  4545. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4546. {
  4547. if (value)
  4548. {
  4549. if (Component == fcCustomCommandsBand)
  4550. {
  4551. UpdateCustomCommandsToolbar();
  4552. }
  4553. }
  4554. }
  4555. //---------------------------------------------------------------------------
  4556. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4557. {
  4558. TControl * Control = GetComponent(Component);
  4559. DebugAssert(Control);
  4560. bool Changed = (Control->Visible != value);
  4561. if (Changed)
  4562. {
  4563. ComponentShowing(Component, value);
  4564. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4565. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4566. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4567. if (value)
  4568. {
  4569. int RemainingHeight = Control->Parent->ClientHeight;
  4570. int RemainingWidth = Control->Parent->ClientWidth;
  4571. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4572. {
  4573. TControl * ChildControl = Control->Parent->Controls[i];
  4574. if (ChildControl->Visible)
  4575. {
  4576. switch (ChildControl->Align)
  4577. {
  4578. case alTop:
  4579. case alBottom:
  4580. RemainingHeight -= ChildControl->Height;
  4581. break;
  4582. case alLeft:
  4583. case alRight:
  4584. RemainingWidth -= ChildControl->Width;
  4585. break;
  4586. }
  4587. }
  4588. }
  4589. int Reserve = ScaleByTextHeight(this, 32);
  4590. // queue in explorer, trees in commander
  4591. if (Control->Height > RemainingHeight - Reserve)
  4592. {
  4593. Control->Height = RemainingHeight / 2;
  4594. }
  4595. if (Control->Width > RemainingWidth - Reserve)
  4596. {
  4597. Control->Width = RemainingWidth / 2;
  4598. }
  4599. }
  4600. Control->Visible = value;
  4601. if (WasFocused && Visible)
  4602. {
  4603. DirView(osCurrent)->SetFocus();
  4604. }
  4605. FixControlsPlacement();
  4606. }
  4607. }
  4608. //---------------------------------------------------------------------------
  4609. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4610. {
  4611. TControl * Control = GetComponent(Component);
  4612. if (Control == NULL)
  4613. {
  4614. return false;
  4615. }
  4616. else
  4617. {
  4618. return Control->Visible;
  4619. }
  4620. }
  4621. //---------------------------------------------------------------------------
  4622. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4623. {
  4624. bool Result;
  4625. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4626. {
  4627. Result = !IsUWP();
  4628. }
  4629. else
  4630. {
  4631. Result = true;
  4632. }
  4633. return Result;
  4634. }
  4635. //---------------------------------------------------------------------------
  4636. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4637. {
  4638. if (RemoteDirView->ItemFocused != NULL)
  4639. {
  4640. RemoteDirView->ItemFocused->MakeVisible(false);
  4641. }
  4642. QueueSplitter->Visible = QueuePanel->Visible;
  4643. RemotePanelSplitter->Visible = RemoteDriveView->Visible;
  4644. }
  4645. //---------------------------------------------------------------------------
  4646. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4647. {
  4648. switch (Component) {
  4649. case fcStatusBar: return RemoteStatusBar;
  4650. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4651. case fcQueueView: return QueuePanel;
  4652. case fcQueueToolbar: return QueueDock;
  4653. case fcRemoteTree: return RemoteDriveView;
  4654. case fcSessionsTabs: return SessionsPageControl;
  4655. default: return NULL;
  4656. }
  4657. }
  4658. //---------------------------------------------------------------------------
  4659. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4660. TObject *Sender, TListColumn *Column, TPoint &Point)
  4661. {
  4662. DebugAssert(NonVisualDataModule && Column && Sender);
  4663. NonVisualDataModule->ListColumn = Column;
  4664. TPopupMenu * DirViewColumnMenu;
  4665. if (Sender == RemoteDirView)
  4666. {
  4667. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4668. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4669. (Column->Index == uvName);
  4670. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4671. (Column->Index == uvSize);
  4672. }
  4673. else
  4674. {
  4675. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4676. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4677. (Column->Index == dvName);
  4678. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4679. (Column->Index == dvSize);
  4680. }
  4681. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  4682. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  4683. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  4684. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4685. }
  4686. //---------------------------------------------------------------------------
  4687. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4688. TObject *Sender, TListItem *Item, bool &AllowExec)
  4689. {
  4690. DoDirViewExecFile(Sender, Item, AllowExec);
  4691. }
  4692. //---------------------------------------------------------------------------
  4693. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4694. TListItem * Item, bool & AllowExec)
  4695. {
  4696. DebugAssert(Sender && Item && Configuration);
  4697. DebugAssert(AllowExec);
  4698. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  4699. bool Remote = (ADirView == DirView(osRemote));
  4700. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks;
  4701. TOperationSide Side = (Remote ? osRemote : osLocal);
  4702. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  4703. // on files only (not directories)
  4704. // and only when symlinks are resolved (apply to remote panel only)
  4705. if (!ADirView->ItemIsDirectory(Item) &&
  4706. (ResolvedSymlinks || FForceExecution || Terminal->IsEncryptingFiles()))
  4707. {
  4708. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  4709. !FForceExecution &&
  4710. (ResolvedSymlinks || Terminal->IsEncryptingFiles()))
  4711. {
  4712. if (WinConfiguration->DoubleClickAction == dcaCopy)
  4713. {
  4714. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  4715. UnlockWindow();
  4716. try
  4717. {
  4718. ExecuteFileOperation(
  4719. foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  4720. }
  4721. __finally
  4722. {
  4723. LockWindow();
  4724. }
  4725. AllowExec = false;
  4726. }
  4727. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  4728. {
  4729. if (!Remote || !WinConfiguration->DisableOpenEdit)
  4730. {
  4731. ExecuteFile(Side, efDefaultEditor);
  4732. AllowExec = false;
  4733. }
  4734. }
  4735. else
  4736. {
  4737. DebugFail();
  4738. }
  4739. }
  4740. // if we have not done anything special, fall back to default behavior
  4741. if (AllowExec)
  4742. {
  4743. if (Remote && !WinConfiguration->DisableOpenEdit)
  4744. {
  4745. ExecuteFile(osRemote, efShell);
  4746. AllowExec = false;
  4747. }
  4748. }
  4749. }
  4750. }
  4751. //---------------------------------------------------------------------------
  4752. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  4753. {
  4754. return ((Side == osRemote) || (Side == osCurrent));
  4755. }
  4756. //---------------------------------------------------------------------------
  4757. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  4758. {
  4759. DebugFail();
  4760. }
  4761. //---------------------------------------------------------------------------
  4762. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  4763. {
  4764. DebugFail();
  4765. }
  4766. //---------------------------------------------------------------------------
  4767. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  4768. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, bool UseDefaults)
  4769. {
  4770. TSynchronizeParamType Params;
  4771. Params.LocalDirectory = LocalDirectory;
  4772. Params.RemoteDirectory = RemoteDirectory;
  4773. int UnusedParams =
  4774. (GUIConfiguration->SynchronizeParams &
  4775. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize));
  4776. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  4777. Params.Options = GUIConfiguration->SynchronizeOptions;
  4778. bool SaveSettings = false;
  4779. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  4780. &DoSynchronizeTooManyDirectories);
  4781. DebugAssert(FSynchronizeController == NULL);
  4782. FSynchronizeController = &Controller;
  4783. bool Result;
  4784. try
  4785. {
  4786. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4787. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  4788. int Options =
  4789. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  4790. DebugAssert(FOnFeedSynchronizeError == NULL);
  4791. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  4792. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  4793. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  4794. if (Result)
  4795. {
  4796. if (SaveSettings)
  4797. {
  4798. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  4799. GUIConfiguration->SynchronizeOptions = Params.Options;
  4800. }
  4801. else
  4802. {
  4803. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  4804. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  4805. FLAGSET(Params.Options, soSynchronize))
  4806. {
  4807. GUIConfiguration->SynchronizeOptions =
  4808. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  4809. soSynchronize;
  4810. }
  4811. }
  4812. LocalDirectory = Params.LocalDirectory;
  4813. RemoteDirectory = Params.RemoteDirectory;
  4814. }
  4815. }
  4816. __finally
  4817. {
  4818. FSynchronizeController = NULL;
  4819. DebugAssert(FOnFeedSynchronizeError == NULL);
  4820. FOnFeedSynchronizeError = NULL;
  4821. }
  4822. return Result;
  4823. }
  4824. //---------------------------------------------------------------------------
  4825. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  4826. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  4827. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  4828. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  4829. TSynchronizeOptions * Options, bool Full)
  4830. {
  4831. // Keep-up-to-date mode
  4832. if (Terminal->Status == ssOpened)
  4833. {
  4834. try
  4835. {
  4836. int PParams = Params.Params;
  4837. if (!Full)
  4838. {
  4839. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  4840. if (FLAGSET(Params.Options, soRecurse))
  4841. {
  4842. PParams |= TTerminal::spSubDirs;
  4843. }
  4844. }
  4845. else
  4846. {
  4847. // if keepuptodate is non-recursive,
  4848. // full sync before has to be non-recursive as well
  4849. if (FLAGCLEAR(Params.Options, soRecurse))
  4850. {
  4851. PParams |= TTerminal::spNoRecurse;
  4852. }
  4853. }
  4854. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  4855. PParams, Checklist, Options);
  4856. }
  4857. catch (Exception & E)
  4858. {
  4859. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  4860. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  4861. {
  4862. // noop, already logged in MoreMessageDialog
  4863. }
  4864. else
  4865. {
  4866. ShowExtendedExceptionEx(Terminal, &E);
  4867. throw;
  4868. }
  4869. }
  4870. }
  4871. }
  4872. //---------------------------------------------------------------------------
  4873. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  4874. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  4875. const UnicodeString ErrorStr)
  4876. {
  4877. if (!Directory.IsEmpty())
  4878. {
  4879. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  4880. }
  4881. else
  4882. {
  4883. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  4884. }
  4885. }
  4886. //---------------------------------------------------------------------------
  4887. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  4888. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  4889. {
  4890. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  4891. {
  4892. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  4893. }
  4894. else
  4895. {
  4896. TMessageParams Params(mpNeverAskAgainCheck);
  4897. unsigned int Result = MessageDialog(
  4898. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  4899. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  4900. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  4901. {
  4902. MaxDirectories *= 2;
  4903. if (Result == qaNeverAskAgain)
  4904. {
  4905. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  4906. }
  4907. }
  4908. else
  4909. {
  4910. Abort();
  4911. }
  4912. }
  4913. }
  4914. //---------------------------------------------------------------------------
  4915. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  4916. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  4917. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  4918. TSynchronizeOptions * Options)
  4919. {
  4920. // Keep-up-to-date mode
  4921. DebugAssert(!FAutoOperation);
  4922. void * BatchStorage;
  4923. BatchStart(BatchStorage);
  4924. FAutoOperation = true;
  4925. bool AnyOperation = false;
  4926. TDateTime StartTime = Now();
  4927. TSynchronizeChecklist * AChecklist = NULL;
  4928. try
  4929. {
  4930. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  4931. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  4932. {
  4933. FSynchronizeProgressForm->Start();
  4934. }
  4935. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  4936. static_cast<TTerminal::TSynchronizeMode>(Mode),
  4937. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  4938. Options);
  4939. SAFE_DESTROY(FSynchronizeProgressForm);
  4940. AnyOperation = (AChecklist->CheckedCount > 0);
  4941. if (AnyOperation)
  4942. {
  4943. TSynchronizeProgress SynchronizeProgress(AChecklist);
  4944. CreateProgressForm(&SynchronizeProgress);
  4945. try
  4946. {
  4947. Terminal->SynchronizeApply(
  4948. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  4949. SynchronizeProcessedItem, NULL, NULL, NULL);
  4950. }
  4951. __finally
  4952. {
  4953. DestroyProgressForm();
  4954. }
  4955. }
  4956. }
  4957. __finally
  4958. {
  4959. if (Checklist == NULL)
  4960. {
  4961. delete AChecklist;
  4962. }
  4963. else
  4964. {
  4965. *Checklist = AChecklist;
  4966. }
  4967. FAutoOperation = false;
  4968. SAFE_DESTROY(FSynchronizeProgressForm);
  4969. BatchEnd(BatchStorage);
  4970. ReloadLocalDirectory();
  4971. if (AnyOperation)
  4972. {
  4973. OperationComplete(StartTime);
  4974. }
  4975. }
  4976. }
  4977. //---------------------------------------------------------------------------
  4978. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  4979. {
  4980. // can be called from command line
  4981. return Visible &&
  4982. ((DirView(osRemote)->SelCount > 0) ||
  4983. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  4984. }
  4985. //---------------------------------------------------------------------------
  4986. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  4987. {
  4988. LogSynchronizeEvent(Terminal, Message);
  4989. }
  4990. //---------------------------------------------------------------------------
  4991. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  4992. int Params, TSynchronizeOptions & Options)
  4993. {
  4994. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  4995. {
  4996. Options.Filter = new TStringList();
  4997. Options.Filter->CaseSensitive = false;
  4998. Options.Filter->Duplicates = Types::dupAccept;
  4999. if (DirView(osRemote)->SelCount > 0)
  5000. {
  5001. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5002. }
  5003. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5004. {
  5005. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5006. }
  5007. Options.Filter->Sort();
  5008. }
  5009. }
  5010. //---------------------------------------------------------------------------
  5011. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5012. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5013. {
  5014. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5015. UnicodeString AdditionalParams =
  5016. FORMAT(L"%s \"%s\" \"%s\" %d %d %s", (
  5017. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5018. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options,
  5019. TProgramParams::FormatSwitch(DEFAULTS_SWITCH)));
  5020. TCopyParamType Defaults;
  5021. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5022. ConfigStorage->AccessMode = smRead;
  5023. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false, false))
  5024. {
  5025. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5026. }
  5027. ConfigStorage.reset(NULL);
  5028. std::unique_ptr<TStringList> Options(new TStringList());
  5029. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5030. GUIConfiguration->SaveCopyParam(OptionsStorage.get(), CopyParams, &Defaults);
  5031. if (Options->Count > 0)
  5032. {
  5033. AdditionalParams +=
  5034. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAW_CONFIG_SWITCH), StringsToParams(Options.get())));
  5035. }
  5036. ExecuteNewInstance(SessionName, AdditionalParams);
  5037. }
  5038. //---------------------------------------------------------------------------
  5039. struct TSynchronizeParams
  5040. {
  5041. UnicodeString LocalDirectory;
  5042. UnicodeString RemoteDirectory;
  5043. TSynchronizeMode Mode;
  5044. TSynchronizeChecklist * Checklist;
  5045. int Params;
  5046. TCopyParamType * CopyParam;
  5047. TDateTime CollectElapsed;
  5048. TDateTime * StartTime;
  5049. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5050. };
  5051. //---------------------------------------------------------------------------
  5052. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5053. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5054. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5055. {
  5056. DebugAssert(!FAutoOperation);
  5057. void * BatchStorage;
  5058. BatchStart(BatchStorage);
  5059. FAutoOperation = true;
  5060. TFileOperationStatistics Statistics;
  5061. TDateTime Start = Now();
  5062. try
  5063. {
  5064. Params.OnProcessedItem = OnProcessedItem;
  5065. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5066. CreateProgressForm(&SynchronizeProgress);
  5067. Terminal->SynchronizeApply(
  5068. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5069. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5070. &Statistics);
  5071. }
  5072. __finally
  5073. {
  5074. Params.OnProcessedItem = NULL;
  5075. FAutoOperation = false;
  5076. DestroyProgressForm();
  5077. BatchEnd(BatchStorage);
  5078. ReloadLocalDirectory();
  5079. }
  5080. if (WinConfiguration->SynchronizeSummary)
  5081. {
  5082. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5083. // The statistics should be 0 anyway in this case
  5084. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5085. {
  5086. Message += L"\n";
  5087. if (Statistics.FilesUploaded > 0)
  5088. {
  5089. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5090. }
  5091. if (Statistics.FilesDownloaded > 0)
  5092. {
  5093. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5094. }
  5095. if (Statistics.FilesDeletedLocal > 0)
  5096. {
  5097. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5098. }
  5099. if (Statistics.FilesDeletedRemote > 0)
  5100. {
  5101. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5102. }
  5103. }
  5104. TDateTime Elapsed = (Now() - Start);
  5105. Message +=
  5106. L"\n" +
  5107. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5108. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5109. TMessageParams Params(mpNeverAskAgainCheck);
  5110. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5111. if (Result == qaNeverAskAgain)
  5112. {
  5113. WinConfiguration->SynchronizeSummary = false;
  5114. }
  5115. }
  5116. }
  5117. //---------------------------------------------------------------------------
  5118. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5119. {
  5120. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5121. {
  5122. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5123. }
  5124. // Not set in keep-up-to-date mode - Synchronize() method
  5125. if (Token != NULL)
  5126. {
  5127. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5128. if (Params.OnProcessedItem != NULL)
  5129. {
  5130. Params.OnProcessedItem(NULL, ChecklistItem);
  5131. }
  5132. }
  5133. }
  5134. //---------------------------------------------------------------------------
  5135. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5136. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5137. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5138. {
  5139. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5140. *Params.StartTime = Now();
  5141. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5142. }
  5143. //---------------------------------------------------------------------------
  5144. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5145. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5146. {
  5147. // terminal can be already closed (e.g. dropped connection)
  5148. if (Terminal != NULL)
  5149. {
  5150. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5151. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5152. }
  5153. }
  5154. //---------------------------------------------------------------------------
  5155. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5156. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5157. {
  5158. TAutoBatch AutoBatch(this);
  5159. TAutoFlag AutoOperationFlag(FAutoOperation);
  5160. if (Side == osRemote)
  5161. {
  5162. std::unique_ptr<TStrings> FileList(new TStringList());
  5163. FileList->AddObject(FileName, RemoteFile);
  5164. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5165. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5166. RemoteDirView->SaveSelection();
  5167. RemoteDirView->SaveSelectedNames();
  5168. try
  5169. {
  5170. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5171. }
  5172. catch(...)
  5173. {
  5174. RemoteDirView->DiscardSavedSelection();
  5175. throw;
  5176. }
  5177. RemoteDirView->RestoreSelection();
  5178. }
  5179. else if (DebugAlwaysTrue(Side == osLocal))
  5180. {
  5181. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5182. {
  5183. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5184. }
  5185. UnicodeString Directory = ExtractFileDir(FileName);
  5186. ReloadLocalDirectory(Directory);
  5187. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5188. if (!SamePaths(Directory, NewDirectory))
  5189. {
  5190. ReloadLocalDirectory(NewDirectory);
  5191. }
  5192. }
  5193. }
  5194. //---------------------------------------------------------------------------
  5195. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5196. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5197. TSynchronizeMode & Mode, bool & SaveMode, bool UseDefaults)
  5198. {
  5199. int Result;
  5200. int Params = GUIConfiguration->SynchronizeParams;
  5201. bool SaveSettings = false;
  5202. int Options =
  5203. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5204. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5205. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5206. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5207. bool Continue =
  5208. UseDefaults ||
  5209. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5210. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs);
  5211. if (Continue)
  5212. {
  5213. Configuration->Usage->Inc(L"Synchronizations");
  5214. UpdateCopyParamCounters(CopyParam);
  5215. TSynchronizeOptions SynchronizeOptions;
  5216. GetSynchronizeOptions(Params, SynchronizeOptions);
  5217. if (SaveSettings)
  5218. {
  5219. GUIConfiguration->SynchronizeParams = Params;
  5220. }
  5221. else
  5222. {
  5223. SaveMode = false;
  5224. }
  5225. TDateTime StartTime = Now();
  5226. TSynchronizeChecklist * Checklist = NULL;
  5227. try
  5228. {
  5229. DebugAssert(!FAutoOperation);
  5230. FAutoOperation = true;
  5231. try
  5232. {
  5233. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  5234. FSynchronizeProgressForm->Start();
  5235. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5236. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5237. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5238. &SynchronizeOptions);
  5239. }
  5240. __finally
  5241. {
  5242. FAutoOperation = false;
  5243. SAFE_DESTROY(FSynchronizeProgressForm);
  5244. }
  5245. TSynchronizeParams SynchronizeParams;
  5246. SynchronizeParams.LocalDirectory = LocalDirectory;
  5247. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5248. SynchronizeParams.Mode = Mode;
  5249. SynchronizeParams.CopyParam = &CopyParam;
  5250. SynchronizeParams.Params = Params;
  5251. SynchronizeParams.Checklist = Checklist;
  5252. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5253. SynchronizeParams.StartTime = &StartTime;
  5254. SynchronizeParams.OnProcessedItem = NULL;
  5255. Result = Checklist->Count;
  5256. if (Checklist->Count > 0)
  5257. {
  5258. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5259. {
  5260. if (!DoSynchronizeChecklistDialog(
  5261. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5262. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, &SynchronizeParams))
  5263. {
  5264. Result = -1;
  5265. }
  5266. }
  5267. else
  5268. {
  5269. FullSynchronize(SynchronizeParams, NULL, NULL);
  5270. }
  5271. }
  5272. else
  5273. {
  5274. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5275. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5276. }
  5277. }
  5278. __finally
  5279. {
  5280. delete Checklist;
  5281. }
  5282. OperationComplete(StartTime);
  5283. }
  5284. else
  5285. {
  5286. Result = -1;
  5287. }
  5288. return Result;
  5289. }
  5290. //---------------------------------------------------------------------------
  5291. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5292. const UnicodeString LocalDirectory, const UnicodeString RemoteDirectory,
  5293. bool & Continue, bool Collect)
  5294. {
  5295. if (Collect)
  5296. {
  5297. DebugAssert(FSynchronizeProgressForm != NULL);
  5298. if (!FSynchronizeProgressForm->Started)
  5299. {
  5300. FSynchronizeProgressForm->Start();
  5301. }
  5302. FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Continue);
  5303. }
  5304. else
  5305. {
  5306. DebugAssert(FSynchronizeProgressForm == NULL);
  5307. DebugAssert(FProgressForm != NULL);
  5308. }
  5309. }
  5310. //---------------------------------------------------------------------------
  5311. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5312. {
  5313. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  5314. TRemoteFile * File = NULL;
  5315. Terminal->ReadFile(FullFileName, File);
  5316. if (File != NULL)
  5317. {
  5318. std::unique_ptr<TRemoteFile> FileOwner(File);
  5319. TAutoFlag Flag(FStandaloneEditing);
  5320. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5321. Application->ShowMainForm = false;
  5322. Application->Run();
  5323. }
  5324. }
  5325. //---------------------------------------------------------------------------
  5326. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  5327. {
  5328. DebugFail();
  5329. }
  5330. //---------------------------------------------------------------------------
  5331. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5332. {
  5333. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5334. SessionData->Assign(Terminal->SessionData);
  5335. UpdateSessionData(SessionData.get());
  5336. Terminal->UpdateSessionCredentials(SessionData.get());
  5337. if (!Terminal->SessionData->HasSessionName())
  5338. {
  5339. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5340. // once IsWorkspace is cleared
  5341. SessionData->Name = UnicodeString();
  5342. SessionData->IsWorkspace = false;
  5343. }
  5344. return SessionData.release();
  5345. }
  5346. //---------------------------------------------------------------------------
  5347. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5348. {
  5349. TSessionData * SessionData = CloneCurrentSessionData();
  5350. try
  5351. {
  5352. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5353. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5354. }
  5355. __finally
  5356. {
  5357. delete SessionData;
  5358. }
  5359. }
  5360. //---------------------------------------------------------------------------
  5361. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5362. {
  5363. TTerminalManager * Manager = TTerminalManager::Instance();
  5364. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5365. if (DebugAlwaysTrue(Terminal != NULL))
  5366. {
  5367. // Update (Managed)Terminal->StateData
  5368. UpdateTerminal(Terminal);
  5369. }
  5370. Manager->SaveWorkspace(DataList.get());
  5371. return DataList.release();
  5372. }
  5373. //---------------------------------------------------------------------------
  5374. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5375. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5376. {
  5377. WinConfiguration->LastWorkspace = Name;
  5378. if (!SavePasswords)
  5379. {
  5380. for (int Index = 0; Index < DataList->Count; Index++)
  5381. {
  5382. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5383. if (SessionData->Link.IsEmpty())
  5384. {
  5385. SessionData->ClearSessionPasswords();
  5386. }
  5387. }
  5388. }
  5389. StoredSessions->NewWorkspace(Name, DataList);
  5390. // modified only
  5391. StoredSessions->Save(false, Explicit);
  5392. }
  5393. //---------------------------------------------------------------------------
  5394. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5395. {
  5396. return
  5397. DefaultStr(WinConfiguration->LastWorkspace,
  5398. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5399. }
  5400. //---------------------------------------------------------------------------
  5401. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5402. {
  5403. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5404. bool AnyNonStoredSessionWithPassword = false;
  5405. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5406. bool AllNonStoredSessionsAnonymous = true;
  5407. TTerminalManager * Manager = TTerminalManager::Instance();
  5408. for (int Index = 0; Index < DataList->Count; Index++)
  5409. {
  5410. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5411. if (SessionData->Link.IsEmpty())
  5412. {
  5413. if (SessionData->HasAnySessionPassword())
  5414. {
  5415. AnyNonStoredSessionWithPassword = true;
  5416. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  5417. {
  5418. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5419. }
  5420. }
  5421. if (!SameText(SessionData->UserName, AnonymousUserName))
  5422. {
  5423. AllNonStoredSessionsAnonymous = false;
  5424. }
  5425. }
  5426. }
  5427. bool SavePasswords;
  5428. bool * PSavePasswords;
  5429. bool NotRecommendedSavingPasswords =
  5430. !CustomWinConfiguration->UseMasterPassword &&
  5431. !AllNonStoredSessionsAnonymous;
  5432. if (Configuration->DisablePasswordStoring ||
  5433. !AnyNonStoredSessionWithPassword)
  5434. {
  5435. PSavePasswords = NULL;
  5436. SavePasswords = false;
  5437. }
  5438. else
  5439. {
  5440. PSavePasswords = &SavePasswords;
  5441. SavePasswords =
  5442. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5443. !NotRecommendedSavingPasswords;
  5444. }
  5445. UnicodeString Name = WorkspaceName();
  5446. bool CreateShortcut = false;
  5447. bool Result =
  5448. DoSaveWorkspaceDialog(
  5449. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5450. EnableAutoSave);
  5451. if (Result)
  5452. {
  5453. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5454. if (CreateShortcut)
  5455. {
  5456. TOperationVisualizer Visualizer;
  5457. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5458. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5459. }
  5460. if (EnableAutoSave)
  5461. {
  5462. WinConfiguration->AutoSaveWorkspace = true;
  5463. WinConfiguration->AutoWorkspace = Name;
  5464. if (PSavePasswords != NULL)
  5465. {
  5466. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5467. }
  5468. }
  5469. }
  5470. return Result;
  5471. }
  5472. //---------------------------------------------------------------------------
  5473. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TTerminal * Terminal)
  5474. {
  5475. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  5476. DebugAssert(ManagedTerminal != NULL);
  5477. SAFE_DESTROY(ManagedTerminal->RemoteExplorerState);
  5478. if (WinConfiguration->PreservePanelState)
  5479. {
  5480. ManagedTerminal->RemoteExplorerState = RemoteDirView->SaveState();
  5481. }
  5482. UpdateSessionData(ManagedTerminal->StateData);
  5483. }
  5484. //---------------------------------------------------------------------------
  5485. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5486. {
  5487. // Keep in sync with TSessionData::CopyStateData
  5488. DebugAssert(Data != NULL);
  5489. // This is inconsistent with how color (for example) is handled.
  5490. Data->Name = Terminal->SessionData->Name;
  5491. // cannot use RemoteDirView->Path, because it is empty if connection
  5492. // was already closed
  5493. // also only peek, we may not be connected at all atm
  5494. // so make sure we do not try retrieving current directory from the server
  5495. // (particularly with FTP)
  5496. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5497. if (!ACurrentDirectory.IsEmpty())
  5498. {
  5499. Data->RemoteDirectory = ACurrentDirectory;
  5500. }
  5501. Data->Color = SessionColor;
  5502. }
  5503. //---------------------------------------------------------------------------
  5504. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5505. {
  5506. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5507. DebugAssert(Toolbar != NULL);
  5508. for (int i = 0; i < Toolbar->Items->Count; i++)
  5509. {
  5510. TTBXCustomDropDownItem * DropDownItem;
  5511. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5512. if (DropDownItem != NULL)
  5513. {
  5514. ToolbarItemResize(DropDownItem,
  5515. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5516. Toolbar->NonClientWidth);
  5517. break;
  5518. }
  5519. }
  5520. }
  5521. //---------------------------------------------------------------------------
  5522. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5523. {
  5524. Item->EditWidth = Width;
  5525. }
  5526. //---------------------------------------------------------------------------
  5527. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5528. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5529. {
  5530. for (int i = 0; i < Toolbar->Items->Count; i++)
  5531. {
  5532. TTBXCustomDropDownItem * DropDownItem;
  5533. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5534. if (DropDownItem != NULL)
  5535. {
  5536. ASize.x -= DropDownItem->EditWidth;
  5537. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5538. }
  5539. }
  5540. }
  5541. //---------------------------------------------------------------------------
  5542. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5543. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5544. {
  5545. if (WinConfiguration->DDWarnLackOfTempSpace)
  5546. {
  5547. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5548. if (!ADrive.IsEmpty())
  5549. {
  5550. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5551. DebugAssert(RequiredSpace >= 0);
  5552. __int64 RequiredWithReserve;
  5553. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5554. if (FreeSpace < RequiredWithReserve)
  5555. {
  5556. unsigned int Result;
  5557. TMessageParams Params(mpNeverAskAgainCheck);
  5558. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5559. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5560. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5561. if (Result == qaNeverAskAgain)
  5562. {
  5563. WinConfiguration->DDWarnLackOfTempSpace = false;
  5564. }
  5565. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5566. }
  5567. }
  5568. }
  5569. }
  5570. //---------------------------------------------------------------------------
  5571. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5572. {
  5573. DoOpenDirectoryDialog(odAddBookmark, Side);
  5574. }
  5575. //---------------------------------------------------------------------------
  5576. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5577. {
  5578. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5579. TStringList * VisitedDirectories = CreateSortedStringList((Side == osRemote));
  5580. try
  5581. {
  5582. TCustomDirView * DView = DirView(Side);
  5583. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5584. {
  5585. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5586. }
  5587. }
  5588. catch (...)
  5589. {
  5590. delete VisitedDirectories;
  5591. throw;
  5592. }
  5593. return VisitedDirectories;
  5594. }
  5595. //---------------------------------------------------------------------------
  5596. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5597. TOpenDirectoryMode Mode, TOperationSide Side)
  5598. {
  5599. bool Continue = true;
  5600. if (Mode == odAddBookmark)
  5601. {
  5602. TMessageParams Params(mpNeverAskAgainCheck);
  5603. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5604. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5605. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  5606. unsigned int Answer =
  5607. MessageDialog(Message,
  5608. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  5609. if (Answer == qaNeverAskAgain)
  5610. {
  5611. Continue = true;
  5612. WinConfiguration->UseSharedBookmarks = true;
  5613. }
  5614. else if (Answer == qaOK)
  5615. {
  5616. Continue = true;
  5617. WinConfiguration->UseSharedBookmarks = false;
  5618. }
  5619. else
  5620. {
  5621. Continue = false;
  5622. }
  5623. }
  5624. if (Continue)
  5625. {
  5626. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  5627. try
  5628. {
  5629. UnicodeString Name = DirView(Side)->PathName;
  5630. bool Repeat;
  5631. do
  5632. {
  5633. Repeat = false;
  5634. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  5635. // do not allow switching to location profiles,
  5636. // if we are not connected
  5637. HasDirView[osLocal] && (Terminal != NULL)))
  5638. {
  5639. TWindowLock Lock(this);
  5640. if (!TryOpenDirectory(Side, Name))
  5641. {
  5642. Repeat = true;
  5643. Mode = odBrowse;
  5644. }
  5645. }
  5646. }
  5647. while (Repeat);
  5648. }
  5649. __finally
  5650. {
  5651. delete VisitedDirectories;
  5652. }
  5653. }
  5654. }
  5655. //---------------------------------------------------------------------------
  5656. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  5657. {
  5658. DebugAssert(!FTerminal->CommandSessionOpened);
  5659. return TTerminalManager::Instance()->ConnectTerminal(FTerminal->CommandSession);
  5660. }
  5661. //---------------------------------------------------------------------------
  5662. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  5663. {
  5664. bool Result = FTerminal->IsCapable[Capability] ||
  5665. FTerminal->CommandSessionOpened;
  5666. if (!Result)
  5667. {
  5668. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  5669. if (!GUIConfiguration->ConfirmCommandSession)
  5670. {
  5671. Result = true;
  5672. }
  5673. else
  5674. {
  5675. TMessageParams Params(mpNeverAskAgainCheck);
  5676. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  5677. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  5678. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  5679. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  5680. if (Answer == qaNeverAskAgain)
  5681. {
  5682. GUIConfiguration->ConfirmCommandSession = false;
  5683. Result = true;
  5684. }
  5685. else if (Answer == qaOK)
  5686. {
  5687. Result = true;
  5688. }
  5689. }
  5690. if (Result)
  5691. {
  5692. Result = CommandSessionFallback();
  5693. }
  5694. }
  5695. return Result;
  5696. }
  5697. //---------------------------------------------------------------------------
  5698. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  5699. {
  5700. if (EnsureCommandSessionFallback(fcAnyCommand))
  5701. {
  5702. DoConsoleDialog(Terminal, Command);
  5703. }
  5704. }
  5705. //---------------------------------------------------------------------------
  5706. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  5707. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  5708. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  5709. {
  5710. if (IsFileControl(DropSourceControl, osRemote) &&
  5711. (FDDExtMapFile != NULL))
  5712. {
  5713. Accept = true;
  5714. }
  5715. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  5716. DebugAssert(FDDTargetControl != NULL);
  5717. }
  5718. //---------------------------------------------------------------------------
  5719. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  5720. _di_IDataObject DataObj, int KeyState,
  5721. const TPoint & Point, int & Effect, bool & Accept)
  5722. {
  5723. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  5724. }
  5725. //---------------------------------------------------------------------------
  5726. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  5727. _di_IDataObject DataObj, int KeyState,
  5728. const TPoint & Point, int & Effect, bool & Accept)
  5729. {
  5730. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  5731. }
  5732. //---------------------------------------------------------------------------
  5733. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  5734. TObject * Sender)
  5735. {
  5736. DebugUsedParam(Sender);
  5737. DebugAssert(FDDTargetControl == Sender);
  5738. FDDTargetControl = NULL;
  5739. }
  5740. //---------------------------------------------------------------------------
  5741. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  5742. {
  5743. FileControlDDDragLeave(SessionsPageControl);
  5744. }
  5745. //---------------------------------------------------------------------------
  5746. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  5747. {
  5748. FileControlDDDragLeave(QueueView3);
  5749. }
  5750. //---------------------------------------------------------------------------
  5751. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  5752. {
  5753. DebugAssert(GetSide(Side) == osRemote);
  5754. DebugUsedParam(Side);
  5755. bool Edit = false;
  5756. TRemoteFile * File = NULL;
  5757. UnicodeString FileName;
  5758. UnicodeString PointTo;
  5759. bool SymbolicLink = true;
  5760. if (RemoteDirView->ItemFocused)
  5761. {
  5762. DebugAssert(RemoteDirView->ItemFocused->Data);
  5763. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  5764. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  5765. if (Edit)
  5766. {
  5767. FileName = File->FileName;
  5768. PointTo = File->LinkTo;
  5769. }
  5770. else
  5771. {
  5772. PointTo = File->FileName;
  5773. }
  5774. }
  5775. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  5776. Terminal->IsCapable[fcHardLink]))
  5777. {
  5778. Configuration->Usage->Inc(L"RemoteLinksCreated");
  5779. if (Edit)
  5780. {
  5781. DebugAssert(File->FileName == FileName);
  5782. int Params = dfNoRecursive;
  5783. Terminal->ExceptionOnFail = true;
  5784. try
  5785. {
  5786. Terminal->DeleteFile(L"", File, &Params);
  5787. }
  5788. __finally
  5789. {
  5790. Terminal->ExceptionOnFail = false;
  5791. }
  5792. }
  5793. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  5794. }
  5795. }
  5796. //---------------------------------------------------------------------------
  5797. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  5798. {
  5799. return
  5800. (Terminal != NULL) &&
  5801. ((GetSide(Side) != osRemote) ||
  5802. (Terminal->ResolvingSymlinks &&
  5803. Terminal->IsCapable[fcSymbolicLink]));
  5804. }
  5805. //---------------------------------------------------------------------------
  5806. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  5807. {
  5808. return
  5809. (FCurrentSide == osRemote) &&
  5810. (RemoteDirView->ItemFocused != NULL) &&
  5811. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  5812. Terminal->SessionData->ResolveSymlinks;
  5813. }
  5814. //---------------------------------------------------------------------------
  5815. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  5816. {
  5817. DebugAssert(!WinConfiguration->DisableOpenEdit);
  5818. FForceExecution = true;
  5819. try
  5820. {
  5821. DirView(osCurrent)->ExecuteCurrentFile();
  5822. }
  5823. __finally
  5824. {
  5825. FForceExecution = false;
  5826. }
  5827. }
  5828. //---------------------------------------------------------------------------
  5829. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  5830. {
  5831. TEditorData ExternalEditor;
  5832. ExternalEditor.Editor = edExternal;
  5833. bool Remember = false;
  5834. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc,
  5835. (GetSide(osCurrent) == osRemote)))
  5836. {
  5837. if (Remember)
  5838. {
  5839. TEditorList * EditorList = new TEditorList;
  5840. try
  5841. {
  5842. *EditorList = *WinConfiguration->EditorList;
  5843. bool Found = false;
  5844. int i = 0;
  5845. while (!Found && (i < EditorList->Count))
  5846. {
  5847. const TEditorPreferences * Editor = EditorList->Editors[i];
  5848. if ((Editor->Data->Editor == edExternal) &&
  5849. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  5850. {
  5851. Found = true;
  5852. }
  5853. i++;
  5854. }
  5855. if (!Found)
  5856. {
  5857. EditorList->Add(new TEditorPreferences(ExternalEditor));
  5858. WinConfiguration->EditorList = EditorList;
  5859. }
  5860. }
  5861. __finally
  5862. {
  5863. delete EditorList;
  5864. }
  5865. }
  5866. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  5867. }
  5868. }
  5869. //---------------------------------------------------------------------------
  5870. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  5871. {
  5872. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  5873. if (FFileFindTerminal == Sender)
  5874. {
  5875. FFileFindTerminal = NULL;
  5876. HideFileFindDialog();
  5877. }
  5878. if (FClipboardTerminal == Sender)
  5879. {
  5880. ClipboardClear(); // implies ClipboardStop
  5881. }
  5882. }
  5883. //---------------------------------------------------------------------------
  5884. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  5885. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  5886. {
  5887. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  5888. DebugAssert(Terminal != NULL);
  5889. if (Data->Terminal == Terminal)
  5890. {
  5891. Data->Terminal = NULL;
  5892. }
  5893. }
  5894. //---------------------------------------------------------------------------
  5895. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  5896. {
  5897. UpdateControls();
  5898. SessionColor = TColor(0);
  5899. UpdateRemotePathComboBox(false);
  5900. NeedSession(true);
  5901. }
  5902. //---------------------------------------------------------------------------
  5903. void __fastcall TCustomScpExplorerForm::NeedSession(bool ReloadSessions)
  5904. {
  5905. try
  5906. {
  5907. try
  5908. {
  5909. TTerminalManager::Instance()->NewSession(false, L"", ReloadSessions, this);
  5910. }
  5911. __finally
  5912. {
  5913. if (!WinConfiguration->KeepOpenWhenNoSession &&
  5914. (!Terminal || !Terminal->Active))
  5915. {
  5916. TerminateApplication();
  5917. }
  5918. }
  5919. }
  5920. catch (EAbort &)
  5921. {
  5922. // swallow
  5923. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  5924. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  5925. }
  5926. }
  5927. //---------------------------------------------------------------------------
  5928. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged()
  5929. {
  5930. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  5931. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  5932. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  5933. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  5934. try
  5935. {
  5936. while ((SessionsPageControl->PageCount > TerminalList->Count + 1) ||
  5937. // Clear the design time unthemed tab
  5938. ((SessionsPageControl->PageCount > 0) &&
  5939. (dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[SessionsPageControl->PageCount - 1]) == NULL)))
  5940. {
  5941. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  5942. }
  5943. for (int Index = 0; Index <= TerminalList->Count; Index++)
  5944. {
  5945. TThemeTabSheet * TabSheet;
  5946. if (Index >= SessionsPageControl->PageCount)
  5947. {
  5948. TabSheet = new TThemeTabSheet(SessionsPageControl);
  5949. TabSheet->PageControl = SessionsPageControl;
  5950. }
  5951. else
  5952. {
  5953. TabSheet = DebugNotNull(dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[Index]));
  5954. }
  5955. bool IsSessionTab = (Index < TerminalList->Count);
  5956. if (IsSessionTab)
  5957. {
  5958. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  5959. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  5960. UpdateSessionTab(TabSheet);
  5961. }
  5962. else
  5963. {
  5964. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  5965. TabSheet->Tag = 0; // not really needed
  5966. // 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)
  5967. UpdateNewSessionTab();
  5968. }
  5969. TabSheet->ShowCloseButton = IsSessionTab;
  5970. }
  5971. }
  5972. __finally
  5973. {
  5974. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  5975. }
  5976. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  5977. }
  5978. //---------------------------------------------------------------------------
  5979. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  5980. {
  5981. DoTerminalListChanged();
  5982. }
  5983. //---------------------------------------------------------------------------
  5984. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  5985. {
  5986. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  5987. DebugAssert(TabSheet->ImageIndex == 0);
  5988. TabSheet->Caption =
  5989. WinConfiguration->SelectiveToolbarText ?
  5990. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  5991. UnicodeString();
  5992. }
  5993. //---------------------------------------------------------------------------
  5994. TTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  5995. {
  5996. return reinterpret_cast<TTerminal *>(TabSheet->Tag);
  5997. }
  5998. //---------------------------------------------------------------------------
  5999. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  6000. {
  6001. if (DebugAlwaysTrue(TabSheet != NULL))
  6002. {
  6003. TManagedTerminal * ManagedTerminal =
  6004. dynamic_cast<TManagedTerminal *>(GetSessionTabTerminal(TabSheet));
  6005. if (DebugAlwaysTrue(ManagedTerminal != NULL))
  6006. {
  6007. TColor Color = (ManagedTerminal == FTerminal) ? FSessionColor : ManagedTerminal->StateData->Color;
  6008. TabSheet->ImageIndex = AddSessionColor(Color);
  6009. UnicodeString TabCaption = TTerminalManager::Instance()->GetTerminalTitle(ManagedTerminal, true);
  6010. TabSheet->Caption = SessionsPageControl->FormatCaptionWithCloseButton(TabCaption);
  6011. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  6012. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  6013. {
  6014. ThemeTabSheet->Shadowed = !ManagedTerminal->Active;
  6015. }
  6016. }
  6017. }
  6018. }
  6019. //---------------------------------------------------------------------------
  6020. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6021. {
  6022. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6023. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  6024. bool Result = (Terminal != NULL);
  6025. if (Result)
  6026. {
  6027. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6028. }
  6029. else
  6030. {
  6031. try
  6032. {
  6033. NewSession(false);
  6034. }
  6035. __finally
  6036. {
  6037. DoTerminalListChanged();
  6038. }
  6039. FSessionsPageControlNewSessionTime = Now();
  6040. }
  6041. return Result;
  6042. }
  6043. //---------------------------------------------------------------------------
  6044. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6045. {
  6046. SessionTabSwitched();
  6047. }
  6048. //---------------------------------------------------------------------------
  6049. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6050. {
  6051. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6052. DebugAssert(TransferList != NULL);
  6053. UnicodeString Name;
  6054. if (TransferList->ItemIndex <= 0)
  6055. {
  6056. Name = L"";
  6057. }
  6058. else
  6059. {
  6060. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6061. }
  6062. if (FCopyParamAutoSelected.IsEmpty())
  6063. {
  6064. // if previous preset was not autoselected, make new preset the "default"
  6065. FCopyParamDefault = Name;
  6066. }
  6067. GUIConfiguration->CopyParamCurrent = Name;
  6068. }
  6069. //---------------------------------------------------------------------------
  6070. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6071. {
  6072. // sanity check
  6073. bool ExistingPreset =
  6074. (FTransferListHoverIndex >= 0) &&
  6075. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6076. DebugAssert(ExistingPreset);
  6077. if (ExistingPreset)
  6078. {
  6079. HDC DC = GetDC(0);
  6080. TCanvas * Canvas = new TCanvas();
  6081. try
  6082. {
  6083. Canvas->Handle = DC;
  6084. Canvas->Font = GetToolbarFont(this);
  6085. UnicodeString Name;
  6086. if (FTransferListHoverIndex == 0)
  6087. {
  6088. Name = L"";
  6089. }
  6090. else
  6091. {
  6092. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6093. }
  6094. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6095. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6096. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6097. static_cast<TObject*>(GetComponent(fcTransferList)));
  6098. UnicodeString InfoStr =
  6099. GUIConfiguration->CopyParamPreset[Name].
  6100. GetInfoStr(L"; ",
  6101. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General));
  6102. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6103. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6104. {
  6105. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6106. {
  6107. InfoStr.SetLength(InfoStr.Length() - 1);
  6108. }
  6109. InfoStr += Ellipsis;
  6110. }
  6111. // UpdateCaption does not cause invalidation of whole submenu, while
  6112. // setting Caption property does.
  6113. // also it probably does not resize the label, even if necessary
  6114. // (we do not want that anyway)
  6115. TransferLabel->UpdateCaption(InfoStr);
  6116. }
  6117. __finally
  6118. {
  6119. Canvas->Handle = NULL;
  6120. ReleaseDC(0, DC);
  6121. delete Canvas;
  6122. }
  6123. }
  6124. }
  6125. //---------------------------------------------------------------------------
  6126. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6127. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6128. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6129. {
  6130. if (FTransferListHoverIndex != AHoverIndex)
  6131. {
  6132. FTransferListHoverIndex = AHoverIndex;
  6133. UpdateTransferLabel();
  6134. }
  6135. }
  6136. //---------------------------------------------------------------------------
  6137. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6138. {
  6139. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6140. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6141. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6142. {
  6143. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6144. {
  6145. OpenDirectory(GetSide(osCurrent));
  6146. Message.Result = 1;
  6147. }
  6148. else
  6149. {
  6150. TForm::Dispatch(&Message);
  6151. }
  6152. }
  6153. else
  6154. {
  6155. TForm::Dispatch(&Message);
  6156. }
  6157. }
  6158. //---------------------------------------------------------------------------
  6159. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6160. {
  6161. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6162. if ((FIgnoreNextDialogChar != 0) &&
  6163. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6164. {
  6165. Message.Result = 1;
  6166. }
  6167. else
  6168. {
  6169. TForm::Dispatch(&Message);
  6170. }
  6171. FIgnoreNextDialogChar = 0;
  6172. }
  6173. //---------------------------------------------------------------------------
  6174. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6175. {
  6176. // The four low-order bits are used internally by Windows
  6177. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6178. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6179. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6180. // 61730 - restore thru double click - undocumented
  6181. // 61490 - maximize thru double click - undocumented
  6182. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6183. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6184. {
  6185. SysResizing(Cmd);
  6186. }
  6187. TForm::Dispatch(&Message);
  6188. }
  6189. //---------------------------------------------------------------------------
  6190. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6191. {
  6192. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6193. // Also there no point returning TRUE as we are not able to
  6194. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6195. // Hence the process termination might be safer :)
  6196. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6197. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6198. {
  6199. Message.Result = FALSE;
  6200. }
  6201. else
  6202. {
  6203. Message.Result = TRUE;
  6204. }
  6205. // Do not call default handling as that triggers OnCloseQuery,
  6206. // where our implementation will popup confirmation dialogs, what we do not want,
  6207. // as per Vista guidelines:
  6208. // https://docs.microsoft.com/en-us/windows/desktop/shutdown/shutting-down
  6209. }
  6210. //---------------------------------------------------------------------------
  6211. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6212. {
  6213. if (Message.EndSession && IsApplicationMinimized())
  6214. {
  6215. // WORKAROUND
  6216. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6217. // when it receives WM_ENDSESSION,
  6218. // but that sometimes (particularly when application is minimized) cause crashes.
  6219. // Crash popups message that blocks log off.
  6220. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6221. // so we call ExitProcess() immediately, not even trying to cleanup.
  6222. // It still causes beep, so there's likely some popup, but it does not block
  6223. // log off.
  6224. ExitProcess(0);
  6225. }
  6226. TForm::Dispatch(&Message);
  6227. }
  6228. #ifdef _DEBUG
  6229. //---------------------------------------------------------------------------
  6230. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6231. {
  6232. TForm::Dispatch(&Message);
  6233. }
  6234. #endif
  6235. //---------------------------------------------------------------------------
  6236. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6237. {
  6238. }
  6239. //---------------------------------------------------------------------------
  6240. void __fastcall TCustomScpExplorerForm::DoShow()
  6241. {
  6242. // only now are the controls resized finally, so the size constraints
  6243. // will not conflict with possibly very small window size
  6244. RestoreFormParams();
  6245. FixControlsPlacement();
  6246. if (Position == poDefaultPosOnly)
  6247. {
  6248. CutFormToDesktop(this);
  6249. }
  6250. TForm::DoShow();
  6251. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6252. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6253. if (Terminal == NULL)
  6254. {
  6255. StartingDisconnected();
  6256. }
  6257. UpdatePixelsPerInchMainWindowCounter();
  6258. FShowing = true;
  6259. }
  6260. //---------------------------------------------------------------------------
  6261. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6262. {
  6263. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6264. }
  6265. //---------------------------------------------------------------------------
  6266. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  6267. {
  6268. DoTerminalListChanged();
  6269. }
  6270. //---------------------------------------------------------------------------
  6271. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6272. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6273. TNotifyEvent OnBalloonClick, TObject * UserData)
  6274. {
  6275. bool Do;
  6276. UnicodeString Message;
  6277. if (E == NULL)
  6278. {
  6279. Message = Str;
  6280. Do = true;
  6281. }
  6282. else
  6283. {
  6284. Do = ExceptionMessage(E, Message);
  6285. }
  6286. if (Do && WinConfiguration->BalloonNotifications)
  6287. {
  6288. UnicodeString Title;
  6289. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6290. {
  6291. Message = TrimLeft(Message);
  6292. }
  6293. else
  6294. {
  6295. Message = UnformatMessage(Message);
  6296. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6297. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6298. Title = LoadResourceString(Captions[Type]);
  6299. if (Terminal != NULL)
  6300. {
  6301. Title = FORMAT(L"%s - %s",
  6302. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), Title));
  6303. }
  6304. }
  6305. if (Seconds == 0)
  6306. {
  6307. Seconds = WinConfiguration->NotificationsTimeout;
  6308. }
  6309. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6310. }
  6311. }
  6312. //---------------------------------------------------------------------------
  6313. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6314. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6315. UnicodeString HelpKeyword, const TMessageParams * Params,
  6316. TTerminal * Terminal)
  6317. {
  6318. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6319. (FOnFeedSynchronizeError != NULL)) &&
  6320. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6321. {
  6322. if (FOnFeedSynchronizeError != NULL)
  6323. {
  6324. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6325. }
  6326. else
  6327. {
  6328. DebugAssert(FErrorList != NULL);
  6329. TStringList * MoreMessagesCopy = NULL;
  6330. if (MoreMessages)
  6331. {
  6332. MoreMessagesCopy = new TStringList();
  6333. MoreMessagesCopy->Assign(MoreMessages);
  6334. }
  6335. FErrorList->AddObject(Message, MoreMessagesCopy);
  6336. }
  6337. PopupTrayBalloon(Terminal, Message, Type);
  6338. return ContinueAnswer(Answers);
  6339. }
  6340. else
  6341. {
  6342. bool UseBalloon = FTrayIcon->Visible;
  6343. if (UseBalloon)
  6344. {
  6345. PopupTrayBalloon(Terminal, Message, Type);
  6346. }
  6347. unsigned int Result;
  6348. try
  6349. {
  6350. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6351. }
  6352. __finally
  6353. {
  6354. // cancel only balloon we popped up, otherwise we may cancel notification
  6355. // balloon that was there before the message dialog
  6356. // (such as "dd confirmation opt in balloon)
  6357. if (UseBalloon)
  6358. {
  6359. FTrayIcon->CancelBalloon();
  6360. }
  6361. }
  6362. return Result;
  6363. }
  6364. }
  6365. //---------------------------------------------------------------------------
  6366. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6367. TTerminal * Terminal, Exception * E)
  6368. {
  6369. if (FTrayIcon->Visible)
  6370. {
  6371. PopupTrayBalloon(Terminal, L"", qtError, E);
  6372. }
  6373. // particularly prevent opening new session from jump list,
  6374. // while exception is shown
  6375. NonVisualDataModule->StartBusy();
  6376. try
  6377. {
  6378. ShowExtendedExceptionEx(Terminal, E);
  6379. }
  6380. __finally
  6381. {
  6382. NonVisualDataModule->EndBusy();
  6383. FTrayIcon->CancelBalloon();
  6384. }
  6385. }
  6386. //---------------------------------------------------------------------------
  6387. void __fastcall TCustomScpExplorerForm::TerminalReady()
  6388. {
  6389. // cannot rely on active page being page for active terminal,
  6390. // as it can happen that active page is the "new session" page
  6391. // (e.g. when reconnecting active terminal, while login dialog
  6392. // invoked from "new session" page is modal)
  6393. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6394. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  6395. }
  6396. //---------------------------------------------------------------------------
  6397. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6398. TTerminal * Terminal, Exception * E)
  6399. {
  6400. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6401. if (!Terminal->Active)
  6402. {
  6403. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6404. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6405. {
  6406. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6407. }
  6408. }
  6409. }
  6410. //---------------------------------------------------------------------------
  6411. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6412. UnicodeString Message, TQueryType Type,
  6413. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6414. {
  6415. if ((E == NULL) ||
  6416. ExceptionMessage(E, Message))
  6417. {
  6418. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6419. if (Important)
  6420. {
  6421. Seconds *= 5;
  6422. }
  6423. UnicodeString NoteMessage(UnformatMessage(Message));
  6424. if (Terminal != NULL)
  6425. {
  6426. NoteMessage = FORMAT(L"%s: %s",
  6427. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), NoteMessage));
  6428. }
  6429. if (WinConfiguration->BalloonNotifications)
  6430. {
  6431. AddNote(NoteMessage);
  6432. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6433. }
  6434. else
  6435. {
  6436. FlashOnBackground();
  6437. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6438. }
  6439. }
  6440. }
  6441. //---------------------------------------------------------------------------
  6442. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6443. {
  6444. RestoreApp();
  6445. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6446. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6447. !NonVisualDataModule->Busy)
  6448. {
  6449. TTerminal * Terminal = TerminalNoteData->Terminal;
  6450. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6451. if (!ComponentVisible[fcQueueView])
  6452. {
  6453. ToggleQueueVisibility();
  6454. GoToQueue();
  6455. }
  6456. }
  6457. }
  6458. //---------------------------------------------------------------------------
  6459. void __fastcall TCustomScpExplorerForm::QueueEvent(TTerminal * ATerminal,
  6460. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6461. {
  6462. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  6463. UnicodeString Message;
  6464. TNotifyEvent OnClick = NULL;
  6465. TObject * UserData = NULL;
  6466. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6467. switch (Event)
  6468. {
  6469. case qeEmptyButMonitored:
  6470. if ((ATerminal != Terminal) || QueueInvisible)
  6471. {
  6472. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6473. OnClick = QueueEmptyNoteClicked;
  6474. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6475. TerminalNoteData->Terminal = ATerminal;
  6476. UserData = TerminalNoteData;
  6477. }
  6478. break;
  6479. case qeEmpty:
  6480. OperationComplete(ManagedTerminal->QueueOperationStart);
  6481. break;
  6482. case qePendingUserAction:
  6483. if ((ATerminal != Terminal) ||
  6484. (QueueInvisible && !IsQueueAutoPopup()))
  6485. {
  6486. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6487. }
  6488. break;
  6489. default:
  6490. DebugFail();
  6491. }
  6492. if (!Message.IsEmpty())
  6493. {
  6494. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6495. }
  6496. }
  6497. //---------------------------------------------------------------------------
  6498. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6499. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6500. {
  6501. if (FDDExtMapFile != NULL)
  6502. {
  6503. CloseHandle(FDDExtMapFile);
  6504. FDDExtMapFile = NULL;
  6505. }
  6506. if (WinConfiguration->DDFakeFile)
  6507. {
  6508. DDFakeFileInitDrag(FileList, Created);
  6509. }
  6510. }
  6511. //---------------------------------------------------------------------------
  6512. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  6513. {
  6514. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  6515. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  6516. {
  6517. FFakeFileDropTarget = FileName;
  6518. }
  6519. }
  6520. //---------------------------------------------------------------------------
  6521. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  6522. {
  6523. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6524. if (!ForceDirectories(ApiPath(Result)))
  6525. {
  6526. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  6527. }
  6528. FileSetAttr(ApiPath(Result), faHidden);
  6529. return Result;
  6530. }
  6531. //---------------------------------------------------------------------------
  6532. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  6533. bool & Created)
  6534. {
  6535. FFakeFileDropTarget = UnicodeString();
  6536. FDragFakeDirectory = CreateFakeTransferDirectory();
  6537. FileList->AddItem(NULL, FDragFakeDirectory);
  6538. Created = true;
  6539. if (!WinConfiguration->IsDDExtRunning() || (GetWindowsBuild() >= 17134))
  6540. {
  6541. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  6542. }
  6543. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6544. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6545. {
  6546. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6547. TDragExtCommStruct* CommStruct;
  6548. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6549. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6550. DebugAssert(CommStruct != NULL);
  6551. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  6552. CommStruct->Dragging = true;
  6553. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  6554. LENOF(CommStruct->DropDest));
  6555. NULL_TERMINATE(CommStruct->DropDest);
  6556. UnmapViewOfFile(CommStruct);
  6557. }
  6558. }
  6559. //---------------------------------------------------------------------------
  6560. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  6561. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  6562. {
  6563. bool Result;
  6564. if (Sender == RemoteDirView)
  6565. {
  6566. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  6567. }
  6568. else
  6569. {
  6570. DebugAssert(Sender == RemoteDriveView);
  6571. TStrings * FileList = RemoteDriveView->DragFileList();
  6572. try
  6573. {
  6574. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  6575. }
  6576. __finally
  6577. {
  6578. delete FileList;
  6579. }
  6580. }
  6581. if (FDDTargetControl == RemoteDriveView)
  6582. {
  6583. RemoteDriveView->UpdateDropTarget();
  6584. }
  6585. // foRemoteMove happens when file/dir is dragged within the remote tree view
  6586. // or from tree view to dir view.
  6587. // foMove happens when file/dir is dragged from remote tree view outside of
  6588. // application via dragex
  6589. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  6590. (DropSourceControl == RemoteDriveView))
  6591. {
  6592. RemoteDriveView->UpdateDropSource();
  6593. }
  6594. return Result;
  6595. }
  6596. //---------------------------------------------------------------------------
  6597. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  6598. {
  6599. // This also handles drops of remote files to queue.
  6600. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  6601. SAFE_DESTROY(FDDFileList);
  6602. if (!FFakeFileDropTarget.IsEmpty())
  6603. {
  6604. RemoveDir(ApiPath(FFakeFileDropTarget));
  6605. }
  6606. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  6607. {
  6608. try
  6609. {
  6610. TDragResult DDResult = (Sender == RemoteDirView) ?
  6611. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  6612. // Focus is moved to the target application,
  6613. // but as we are going to present the UI, we need to steal the focus back.
  6614. // This most likely won't work though (windows does not allow application
  6615. // to steal focus most of the time)
  6616. Application->BringToFront();
  6617. // On older version of Windows we never got drMove here, see also comment below.
  6618. // On Windows 10, we get the "move".
  6619. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  6620. {
  6621. UnicodeString TargetDirectory;
  6622. TFileOperation Operation;
  6623. // drInvalid may mean drMove, see comment below
  6624. switch (DDResult)
  6625. {
  6626. case drCopy:
  6627. Operation = foCopy;
  6628. break;
  6629. case drMove:
  6630. Operation = foMove;
  6631. break;
  6632. default:
  6633. DebugFail();
  6634. case drInvalid:
  6635. // prefer "copy" for safety
  6636. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  6637. break;
  6638. }
  6639. TTransferOperationParam Param;
  6640. UnicodeString CounterName;
  6641. bool ForceQueue;
  6642. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  6643. {
  6644. // we get drInvalid both if d&d was intercepted by ddext,
  6645. // and when users drops on no-drop location.
  6646. // we tell the difference by existence of response from ddext,
  6647. // so we ignore absence of response in this case
  6648. if (DDResult != drInvalid)
  6649. {
  6650. // here we know that the extension is installed,
  6651. // as it is checked as soon as drag&drop starts from
  6652. // RemoteFileControlDDCreateDragFileList
  6653. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  6654. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  6655. }
  6656. }
  6657. else
  6658. {
  6659. // download using fake file
  6660. Param.Temp = false;
  6661. Param.DragDrop = true;
  6662. if (ForceQueue)
  6663. {
  6664. Param.Queue = asOn;
  6665. }
  6666. if (Sender == RemoteDirView)
  6667. {
  6668. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  6669. }
  6670. if (RemoteFileControlFileOperation(Sender, Operation,
  6671. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  6672. {
  6673. Configuration->Usage->Inc(CounterName);
  6674. }
  6675. }
  6676. }
  6677. }
  6678. __finally
  6679. {
  6680. CloseHandle(FDDExtMapFile);
  6681. FDDExtMapFile = NULL;
  6682. delete FDragFakeMonitors;
  6683. FDragFakeMonitors = NULL;
  6684. RemoveDir(ApiPath(FDragFakeDirectory));
  6685. FDragFakeDirectory = L"";
  6686. FFakeFileDropTarget = UnicodeString();
  6687. }
  6688. }
  6689. if (FDragDropSshTerminate.get() != NULL)
  6690. {
  6691. FDragDropSshTerminate->Rethrow();
  6692. }
  6693. }
  6694. //---------------------------------------------------------------------------
  6695. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  6696. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  6697. {
  6698. // Remember drop effect so we know (when user drops files), if we copy or move
  6699. FLastDropEffect = dwEffect;
  6700. }
  6701. //---------------------------------------------------------------------------
  6702. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  6703. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  6704. {
  6705. bool Result;
  6706. if (FDDTargetControl == QueueView3)
  6707. {
  6708. Directory = DefaultDownloadTargetDirectory();
  6709. Result = true;
  6710. CounterName = L"DownloadsDragDropQueue";
  6711. ForceQueue = true;
  6712. }
  6713. else if (!FFakeFileDropTarget.IsEmpty())
  6714. {
  6715. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  6716. Result = true;
  6717. ForceQueue = false;
  6718. CounterName = L"DownloadsDragDropFakeFile";
  6719. }
  6720. else
  6721. {
  6722. ForceQueue = false;
  6723. Enabled = false;
  6724. try
  6725. {
  6726. int Timer = 0;
  6727. Result = false;
  6728. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  6729. {
  6730. {
  6731. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6732. TDragExtCommStruct* CommStruct;
  6733. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6734. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6735. DebugAssert(CommStruct != NULL);
  6736. Result = !CommStruct->Dragging;
  6737. if (Result)
  6738. {
  6739. Directory = ExtractFilePath(CommStruct->DropDest);
  6740. CounterName = L"DownloadsDragDropExternalExt";
  6741. }
  6742. UnmapViewOfFile(CommStruct);
  6743. }
  6744. if (!Result)
  6745. {
  6746. Sleep(50);
  6747. Timer += 50;
  6748. Application->ProcessMessages();
  6749. }
  6750. }
  6751. }
  6752. __finally
  6753. {
  6754. Enabled = true;
  6755. }
  6756. }
  6757. return Result;
  6758. }
  6759. //---------------------------------------------------------------------------
  6760. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  6761. const UnicodeString TempDir, int SecDelay)
  6762. {
  6763. TDateTime Alarm = Now() + (double)((double)SecDelay*OneMillisecond);
  6764. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  6765. if (FDelayedDeletionTimer == NULL)
  6766. {
  6767. DebugAssert(HandleAllocated());
  6768. FDelayedDeletionTimer = new TTimer(this);
  6769. FDelayedDeletionTimer->Interval = 10000;
  6770. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  6771. }
  6772. else
  6773. {
  6774. FDelayedDeletionTimer->Enabled = true;
  6775. }
  6776. }
  6777. //---------------------------------------------------------------------------
  6778. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  6779. {
  6780. DebugAssert(FDelayedDeletionList != NULL);
  6781. TDateTime N = Now();
  6782. TDateTime Alert;
  6783. UnicodeString Directory;
  6784. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  6785. {
  6786. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  6787. if ((N >= Alert) || (Sender == NULL))
  6788. {
  6789. Directory = FDelayedDeletionList->Strings[Index];
  6790. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  6791. {
  6792. FDelayedDeletionList->Delete(Index);
  6793. }
  6794. }
  6795. }
  6796. if (FDelayedDeletionList->Count == 0)
  6797. {
  6798. FDelayedDeletionTimer->Enabled = false;
  6799. }
  6800. }
  6801. //---------------------------------------------------------------------------
  6802. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  6803. {
  6804. if (IsFileControl(FDDTargetControl, osRemote) ||
  6805. (FDDTargetControl == SessionsPageControl))
  6806. {
  6807. TTerminal * TargetTerminal = NULL;
  6808. TFileOperation Operation = foNone;
  6809. if (FDDTargetControl == SessionsPageControl)
  6810. {
  6811. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  6812. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  6813. // do not allow dropping on the "+" tab
  6814. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  6815. if (TargetTerminal != NULL)
  6816. {
  6817. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  6818. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  6819. {
  6820. Operation = foRemoteMove;
  6821. }
  6822. else
  6823. {
  6824. Operation = foRemoteCopy;
  6825. }
  6826. }
  6827. }
  6828. else
  6829. {
  6830. if (FLastDropEffect == DROPEFFECT_MOVE)
  6831. {
  6832. Operation = foRemoteMove;
  6833. }
  6834. else if (FLastDropEffect == DROPEFFECT_COPY)
  6835. {
  6836. Operation = foRemoteCopy;
  6837. }
  6838. }
  6839. if (Operation != foNone)
  6840. {
  6841. RemoteFileControlFileOperation(DropSourceControl,
  6842. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  6843. }
  6844. // abort drag&drop
  6845. Abort();
  6846. }
  6847. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  6848. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  6849. (FDDTargetControl != QueueView3))
  6850. {
  6851. DebugAssert(!FDragTempDir.IsEmpty());
  6852. TTransferType Type;
  6853. UnicodeString TempDir = FDragTempDir;
  6854. // We clear FDragTempDir before calling
  6855. // just in case it fail (raises exception)
  6856. FDragTempDir = L"";
  6857. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  6858. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  6859. // empty directory parameter means temp directory -> don't display it!
  6860. UnicodeString TargetDir = L"";
  6861. int Options = 0;
  6862. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  6863. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  6864. {
  6865. Abort();
  6866. }
  6867. // TargetDir is set when dropped on local file control
  6868. // (this was workaround for legacy dirview event handling, now it should be
  6869. // made prettier)
  6870. if (TargetDir.IsEmpty())
  6871. {
  6872. TargetDir = TempDir;
  6873. if (ForceDirectories(ApiPath(TargetDir)))
  6874. {
  6875. DebugAssert(Terminal && !TargetDir.IsEmpty());
  6876. FPendingTempSpaceWarn = true;
  6877. try
  6878. {
  6879. FDragDropOperation = true;
  6880. int Params = cpTemporary |
  6881. (Type == ttMove ? cpDelete : 0);
  6882. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  6883. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  6884. }
  6885. __finally
  6886. {
  6887. FDragDropOperation = false;
  6888. FPendingTempSpaceWarn = false;
  6889. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  6890. }
  6891. }
  6892. else
  6893. {
  6894. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  6895. }
  6896. }
  6897. }
  6898. }
  6899. //---------------------------------------------------------------------------
  6900. void __fastcall TCustomScpExplorerForm::DDDownload(
  6901. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  6902. {
  6903. TAutoBatch AutoBatch(this);
  6904. UpdateCopyParamCounters(*CopyParam);
  6905. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  6906. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  6907. {
  6908. RemoteDriveView->UpdateDropSource();
  6909. }
  6910. }
  6911. //---------------------------------------------------------------------------
  6912. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  6913. {
  6914. public:
  6915. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  6916. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  6917. {
  6918. }
  6919. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  6920. {
  6921. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  6922. TDataObjectFilesEx::AllowData(FormatEtc);
  6923. }
  6924. };
  6925. //---------------------------------------------------------------------------
  6926. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  6927. TObject * Sender, TDataObject *& DataObject)
  6928. {
  6929. if (FDDExtMapFile != NULL)
  6930. {
  6931. TFileList * FileList = DragDropFiles(Sender)->FileList;
  6932. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  6933. {
  6934. Abort();
  6935. }
  6936. if (FileList->Count > 0)
  6937. {
  6938. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  6939. if (!FilesObject->IsValid(true, true))
  6940. {
  6941. FilesObject->_Release();
  6942. }
  6943. else
  6944. {
  6945. DataObject = FilesObject;
  6946. }
  6947. }
  6948. }
  6949. }
  6950. //---------------------------------------------------------------------------
  6951. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  6952. {
  6953. DebugFail();
  6954. }
  6955. //---------------------------------------------------------------------------
  6956. void __fastcall TCustomScpExplorerForm::GoToTree()
  6957. {
  6958. ComponentVisible[fcRemoteTree] = true;
  6959. RemoteDriveView->SetFocus();
  6960. }
  6961. //---------------------------------------------------------------------------
  6962. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  6963. TPanelExport Export)
  6964. {
  6965. TCustomDirView * DirView = this->DirView(Side);
  6966. std::unique_ptr<TStrings> ExportData(new TStringList());
  6967. switch (Export)
  6968. {
  6969. case pePath:
  6970. ExportData->Add(DirView->PathName);
  6971. break;
  6972. case peFileList:
  6973. case peFullFileList:
  6974. {
  6975. bool FullPath = (Export == peFullFileList);
  6976. DirView->CreateFileList(false, FullPath, ExportData.get());
  6977. for (int Index = 0; Index < ExportData->Count; Index++)
  6978. {
  6979. if (ExportData->Strings[Index].Pos(L" ") > 0)
  6980. {
  6981. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  6982. }
  6983. }
  6984. }
  6985. break;
  6986. default:
  6987. DebugFail();
  6988. }
  6989. return ExportData.release();
  6990. }
  6991. //---------------------------------------------------------------------------
  6992. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  6993. TPanelExport Export, TPanelExportDestination Destination)
  6994. {
  6995. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  6996. PanelExportStore(Side, Export, Destination, ExportData.get());
  6997. }
  6998. //---------------------------------------------------------------------------
  6999. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7000. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7001. TStrings * ExportData)
  7002. {
  7003. if (Destination == pedClipboard)
  7004. {
  7005. TInstantOperationVisualizer Visualizer;
  7006. CopyToClipboard(ExportData);
  7007. }
  7008. else
  7009. {
  7010. DebugFail();
  7011. }
  7012. }
  7013. //---------------------------------------------------------------------------
  7014. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7015. {
  7016. TCustomDirView * DirView = this->DirView(Side);
  7017. TFileFilter Filter;
  7018. DefaultFileFilter(Filter);
  7019. Filter.Masks = DirView->Mask;
  7020. if (DoFilterMaskDialog(DirView, &Filter))
  7021. {
  7022. DirView->Mask = TFileMasks::NormalizeMask(Filter.Masks);
  7023. Configuration->Usage->Inc(L"Filters");
  7024. }
  7025. }
  7026. //---------------------------------------------------------------------------
  7027. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7028. {
  7029. return FQueueController->DefaultOperation();
  7030. }
  7031. //---------------------------------------------------------------------------
  7032. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7033. TQueueOperation Operation, void ** Param)
  7034. {
  7035. switch (Operation)
  7036. {
  7037. case qoPreferences:
  7038. return true;
  7039. case qoGoTo:
  7040. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7041. case qoOnceEmpty:
  7042. return !FQueueController->Empty;
  7043. default:
  7044. return FQueueController->AllowOperation(Operation, Param);
  7045. }
  7046. }
  7047. //---------------------------------------------------------------------------
  7048. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7049. {
  7050. if (DebugAlwaysTrue(QueueView3->Visible))
  7051. {
  7052. QueueView3->SetFocus();
  7053. }
  7054. }
  7055. //---------------------------------------------------------------------------
  7056. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7057. TQueueOperation Operation, void * Param)
  7058. {
  7059. if (Operation == qoGoTo)
  7060. {
  7061. GoToQueue();
  7062. }
  7063. else if (Operation == qoPreferences)
  7064. {
  7065. PreferencesDialog(pmQueue);
  7066. }
  7067. else
  7068. {
  7069. FQueueController->ExecuteOperation(Operation, Param);
  7070. }
  7071. }
  7072. //---------------------------------------------------------------------------
  7073. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7074. {
  7075. return (Queue != NULL) && Queue->Enabled;
  7076. }
  7077. //---------------------------------------------------------------------------
  7078. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7079. {
  7080. DebugAssert(Queue != NULL);
  7081. if (Queue != NULL)
  7082. {
  7083. Queue->Enabled = !Queue->Enabled;
  7084. }
  7085. }
  7086. //---------------------------------------------------------------------------
  7087. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7088. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7089. {
  7090. FQueueActedItem = QueueView3->ItemFocused;
  7091. }
  7092. //---------------------------------------------------------------------------
  7093. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7094. {
  7095. return TopDock->Height + QueueSplitter->Height;
  7096. }
  7097. //---------------------------------------------------------------------------
  7098. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7099. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7100. {
  7101. // when queue is hidden by double-clicking splitter, stray attempt to
  7102. // resize the panel with strange value arrives, make sure it is ignored
  7103. if (ComponentVisible[fcQueueView])
  7104. {
  7105. int HeightLimit = ClientHeight - GetStaticComponentsHeight() -
  7106. RemotePanel->Constraints->MinHeight;
  7107. if (NewSize > HeightLimit)
  7108. {
  7109. NewSize = HeightLimit;
  7110. }
  7111. }
  7112. else
  7113. {
  7114. Accept = false;
  7115. }
  7116. }
  7117. //---------------------------------------------------------------------------
  7118. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7119. TDragObject *& /*DragObject*/)
  7120. {
  7121. FQueueActedItem = QueueView3->ItemFocused;
  7122. }
  7123. //---------------------------------------------------------------------------
  7124. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7125. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7126. {
  7127. Accept = true;
  7128. if (Source == QueueView3)
  7129. {
  7130. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7131. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7132. if (Accept)
  7133. {
  7134. TQueueItemProxy * QueueItem;
  7135. TQueueItemProxy * DestQueueItem;
  7136. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7137. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7138. Accept = (QueueItem != DestQueueItem) &&
  7139. (QueueItem->Status == TQueueItem::qsPending) &&
  7140. (DestQueueItem->Status == TQueueItem::qsPending);
  7141. }
  7142. }
  7143. }
  7144. //---------------------------------------------------------------------------
  7145. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7146. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7147. {
  7148. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7149. {
  7150. TQueueItemProxy * QueueItem;
  7151. TQueueItemProxy * DestQueueItem;
  7152. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7153. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7154. QueueItem->Move(DestQueueItem);
  7155. }
  7156. }
  7157. //---------------------------------------------------------------------------
  7158. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7159. {
  7160. if ((QueueView3->ItemFocused == NULL) &&
  7161. (QueueView3->Items->Count > 0))
  7162. {
  7163. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7164. }
  7165. QueueLabelUpdateStatus();
  7166. }
  7167. //---------------------------------------------------------------------------
  7168. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7169. {
  7170. QueueLabelUpdateStatus();
  7171. }
  7172. //---------------------------------------------------------------------------
  7173. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7174. {
  7175. QueueLabel->UpdateStatus();
  7176. }
  7177. //---------------------------------------------------------------------------
  7178. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7179. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7180. {
  7181. if (Selected)
  7182. {
  7183. NonVisualDataModule->UpdateNonVisibleActions();
  7184. }
  7185. }
  7186. //---------------------------------------------------------------------------
  7187. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7188. {
  7189. TDragDropFilesEx * Result = NULL;
  7190. if (Sender == SessionsPageControl)
  7191. {
  7192. Result = FSessionsDragDropFilesEx;
  7193. }
  7194. else if (Sender == QueueView3)
  7195. {
  7196. Result = FQueueDragDropFilesEx;
  7197. }
  7198. else
  7199. {
  7200. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7201. if (DirView != NULL)
  7202. {
  7203. Result = DirView->DragDropFilesEx;
  7204. }
  7205. else
  7206. {
  7207. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7208. if (DriveView != NULL)
  7209. {
  7210. Result = DriveView->DragDropFilesEx;
  7211. }
  7212. }
  7213. }
  7214. DebugAssert(Result != NULL);
  7215. return Result;
  7216. }
  7217. //---------------------------------------------------------------------------
  7218. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7219. {
  7220. return (DView->SelCount == DView->FilesCount);
  7221. }
  7222. //---------------------------------------------------------------------------
  7223. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7224. {
  7225. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7226. }
  7227. //---------------------------------------------------------------------------
  7228. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7229. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7230. {
  7231. TFileOperation Operation;
  7232. switch (Effect)
  7233. {
  7234. case DROPEFFECT_MOVE:
  7235. Operation = foMove;
  7236. break;
  7237. case DROPEFFECT_COPY:
  7238. // occures on WinXP (reported by user)
  7239. default:
  7240. Operation = foCopy;
  7241. break;
  7242. };
  7243. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7244. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7245. if (DragDropFilesEx->FileList->Count > 0)
  7246. {
  7247. TStrings * FileList = new TStringList();
  7248. try
  7249. {
  7250. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7251. {
  7252. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7253. }
  7254. FDragDropOperation = true;
  7255. TTransferOperationParam Param;
  7256. Param.TargetDirectory = TargetPath;
  7257. // upload, no temp dirs
  7258. Param.Temp = false;
  7259. Param.DragDrop = DragDrop;
  7260. Param.Options =
  7261. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7262. if (ForceQueue)
  7263. {
  7264. Param.Queue = asOn;
  7265. }
  7266. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7267. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7268. {
  7269. if (IsFileControl(DropSourceControl, osLocal))
  7270. {
  7271. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7272. }
  7273. else
  7274. {
  7275. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7276. }
  7277. }
  7278. }
  7279. __finally
  7280. {
  7281. FDragDropOperation = false;
  7282. delete FileList;
  7283. }
  7284. }
  7285. }
  7286. //---------------------------------------------------------------------------
  7287. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7288. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7289. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7290. {
  7291. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7292. }
  7293. //---------------------------------------------------------------------------
  7294. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7295. TObject * Sender, int grfKeyState, int & dwEffect)
  7296. {
  7297. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7298. if ((dwEffect != DROPEFFECT_None) &&
  7299. IsFileControl(DropSourceControl, osRemote))
  7300. {
  7301. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7302. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7303. (RemoteDirView->DropTarget == NULL))
  7304. {
  7305. dwEffect = DROPEFFECT_None;
  7306. }
  7307. else
  7308. {
  7309. if (dwEffect == DROPEFFECT_Copy)
  7310. {
  7311. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  7312. // currently we support copying always (at least via temporary directory);
  7313. // remove associated checks once this all proves stable and working
  7314. bool CopyCapable = true;
  7315. // if we do not support neither of operations, there's no discussion
  7316. if (!MoveCapable && !CopyCapable)
  7317. {
  7318. dwEffect = DROPEFFECT_None;
  7319. }
  7320. else
  7321. {
  7322. // The default effect inside remote panel is move,
  7323. // unless we do not support it, but support copy
  7324. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7325. {
  7326. dwEffect = MoveCapable ? DROPEFFECT_Move : DROPEFFECT_Copy;
  7327. }
  7328. else
  7329. {
  7330. // with ctrl-down, we want copy unless it is not supported
  7331. dwEffect = CopyCapable ? DROPEFFECT_Copy : DROPEFFECT_None;
  7332. }
  7333. }
  7334. }
  7335. }
  7336. }
  7337. }
  7338. //---------------------------------------------------------------------------
  7339. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7340. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7341. {
  7342. if (FDDTotalSize >= 0)
  7343. {
  7344. if (File->IsDirectory)
  7345. {
  7346. FDDTotalSize = -1;
  7347. }
  7348. else
  7349. {
  7350. FDDTotalSize += File->Size;
  7351. }
  7352. }
  7353. DebugAssert(!FDragTempDir.IsEmpty());
  7354. // TODO: this is quite ineffective
  7355. // TODO: what if invalid character replacement is disabled?
  7356. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7357. UnicodeString TransferFileName;
  7358. if (Sender == RemoteDriveView)
  7359. {
  7360. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7361. }
  7362. else
  7363. {
  7364. TransferFileName = File->FileName;
  7365. }
  7366. FDDFileList->AddObject(TransferFileName, File);
  7367. }
  7368. //---------------------------------------------------------------------------
  7369. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7370. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7371. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7372. {
  7373. // sometimes we do not get DDEnd so the list is not released
  7374. SAFE_DESTROY(FDDFileList);
  7375. FDDFileList = new TStringList();
  7376. FDragTempDir = WinConfiguration->TemporaryDir();
  7377. FDDTotalSize = 0;
  7378. FDragDropSshTerminate.reset(NULL);
  7379. }
  7380. //---------------------------------------------------------------------------
  7381. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7382. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7383. HRESULT & Result)
  7384. {
  7385. if (Result == DRAGDROP_S_DROP)
  7386. {
  7387. try
  7388. {
  7389. GlobalDragImageList->HideDragImage();
  7390. try
  7391. {
  7392. RemoteFileControlDDTargetDrop();
  7393. }
  7394. catch(ESshTerminate & E)
  7395. {
  7396. FDragDropSshTerminate.reset(E.Clone());
  7397. }
  7398. }
  7399. catch (Exception &E)
  7400. {
  7401. // If downloading fails we need to cancel drag&drop, otherwise
  7402. // Explorer shows error
  7403. // But by the way exception probably never reach this point as
  7404. // it's catched on way.
  7405. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7406. Result = DRAGDROP_S_CANCEL;
  7407. ShowExtendedException(Terminal, &E);
  7408. }
  7409. }
  7410. }
  7411. //---------------------------------------------------------------------------
  7412. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7413. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7414. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7415. {
  7416. TFileMasks::TParams MaskParams;
  7417. MaskParams.Size = Size;
  7418. MaskParams.Modification = Modification;
  7419. // this does not re-parse the mask if it is the same as the last time
  7420. FDirViewMatchMask = Masks;
  7421. bool ImplicitMatch;
  7422. Matches =
  7423. // RecurseInclude parameter has no effect as the Path is empty
  7424. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7425. (AllowImplicitMatches || !ImplicitMatch);
  7426. }
  7427. //---------------------------------------------------------------------------
  7428. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7429. TObject * Sender, TListItem * Item, WORD & Indexes)
  7430. {
  7431. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7432. UnicodeString Ext;
  7433. if (DirView == RemoteDirView)
  7434. {
  7435. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7436. }
  7437. else
  7438. {
  7439. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7440. }
  7441. if (SameText(Ext, Configuration->PartialExt))
  7442. {
  7443. Indexes |= oiPartial;
  7444. }
  7445. }
  7446. //---------------------------------------------------------------------------
  7447. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7448. {
  7449. return
  7450. DirViewEnabled(osCurrent) &&
  7451. DirView(osCurrent)->CanPasteFromClipBoard();
  7452. }
  7453. //---------------------------------------------------------------------------
  7454. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7455. {
  7456. bool Result = false;
  7457. if (CanPasteToDirViewFromClipBoard())
  7458. {
  7459. Result = true;
  7460. }
  7461. else
  7462. {
  7463. UnicodeString ClipboardText;
  7464. if (NonEmptyTextFromClipboard(ClipboardText))
  7465. {
  7466. if (StoredSessions->IsUrl(ClipboardText))
  7467. {
  7468. Result = true;
  7469. }
  7470. else
  7471. {
  7472. Result = DirViewEnabled(osCurrent);
  7473. }
  7474. }
  7475. }
  7476. return Result;
  7477. }
  7478. //---------------------------------------------------------------------------
  7479. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7480. {
  7481. if (DoesClipboardContainOurFiles())
  7482. {
  7483. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  7484. {
  7485. TTerminalManager * Manager = TTerminalManager::Instance();
  7486. TTerminal * TergetTerminal = Manager->ActiveTerminal;
  7487. Manager->ActiveTerminal = FClipboardTerminal;
  7488. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TergetTerminal);
  7489. }
  7490. }
  7491. else if (CanPasteToDirViewFromClipBoard())
  7492. {
  7493. DirView(osCurrent)->PasteFromClipBoard();
  7494. }
  7495. else
  7496. {
  7497. UnicodeString ClipboardText;
  7498. if (NonEmptyTextFromClipboard(ClipboardText))
  7499. {
  7500. if (StoredSessions->IsUrl(ClipboardText))
  7501. {
  7502. NewSession(false, ClipboardText);
  7503. }
  7504. else
  7505. {
  7506. DirView(osCurrent)->Path = ClipboardText;
  7507. }
  7508. }
  7509. }
  7510. }
  7511. //---------------------------------------------------------------------------
  7512. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  7513. {
  7514. // TBD
  7515. }
  7516. //---------------------------------------------------------------------------
  7517. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  7518. {
  7519. TCustomDirView * ADirView = DirView(Side);
  7520. ADirView->SelectAll(Mode);
  7521. ADirView->SetFocus();
  7522. }
  7523. //---------------------------------------------------------------------------
  7524. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  7525. {
  7526. TCustomDirView * ADirView = DirView(Side);
  7527. TFileFilter Filter;
  7528. DefaultFileFilter(Filter);
  7529. if (DoSelectMaskDialog(ADirView, Select, &Filter, Configuration))
  7530. {
  7531. Configuration->Usage->Inc(L"MaskSelections");
  7532. ADirView->SelectFiles(Filter, Select);
  7533. ADirView->SetFocus();
  7534. }
  7535. }
  7536. //---------------------------------------------------------------------------
  7537. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  7538. {
  7539. TCustomDirView * ADirView = DirView(Side);
  7540. ADirView->RestoreSelectedNames();
  7541. ADirView->SetFocus();
  7542. }
  7543. //---------------------------------------------------------------------------
  7544. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  7545. {
  7546. TCustomDirView * CurrentDirView = DirView(osCurrent);
  7547. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  7548. {
  7549. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  7550. UnicodeString Ext;
  7551. if (GetSide(osCurrent) == osRemote)
  7552. {
  7553. Ext = UnixExtractFileExt(FileName);
  7554. }
  7555. else
  7556. {
  7557. Ext = ExtractFileExt(FileName);
  7558. }
  7559. if (Ext.IsEmpty())
  7560. {
  7561. Ext = L".";
  7562. }
  7563. TFileFilter Filter;
  7564. Filter.Masks = FORMAT(L"*%s", (Ext));
  7565. Filter.Directories = false;
  7566. CurrentDirView->SelectFiles(Filter, Select);
  7567. }
  7568. }
  7569. //---------------------------------------------------------------------------
  7570. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  7571. const TStatusFileInfo & FileInfo, TOperationSide Side)
  7572. {
  7573. UnicodeString Result;
  7574. if ((Side == osRemote) && (Terminal == NULL))
  7575. {
  7576. // noop
  7577. }
  7578. else
  7579. {
  7580. Result =
  7581. FMTLOAD(FILE_INFO_FORMAT,
  7582. (FormatBytes(FileInfo.SelectedSize),
  7583. FormatBytes(FileInfo.FilesSize),
  7584. FormatNumber(FileInfo.SelectedCount),
  7585. FormatNumber(FileInfo.FilesCount)));
  7586. }
  7587. return Result;
  7588. }
  7589. //---------------------------------------------------------------------------
  7590. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  7591. TTBXStatusPanel * Panel, TOperationSide Side)
  7592. {
  7593. if (Panel->Index == 1)
  7594. {
  7595. ToggleShowHiddenFiles();
  7596. }
  7597. else if (Panel->Index == 2)
  7598. {
  7599. Filter(Side);
  7600. }
  7601. }
  7602. //---------------------------------------------------------------------------
  7603. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  7604. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  7605. {
  7606. DebugAssert(!StatusBar->SimplePanel);
  7607. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  7608. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  7609. }
  7610. //---------------------------------------------------------------------------
  7611. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  7612. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  7613. {
  7614. DebugAssert(StatusBar->Panels->Count >= 3);
  7615. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  7616. if (FileInfo.HiddenCount > 0)
  7617. {
  7618. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  7619. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7620. }
  7621. else
  7622. {
  7623. HiddenFilesPanel->ViewPriority = 0;
  7624. // not really necessary, just to cleanup no-longer-valid data
  7625. HiddenFilesPanel->Caption = L"";
  7626. }
  7627. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  7628. if (FileInfo.FilteredCount > 0)
  7629. {
  7630. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  7631. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7632. }
  7633. else
  7634. {
  7635. FilteredFilesPanel->ViewPriority = 0;
  7636. // not really necessary, just to cleanup no-longer-valid data
  7637. FilteredFilesPanel->Caption = L"";
  7638. }
  7639. }
  7640. //---------------------------------------------------------------------------
  7641. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  7642. TObject * /*Sender*/)
  7643. {
  7644. if (RemoteDirView->Enabled)
  7645. {
  7646. RemoteDirView->SetFocus();
  7647. }
  7648. }
  7649. //---------------------------------------------------------------------------
  7650. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  7651. {
  7652. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  7653. switch (Config.Show)
  7654. {
  7655. case qvShow:
  7656. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  7657. {
  7658. Config.Show = qvHide;
  7659. }
  7660. else
  7661. {
  7662. Config.Show = Config.LastHideShow;
  7663. }
  7664. break;
  7665. case qvHideWhenEmpty:
  7666. if (ComponentVisible[fcQueueView])
  7667. {
  7668. Config.Show = qvHide;
  7669. }
  7670. else
  7671. {
  7672. Config.LastHideShow = Config.Show;
  7673. Config.Show = qvShow;
  7674. }
  7675. break;
  7676. case qvHide:
  7677. Config.LastHideShow = Config.Show;
  7678. Config.Show = qvShow;
  7679. break;
  7680. }
  7681. WinConfiguration->QueueView = Config;
  7682. }
  7683. //---------------------------------------------------------------------------
  7684. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  7685. {
  7686. UnicodeString Result;
  7687. if (FTerminal != NULL)
  7688. {
  7689. switch (WinConfiguration->PathInCaption)
  7690. {
  7691. case picShort:
  7692. Result = TTerminalManager::Instance()->GetTerminalShortPath(FTerminal);
  7693. break;
  7694. case picFull:
  7695. Result = FTerminal->CurrentDirectory;
  7696. break;
  7697. }
  7698. }
  7699. return Result;
  7700. }
  7701. //---------------------------------------------------------------------------
  7702. void __fastcall TCustomScpExplorerForm::UpdateControls()
  7703. {
  7704. TTerminalManager::Instance()->UpdateAppTitle();
  7705. // WORAKRDOUND: Disabling list view when it is not showing yet does not set its
  7706. // background to gray on Windows 7 (works on Windows 10).
  7707. // See also EnableControl
  7708. if (Showing)
  7709. {
  7710. bool HasTerminal = (Terminal != NULL) && Terminal->Active;
  7711. if (HasTerminal)
  7712. {
  7713. // Update path when it changes
  7714. if ((SessionsPageControl->ActivePage != NULL) && (GetSessionTabTerminal(SessionsPageControl->ActivePage) == Terminal))
  7715. {
  7716. UpdateSessionTab(SessionsPageControl->ActivePage);
  7717. }
  7718. if (!RemoteDirView->Enabled)
  7719. {
  7720. RemoteDirView->Enabled = true;
  7721. if (FRemoteDirViewWasFocused)
  7722. {
  7723. ActiveControl = RemoteDirView;
  7724. }
  7725. }
  7726. RemoteDriveView->Enabled = true;
  7727. RemoteDirView->Color = (FSessionColor != 0 ? FSessionColor : clWindow);
  7728. RemoteDriveView->Color = RemoteDirView->Color;
  7729. }
  7730. else
  7731. {
  7732. if (RemoteDirView->Enabled)
  7733. {
  7734. // This is first called when the form is being constructed
  7735. // (not anymore due to Showing test above)
  7736. // but the false is overriden in the constructor later.
  7737. // An even later in TScpCommanderForm::DoShow()
  7738. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  7739. EnableControl(RemoteDirView, false);
  7740. }
  7741. EnableControl(RemoteDriveView, false);
  7742. }
  7743. EnableControl(QueueView3, HasTerminal && Terminal->IsCapable[fsBackgroundTransfers]);
  7744. QueueLabelUpdateStatus();
  7745. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  7746. }
  7747. }
  7748. //---------------------------------------------------------------------------
  7749. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  7750. {
  7751. UpdateControls();
  7752. }
  7753. //---------------------------------------------------------------------------
  7754. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  7755. TObject * Sender)
  7756. {
  7757. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7758. DebugAssert(DirView != NULL);
  7759. DoDirViewLoaded(DirView);
  7760. TransferPresetAutoSelect();
  7761. }
  7762. //---------------------------------------------------------------------------
  7763. void __fastcall TCustomScpExplorerForm::StartUpdates()
  7764. {
  7765. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  7766. // first run after installation
  7767. if (double(Updates.LastCheck) == 0)
  7768. {
  7769. // make sure next time there will be an update (if enabled)
  7770. Updates.LastCheck = TDateTime(1);
  7771. WinConfiguration->Updates = Updates;
  7772. }
  7773. else if ((double(Updates.Period) > 0) &&
  7774. (Now() - Updates.LastCheck >= Updates.Period))
  7775. {
  7776. TThreadMethod OnUpdatesChecked = NULL;
  7777. if (!IsUWP())
  7778. {
  7779. OnUpdatesChecked = UpdatesChecked;
  7780. }
  7781. StartUpdateThread(OnUpdatesChecked);
  7782. }
  7783. }
  7784. //---------------------------------------------------------------------------
  7785. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  7786. {
  7787. UnicodeString Message;
  7788. bool New;
  7789. TQueryType Type;
  7790. GetUpdatesMessage(Message, New, Type, false);
  7791. if (!Message.IsEmpty())
  7792. {
  7793. if (New)
  7794. {
  7795. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  7796. }
  7797. if (!New && (Type != qtWarning))
  7798. {
  7799. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  7800. }
  7801. else
  7802. {
  7803. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  7804. }
  7805. if (New)
  7806. {
  7807. Configuration->Usage->Inc(L"UpdateNotifications");
  7808. }
  7809. }
  7810. }
  7811. //---------------------------------------------------------------------------
  7812. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  7813. {
  7814. RestoreApp();
  7815. bool CanDisplay = !NonVisualDataModule->Busy;
  7816. if (!CanDisplay && (Screen->ActiveForm != NULL))
  7817. {
  7818. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  7819. }
  7820. if (CanDisplay)
  7821. {
  7822. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  7823. CheckForUpdates(true);
  7824. }
  7825. else
  7826. {
  7827. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  7828. }
  7829. }
  7830. //---------------------------------------------------------------------------
  7831. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  7832. TCopyParamRuleData & Data)
  7833. {
  7834. DebugAssert(Terminal != NULL);
  7835. Data.HostName = Terminal->SessionData->HostNameExpanded;
  7836. Data.UserName = Terminal->SessionData->UserNameExpanded;
  7837. Data.RemoteDirectory = RemoteDirView->PathName;
  7838. }
  7839. //---------------------------------------------------------------------------
  7840. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  7841. {
  7842. // Terminal can be null when we are changing local directory implicitly,
  7843. // because it has been deleted, while no session is connected
  7844. // (Login dialog is open)
  7845. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  7846. {
  7847. TCopyParamRuleData Data;
  7848. GetTransferPresetAutoSelectData(Data);
  7849. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  7850. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  7851. if (CopyParamIndex < 0)
  7852. {
  7853. // there is no preset that matches autoselection
  7854. // set preset that we consider "default"
  7855. FCopyParamAutoSelected = L""; // forget last autoselected preset
  7856. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  7857. }
  7858. else
  7859. {
  7860. // there is preset matching autoselection
  7861. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  7862. if (CopyParamName == FCopyParamAutoSelected)
  7863. {
  7864. // autoselected the same preset as the last time
  7865. // make no change (i.e. preserve custom user preset, if any)
  7866. }
  7867. else
  7868. {
  7869. // autoselected the different preset then the last time (or there
  7870. // was default preset set)
  7871. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  7872. GUIConfiguration->CopyParamCurrent = CopyParamName;
  7873. }
  7874. }
  7875. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  7876. {
  7877. if (CopyParamIndex >= 0)
  7878. {
  7879. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  7880. }
  7881. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  7882. try
  7883. {
  7884. int Fmt =
  7885. (CopyParamIndex < 0) ?
  7886. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  7887. COPY_PARAM_AUTOSELECTED;
  7888. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  7889. Data->Message = MainInstructions(Message);
  7890. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  7891. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  7892. CopyParamAttrs);
  7893. if (CopyParamIndex >= 0)
  7894. {
  7895. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  7896. Info = FORMAT(L"%s\n \n%s", (Info,
  7897. FMTLOAD(COPY_PARAM_RULE,
  7898. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  7899. }
  7900. Data->Message += L"\n\n" + Info;
  7901. if (WinConfiguration->CopyParamAutoSelectNotice)
  7902. {
  7903. TransferPresetNoteMessage(Data, true);
  7904. }
  7905. else
  7906. {
  7907. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  7908. Data = NULL; // ownership passed
  7909. }
  7910. }
  7911. __finally
  7912. {
  7913. delete Data;
  7914. }
  7915. }
  7916. }
  7917. }
  7918. //---------------------------------------------------------------------------
  7919. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  7920. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  7921. {
  7922. DebugAssert(NoteData != NULL);
  7923. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  7924. TQueryButtonAlias Aliases[1];
  7925. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  7926. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  7927. Params.Aliases = Aliases;
  7928. Params.AliasesCount = LENOF(Aliases);
  7929. unsigned int Result =
  7930. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  7931. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  7932. switch (Result)
  7933. {
  7934. case qaNeverAskAgain:
  7935. DebugAssert(AllowNeverAskAgain);
  7936. WinConfiguration->CopyParamAutoSelectNotice = false;
  7937. break;
  7938. case qaIgnore:
  7939. PreferencesDialog(pmPresets);
  7940. break;
  7941. }
  7942. }
  7943. //---------------------------------------------------------------------------
  7944. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  7945. {
  7946. // as of now useless, as this is used for notes only, never for balloons, ...
  7947. RestoreApp();
  7948. // .. and we should never be busy here
  7949. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  7950. {
  7951. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  7952. }
  7953. }
  7954. //---------------------------------------------------------------------------
  7955. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  7956. TPreferencesMode APreferencesMode)
  7957. {
  7958. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  7959. TCopyParamRuleData Data;
  7960. if (Terminal != NULL)
  7961. {
  7962. PreferencesData.reset(new TPreferencesDialogData());
  7963. GetTransferPresetAutoSelectData(Data);
  7964. PreferencesData->CopyParamRuleData = &Data;
  7965. }
  7966. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  7967. }
  7968. //---------------------------------------------------------------------------
  7969. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  7970. const TCustomCommandType & Command)
  7971. {
  7972. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  7973. {
  7974. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (Command.Command)));
  7975. }
  7976. }
  7977. //---------------------------------------------------------------------------
  7978. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  7979. {
  7980. bool RemoteAllowed = CustomCommandRemoteAllowed();
  7981. TCustomCommandType Command;
  7982. // make sure we use local custom command when remote are not supported
  7983. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  7984. {
  7985. Command = FLastCustomCommand;
  7986. }
  7987. else
  7988. {
  7989. Command.Params = Command.Params | ccLocal;
  7990. }
  7991. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  7992. FEditingFocusedAdHocCommand = OnFocused;
  7993. bool LocalSide = (FCurrentSide == osLocal);
  7994. int Options =
  7995. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  7996. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  7997. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  7998. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  7999. {
  8000. FLastCustomCommand = Command;
  8001. UpdateCustomCommandsToolbar();
  8002. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8003. }
  8004. }
  8005. //---------------------------------------------------------------------------
  8006. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8007. {
  8008. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8009. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8010. DebugAssert(State > 0);
  8011. if (State <= 0)
  8012. {
  8013. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (FLastCustomCommand.Command)));
  8014. }
  8015. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8016. }
  8017. //---------------------------------------------------------------------------
  8018. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8019. TCustomCommandType & Command, int & State)
  8020. {
  8021. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8022. if (Result)
  8023. {
  8024. Command = FLastCustomCommand;
  8025. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8026. }
  8027. return Result;
  8028. }
  8029. //---------------------------------------------------------------------------
  8030. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8031. {
  8032. if (RemoteDirView->ItemFocused != NULL)
  8033. {
  8034. RemoteDirView->ItemFocused->CancelEdit();
  8035. }
  8036. }
  8037. //---------------------------------------------------------------------------
  8038. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8039. {
  8040. DebugAssert(ComponentVisible[Component]);
  8041. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8042. }
  8043. //---------------------------------------------------------------------------
  8044. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8045. {
  8046. // when queue panel is resized here directly, the status bar is stretched
  8047. // over whole space the panel occupied
  8048. PostComponentHide(fcQueueView);
  8049. }
  8050. //---------------------------------------------------------------------------
  8051. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8052. {
  8053. TMessage * M = static_cast<TMessage*>(Message);
  8054. switch (M->Msg)
  8055. {
  8056. case CM_DIALOGCHAR:
  8057. CMDialogChar(*M);
  8058. break;
  8059. case WM_APPCOMMAND:
  8060. WMAppCommand(*M);
  8061. break;
  8062. case WM_SYSCOMMAND:
  8063. WMSysCommand(*M);
  8064. break;
  8065. case WM_QUERYENDSESSION:
  8066. WMQueryEndSession(*M);
  8067. break;
  8068. case WM_ENDSESSION:
  8069. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8070. break;
  8071. #ifdef _DEBUG
  8072. case WM_WINDOWPOSCHANGED:
  8073. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8074. break;
  8075. case CM_CUSTOMSTYLECHANGED:
  8076. TForm::Dispatch(Message);
  8077. break;
  8078. #endif
  8079. case WM_COMPONENT_HIDE:
  8080. {
  8081. Byte Component = static_cast<Byte>(M->WParam);
  8082. // sanity check
  8083. if (ComponentVisible[Component])
  8084. {
  8085. // special treatment
  8086. if (Component == fcQueueView)
  8087. {
  8088. ToggleQueueVisibility();
  8089. DebugAssert(!ComponentVisible[fcQueueView]);
  8090. }
  8091. else
  8092. {
  8093. ComponentVisible[Component] = false;
  8094. }
  8095. }
  8096. }
  8097. break;
  8098. case WM_COPYDATA:
  8099. WMCopyData(*M);
  8100. break;
  8101. case WM_MANAGES_CAPTION:
  8102. // caption managed in TTerminalManager::UpdateAppTitle()
  8103. M->Result = 1;
  8104. break;
  8105. case WM_WANTS_MOUSEWHEEL:
  8106. M->Result = 1;
  8107. break;
  8108. case CM_SHOWINGCHANGED:
  8109. CMShowingChanged(*M);
  8110. break;
  8111. case WM_CLOSE:
  8112. WMClose(*M);
  8113. break;
  8114. case WM_DPICHANGED:
  8115. WMDpiChanged(*M);
  8116. break;
  8117. case CM_DPICHANGED:
  8118. CMDpiChanged(*M);
  8119. break;
  8120. default:
  8121. TForm::Dispatch(Message);
  8122. break;
  8123. }
  8124. }
  8125. //---------------------------------------------------------------------------
  8126. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8127. {
  8128. // noop
  8129. }
  8130. //---------------------------------------------------------------------------
  8131. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8132. {
  8133. TForm::Dispatch(&Message);
  8134. FSessionColors->Clear();
  8135. AddFixedSessionImages();
  8136. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8137. UpdateImages();
  8138. }
  8139. //---------------------------------------------------------------------------
  8140. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8141. {
  8142. TForm::Dispatch(&Message);
  8143. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8144. UpdatePixelsPerInchMainWindowCounter();
  8145. }
  8146. //---------------------------------------------------------------------------
  8147. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8148. {
  8149. // Cannot close window while we are busy.
  8150. // We cannot test this in FormCloseQuery as that is called also from
  8151. // Close(), which is called by CloseApplicationAction. So we can be busy
  8152. // there already even, when it is legitimate to close the application.
  8153. // Possibly a better place to handle this would be WMSysCommand.
  8154. if (NonVisualDataModule->Busy)
  8155. {
  8156. Message.Result = 1;
  8157. }
  8158. else
  8159. {
  8160. TForm::Dispatch(&Message);
  8161. }
  8162. }
  8163. //---------------------------------------------------------------------------
  8164. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8165. {
  8166. TForm::Dispatch(&Message);
  8167. if (Showing && (Terminal == NULL))
  8168. {
  8169. // When we are starting minimized (i.e. from an installer),
  8170. // postpone showing Login dialog until we get restored.
  8171. // Otherwise the Login dialog (and Authentication window) show restored
  8172. // over invidible (minimized) main window.
  8173. if (WindowState == wsMinimized)
  8174. {
  8175. FNeedSession = true;
  8176. }
  8177. else
  8178. {
  8179. // This happens before application ever goes idle, so the toolbars would
  8180. // stay enabled (initial state) until the Login dialog is dismissed.
  8181. UpdateActions();
  8182. NonVisualDataModule->StartBusy();
  8183. try
  8184. {
  8185. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8186. // otherwise the dialog does not receive focus.
  8187. // With Commander interface the ProcessMessages is called already
  8188. // by TDriveView, but with Explorer interface, we need to call it explicily
  8189. Application->ProcessMessages();
  8190. // do not reload sessions, they have been loaded just now (optimization)
  8191. NeedSession(false);
  8192. }
  8193. __finally
  8194. {
  8195. NonVisualDataModule->EndBusy();
  8196. }
  8197. }
  8198. }
  8199. }
  8200. //---------------------------------------------------------------------------
  8201. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8202. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8203. int & MaxHeight)
  8204. {
  8205. // workaround for bug in TWinControl.CalcConstraints
  8206. // Check for empty rect (restore from iconic state) is done there only after
  8207. // call to AdjustClientRect, which enlarges the rect (for forms).
  8208. TRect R = GetClientRect();
  8209. // when restoring from iconic state, place no restrictions
  8210. if (IsRectEmpty(R))
  8211. {
  8212. MinWidth = 0;
  8213. MinHeight = 0;
  8214. MaxWidth = 32000;
  8215. MaxHeight = 32000;
  8216. }
  8217. }
  8218. //---------------------------------------------------------------------------
  8219. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8220. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8221. {
  8222. // terminal can be already closed (e.g. dropped connection)
  8223. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8224. {
  8225. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8226. try
  8227. {
  8228. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8229. }
  8230. catch(...)
  8231. {
  8232. if (!Terminal->Active)
  8233. {
  8234. Close = true;
  8235. }
  8236. throw;
  8237. }
  8238. }
  8239. }
  8240. //---------------------------------------------------------------------------
  8241. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8242. {
  8243. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8244. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8245. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8246. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8247. {
  8248. OnGetSpaceAvailable = GetSpaceAvailable;
  8249. }
  8250. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8251. OnGetSpaceAvailable);
  8252. }
  8253. //---------------------------------------------------------------------------
  8254. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8255. {
  8256. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8257. Terminal->FillSessionDataForCode(Data.get());
  8258. return Data.release();
  8259. }
  8260. //---------------------------------------------------------------------------
  8261. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8262. {
  8263. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8264. DoGenerateUrlDialog(Data.get(), Paths);
  8265. }
  8266. //---------------------------------------------------------------------------
  8267. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8268. {
  8269. GenerateUrl(NULL);
  8270. }
  8271. //---------------------------------------------------------------------------
  8272. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8273. {
  8274. std::unique_ptr<TStrings> Paths(new TStringList());
  8275. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8276. GenerateUrl(Paths.get());
  8277. }
  8278. //---------------------------------------------------------------------------
  8279. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8280. {
  8281. if (value != FSessionColor)
  8282. {
  8283. FSessionColor = value;
  8284. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8285. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8286. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8287. DebugAssert(ColorItem != NULL);
  8288. ColorItem->Color = C;
  8289. NonVisualDataModule->ColorMenuItem->Color = C;
  8290. // Is null when called from LastTerminalClosed
  8291. if (Terminal != NULL)
  8292. {
  8293. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8294. }
  8295. UpdateControls();
  8296. }
  8297. }
  8298. //---------------------------------------------------------------------------
  8299. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8300. {
  8301. bool Result = FNoteTimer->Enabled;
  8302. if (Result)
  8303. {
  8304. // cannot cancel note too early
  8305. bool NotEarly =
  8306. (Now() - FNoteShown >
  8307. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8308. if (Force || NotEarly)
  8309. {
  8310. FNoteTimer->Enabled = false;
  8311. FNote = L"";
  8312. SAFE_DESTROY(FNoteData);
  8313. FOnNoteClick = NULL;
  8314. FNoteHints = FNotes->Text;
  8315. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8316. UpdateStatusBar();
  8317. }
  8318. }
  8319. return Result;
  8320. }
  8321. //---------------------------------------------------------------------------
  8322. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8323. {
  8324. DebugAssert(FNoteTimer->Enabled);
  8325. CancelNote(true);
  8326. }
  8327. //---------------------------------------------------------------------------
  8328. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8329. {
  8330. int P = Note.Pos(L"\n");
  8331. if (P > 0)
  8332. {
  8333. Note.SetLength(P - 1);
  8334. }
  8335. FNotes->Add(FORMAT(L"[%s] %s",
  8336. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8337. while (FNotes->Count > 10)
  8338. {
  8339. FNotes->Delete(0);
  8340. }
  8341. if (UpdateNow)
  8342. {
  8343. FNoteHints = FNotes->Text;
  8344. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8345. UpdateStatusBar();
  8346. }
  8347. }
  8348. //---------------------------------------------------------------------------
  8349. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8350. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8351. {
  8352. int P = Note.Pos(L"\n");
  8353. if (P > 0)
  8354. {
  8355. Note.SetLength(P - 1);
  8356. }
  8357. FNoteHints = FNotes->Text;
  8358. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8359. FNote = Note;
  8360. SAFE_DESTROY(FNoteData);
  8361. FNoteData = NoteData;
  8362. FOnNoteClick = OnNoteClick;
  8363. AddNote(Note, false);
  8364. UpdateStatusBar();
  8365. FNoteShown = Now();
  8366. FNoteTimer->Enabled = false;
  8367. if (Seconds == 0)
  8368. {
  8369. Seconds = WinConfiguration->NotificationsTimeout;
  8370. }
  8371. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8372. FNoteTimer->Enabled = true;
  8373. }
  8374. //---------------------------------------------------------------------------
  8375. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8376. {
  8377. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8378. }
  8379. //---------------------------------------------------------------------------
  8380. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8381. {
  8382. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  8383. {
  8384. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8385. }
  8386. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  8387. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  8388. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  8389. 0, NULL, NULL);
  8390. }
  8391. //---------------------------------------------------------------------------
  8392. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  8393. {
  8394. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  8395. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  8396. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  8397. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  8398. }
  8399. //---------------------------------------------------------------------------
  8400. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  8401. {
  8402. WinConfiguration->FormatSizeBytes = Style;
  8403. }
  8404. //---------------------------------------------------------------------------
  8405. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  8406. {
  8407. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  8408. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  8409. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  8410. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  8411. }
  8412. //---------------------------------------------------------------------------
  8413. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  8414. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  8415. {
  8416. if (Panel->Index == 0)
  8417. {
  8418. if (FOnNoteClick != NULL)
  8419. {
  8420. // prevent the user data from being freed by possible call
  8421. // to CancelNote or PostNote during call to OnNoteClick
  8422. std::unique_ptr<TObject> NoteData(FNoteData);
  8423. TNotifyEvent OnNoteClick = FOnNoteClick;
  8424. FNoteData = NULL;
  8425. // need to cancel the note as we are going to delete its user data
  8426. CancelNote(true);
  8427. OnNoteClick(NoteData.get());
  8428. }
  8429. }
  8430. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  8431. {
  8432. FileSystemInfo();
  8433. }
  8434. }
  8435. //---------------------------------------------------------------------------
  8436. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  8437. {
  8438. // workaround:
  8439. // 1) for unknown reason, disabling window, while minimized,
  8440. // prevents it from restoring, even if it was enabled again meanwhile
  8441. // 2) when disabling the main window, while another has focus
  8442. // minimize is no longer possible
  8443. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  8444. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  8445. // Shouldn't we use IsApplicationMinimized() here?
  8446. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  8447. {
  8448. Enabled = false;
  8449. }
  8450. FLockLevel++;
  8451. }
  8452. //---------------------------------------------------------------------------
  8453. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  8454. {
  8455. DebugAssert(FLockLevel > 0);
  8456. FLockLevel--;
  8457. if (FLockLevel == 0)
  8458. {
  8459. DebugAssert(FLockSuspendLevel == 0);
  8460. Enabled = true;
  8461. }
  8462. }
  8463. //---------------------------------------------------------------------------
  8464. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  8465. {
  8466. // We need to make sure that window is enabled when the last modal window closes
  8467. // otherwise focus is not restored correctly.
  8468. // So we re-enable the window when modal window opens and
  8469. // disable it back after is closes
  8470. if (FLockLevel > 0)
  8471. {
  8472. // while we have nesting counter, we know that we never be called
  8473. // recursivelly as Application->OnModalBegin is called only
  8474. // for the top-level modal window
  8475. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8476. {
  8477. // won't be disabled when conditions in LockWindow() were not satisfied
  8478. FDisabledOnLockSuspend = !Enabled;
  8479. // When minimized to tray (or actually when set to SW_HIDE),
  8480. // setting Enabled makes the window focusable even when there's
  8481. // modal window over it
  8482. if (!FTrayIcon->Visible)
  8483. {
  8484. Enabled = true;
  8485. }
  8486. }
  8487. FLockSuspendLevel++;
  8488. }
  8489. }
  8490. //---------------------------------------------------------------------------
  8491. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  8492. {
  8493. if (FLockSuspendLevel > 0)
  8494. {
  8495. DebugAssert(FLockLevel > 0);
  8496. FLockSuspendLevel--;
  8497. // see comment in SuspendWindowLock
  8498. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8499. {
  8500. // Note that window can be enabled here, when we were minized to tray when
  8501. // was SuspendWindowLock() called.
  8502. // We should possibly do the same check as in LockWindow(),
  8503. // if it is ever possible that the consitions change between
  8504. // SuspendWindowLock() and ResumeWindowLock()
  8505. Enabled = !FDisabledOnLockSuspend;
  8506. }
  8507. }
  8508. }
  8509. //---------------------------------------------------------------------------
  8510. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  8511. {
  8512. if (!TextOnly)
  8513. {
  8514. TTBXComboBoxItem * RemotePathComboBox =
  8515. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  8516. TStrings * Items = RemotePathComboBox->Strings;
  8517. Items->BeginUpdate();
  8518. try
  8519. {
  8520. Items->Clear();
  8521. if (Terminal != NULL)
  8522. {
  8523. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8524. while (!IsUnixRootPath(APath))
  8525. {
  8526. int P = APath.LastDelimiter(L'/');
  8527. DebugAssert(P >= 0);
  8528. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  8529. APath.SetLength(P - 1);
  8530. }
  8531. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  8532. }
  8533. }
  8534. __finally
  8535. {
  8536. RemotePathComboBox->ItemIndex = Items->Count - 1;
  8537. // Setting ItemIndex to -1 does not reset its text
  8538. if (Items->Count == 0)
  8539. {
  8540. RemotePathComboBox->Text = L"";
  8541. }
  8542. Items->EndUpdate();
  8543. }
  8544. }
  8545. }
  8546. //---------------------------------------------------------------------------
  8547. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  8548. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  8549. int & ImageIndex)
  8550. {
  8551. if (AIndex < 0)
  8552. {
  8553. AIndex = Sender->ItemIndex;
  8554. }
  8555. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  8556. }
  8557. //---------------------------------------------------------------------------
  8558. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  8559. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  8560. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  8561. {
  8562. ARect.Left += (10 * AIndex);
  8563. }
  8564. //---------------------------------------------------------------------------
  8565. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  8566. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  8567. {
  8568. AWidth += (10 * AIndex);
  8569. }
  8570. //---------------------------------------------------------------------------
  8571. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  8572. TObject * Sender)
  8573. {
  8574. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  8575. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8576. int Index = RemotePathComboBox->ItemIndex;
  8577. while (Index < RemotePathComboBox->Strings->Count - 1)
  8578. {
  8579. APath = UnixExtractFileDir(APath);
  8580. Index++;
  8581. }
  8582. // VanDyke style paths
  8583. if (APath.IsEmpty())
  8584. {
  8585. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  8586. APath = ROOTDIRECTORY;
  8587. }
  8588. if (RemoteDirView->Path != APath)
  8589. {
  8590. RemoteDirView->Path = APath;
  8591. }
  8592. }
  8593. //---------------------------------------------------------------------------
  8594. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  8595. {
  8596. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  8597. DebugUsedParam(Sender);
  8598. UpdateRemotePathComboBox(true);
  8599. }
  8600. //---------------------------------------------------------------------------
  8601. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  8602. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  8603. {
  8604. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8605. DebugAssert(DirView != NULL);
  8606. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  8607. {
  8608. HWND Edit = ListView_GetEditControl(DirView->Handle);
  8609. // OnEditing is called also from TCustomListView::CanEdit
  8610. if (Edit != NULL)
  8611. {
  8612. EditSelectBaseName(Edit);
  8613. }
  8614. }
  8615. }
  8616. //---------------------------------------------------------------------------
  8617. //---------------------------------------------------------------------------
  8618. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  8619. {
  8620. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  8621. Result->AutoDetectDnD = false;
  8622. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  8623. Result->RenderDataOn = rdoEnterAndDropSync;
  8624. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  8625. return Result;
  8626. }
  8627. //---------------------------------------------------------------------------
  8628. void __fastcall TCustomScpExplorerForm::CreateWnd()
  8629. {
  8630. TForm::CreateWnd();
  8631. if (FSessionsDragDropFilesEx == NULL)
  8632. {
  8633. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  8634. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  8635. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  8636. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  8637. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  8638. }
  8639. if (FQueueDragDropFilesEx == NULL)
  8640. {
  8641. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  8642. // No need to set OnDragOver as we do not have any restrictions
  8643. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  8644. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  8645. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  8646. }
  8647. }
  8648. //---------------------------------------------------------------------------
  8649. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  8650. {
  8651. TForm::DestroyWnd();
  8652. FSessionsDragDropFilesEx->DragDropControl = NULL;
  8653. FQueueDragDropFilesEx->DragDropControl = NULL;
  8654. }
  8655. //---------------------------------------------------------------------------
  8656. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  8657. {
  8658. SideEnter(FCurrentSide);
  8659. FEverShown = true;
  8660. }
  8661. //---------------------------------------------------------------------------
  8662. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  8663. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  8664. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  8665. {
  8666. if (!NonVisualDataModule->Busy)
  8667. {
  8668. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8669. Configuration->Usage->Inc(L"FileFinds");
  8670. LockWindow(true);
  8671. NonVisualDataModule->StartBusy();
  8672. WinConfiguration->LockedInterface = true;
  8673. try
  8674. {
  8675. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  8676. }
  8677. __finally
  8678. {
  8679. WinConfiguration->LockedInterface = false;
  8680. NonVisualDataModule->EndBusy();
  8681. UnlockWindow();
  8682. }
  8683. }
  8684. }
  8685. //---------------------------------------------------------------------------
  8686. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  8687. {
  8688. if (!NonVisualDataModule->Busy)
  8689. {
  8690. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8691. SetFocus();
  8692. RemoteDirView->Path = UnixExtractFilePath(Path);
  8693. TListItem * Item = RemoteDirView->FindFileItem(UnixExtractFileName(Path));
  8694. if (Item != NULL)
  8695. {
  8696. RemoteDirView->ItemFocused = Item;
  8697. RemoteDirView->ItemFocused->MakeVisible(false);
  8698. RemoteDirView->SetFocus();
  8699. }
  8700. }
  8701. }
  8702. //---------------------------------------------------------------------------
  8703. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  8704. {
  8705. bool Result = !NonVisualDataModule->Busy;
  8706. if (Result)
  8707. {
  8708. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8709. }
  8710. return Result;
  8711. }
  8712. //---------------------------------------------------------------------------
  8713. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  8714. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8715. {
  8716. if (CanOperateOnFoundFiles(ATerminal))
  8717. {
  8718. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  8719. FOnFileOperationFinished = OnFileOperationFinished;
  8720. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  8721. }
  8722. }
  8723. //---------------------------------------------------------------------------
  8724. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  8725. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8726. {
  8727. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  8728. }
  8729. //---------------------------------------------------------------------------
  8730. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  8731. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8732. {
  8733. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  8734. }
  8735. //---------------------------------------------------------------------------
  8736. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  8737. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8738. {
  8739. if (CanOperateOnFoundFiles(ATerminal))
  8740. {
  8741. for (int Index = 0; Index < FileList->Count; Index++)
  8742. {
  8743. UnicodeString FileName = FileList->Strings[Index];
  8744. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  8745. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  8746. OnFileOperationFinished(FileName, true);
  8747. }
  8748. }
  8749. }
  8750. //---------------------------------------------------------------------------
  8751. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  8752. {
  8753. FFileFindTerminal = Terminal;
  8754. ShowFileFindDialog(
  8755. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  8756. DoDownloadFoundFiles, DoEditFoundFiles);
  8757. }
  8758. //---------------------------------------------------------------------------
  8759. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  8760. {
  8761. FTaskbarList = TaskbarList;
  8762. }
  8763. //---------------------------------------------------------------------------
  8764. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  8765. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  8766. {
  8767. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8768. if (Index >= 0)
  8769. {
  8770. if (Button == mbLeft)
  8771. {
  8772. // "Mouse down" is raised only after tab is switched.
  8773. // If switching tab (switching session) takes long enough for user
  8774. // to actually release the button, "mouse down" is still raised,
  8775. // but we do not get "mouse up" event, so dragging is not cancelled,
  8776. // prevent that by not beginning dragging in the first place.
  8777. if (IsKeyPressed(VK_LBUTTON))
  8778. {
  8779. // when user clicks the "+", we get mouse down only after the session
  8780. // is closed, when new session tab is already on X:Y, so dragging
  8781. // starts, prevent that
  8782. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  8783. {
  8784. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8785. if (Terminal != NULL)
  8786. {
  8787. SessionsPageControl->BeginDrag(false);
  8788. }
  8789. }
  8790. }
  8791. }
  8792. else if (Button == mbMiddle)
  8793. {
  8794. // ignore middle-click for "New session tab"
  8795. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8796. if (Terminal != NULL)
  8797. {
  8798. CloseSessionTab(Index);
  8799. }
  8800. }
  8801. }
  8802. }
  8803. //---------------------------------------------------------------------------
  8804. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  8805. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  8806. {
  8807. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8808. // do not allow dropping on the "+" tab
  8809. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8810. if ((TargetTerminal != NULL) &&
  8811. (SessionsPageControl->ActivePage->PageIndex != Index))
  8812. {
  8813. Configuration->Usage->Inc(L"SessionTabMoves");
  8814. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  8815. // but we want to actually prevent that to avoid flicker
  8816. SessionsPageControl->ActivePage->PageIndex = Index;
  8817. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  8818. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  8819. }
  8820. }
  8821. //---------------------------------------------------------------------------
  8822. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  8823. TObject * Sender, TObject * Source, int X, int Y,
  8824. TDragState /*State*/, bool & Accept)
  8825. {
  8826. Accept = (Sender == Source);
  8827. if (Accept)
  8828. {
  8829. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8830. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8831. // do not allow dragging to the "+" tab
  8832. Accept = (Terminal != NULL);
  8833. }
  8834. }
  8835. //---------------------------------------------------------------------------
  8836. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(int /*KeyState*/,
  8837. const TPoint & Point, int & Effect)
  8838. {
  8839. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  8840. if (Index < 0)
  8841. {
  8842. Effect = DROPEFFECT_None;
  8843. }
  8844. else
  8845. {
  8846. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8847. // do not allow dropping on the "+" tab
  8848. if (TargetTerminal == NULL)
  8849. {
  8850. Effect = DROPEFFECT_None;
  8851. }
  8852. else if ((TargetTerminal != Terminal) && (Effect == DROPEFFECT_Move))
  8853. {
  8854. Effect = DROPEFFECT_Copy;
  8855. }
  8856. }
  8857. }
  8858. //---------------------------------------------------------------------------
  8859. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  8860. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  8861. {
  8862. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  8863. // do not allow dropping on the "+" tab
  8864. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8865. if (TargetTerminal != NULL)
  8866. {
  8867. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  8868. if (!IsFileControl(DropSourceControl, osRemote))
  8869. {
  8870. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  8871. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  8872. // Why don't we use Terminal->CurrentDirectory directly?
  8873. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  8874. // do not force queue, drag drop
  8875. false, true);
  8876. }
  8877. }
  8878. }
  8879. //---------------------------------------------------------------------------
  8880. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  8881. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  8882. {
  8883. // Downloads are handled in RemoteFileControlDDEnd
  8884. if (!IsFileControl(DropSourceControl, osRemote))
  8885. {
  8886. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  8887. Terminal->CurrentDirectory,
  8888. // force queue, drag drop
  8889. true, true);
  8890. }
  8891. }
  8892. //---------------------------------------------------------------------------
  8893. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  8894. {
  8895. FShowing = false;
  8896. // Do not save empty workspace
  8897. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  8898. {
  8899. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  8900. UnicodeString Name = WorkspaceName();
  8901. DoSaveWorkspace(
  8902. Name, DataList.get(),
  8903. !Configuration->DisablePasswordStoring &&
  8904. WinConfiguration->AutoSaveWorkspacePasswords, false);
  8905. WinConfiguration->LastStoredSession = Name;
  8906. }
  8907. }
  8908. //---------------------------------------------------------------------------
  8909. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  8910. {
  8911. TManagedTerminal * ManagedTerminal =
  8912. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  8913. if (ManagedTerminal != NULL)
  8914. {
  8915. ManagedTerminal->DirectoryLoaded = Now();
  8916. }
  8917. }
  8918. //---------------------------------------------------------------------------
  8919. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  8920. TListItem * /*Item*/, bool /*Selected*/)
  8921. {
  8922. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8923. switch (DirView->LastSelectMethod)
  8924. {
  8925. case smKeyboard:
  8926. Configuration->Usage->Inc(L"KeyboardSelections");
  8927. break;
  8928. case smMouse:
  8929. Configuration->Usage->Inc(L"MouseSelections");
  8930. break;
  8931. }
  8932. }
  8933. //---------------------------------------------------------------------------
  8934. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  8935. {
  8936. if (Color != 0)
  8937. {
  8938. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  8939. }
  8940. else
  8941. {
  8942. return FSessionTabImageIndex;
  8943. }
  8944. }
  8945. //---------------------------------------------------------------------------
  8946. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  8947. {
  8948. TPngImageCollectionItem * Item =
  8949. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  8950. FSessionColors->AddPng(Item->PngImage);
  8951. return FSessionColors->Count - 1;
  8952. }
  8953. //---------------------------------------------------------------------------
  8954. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  8955. {
  8956. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  8957. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  8958. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  8959. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  8960. }
  8961. //---------------------------------------------------------------------------
  8962. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  8963. {
  8964. for (int Index = 0; Index < Control->ControlCount; Index++)
  8965. {
  8966. TControl * AControl = Control->Controls[Index];
  8967. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  8968. if (WinControl != NULL)
  8969. {
  8970. CollectItemsWithTextDisplayMode(WinControl);
  8971. }
  8972. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  8973. if (Toolbar != NULL)
  8974. {
  8975. // we care for top-level items only
  8976. TTBCustomItem * Items = Toolbar->Items;
  8977. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  8978. {
  8979. TTBCustomItem * Item = Items->Items[ItemIndex];
  8980. if (((Item->DisplayMode == nbdmImageAndText) ||
  8981. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  8982. EligibleForImageDisplayMode(Item))
  8983. {
  8984. FItemsWithTextDisplayMode.insert(Item);
  8985. }
  8986. }
  8987. }
  8988. }
  8989. }
  8990. //---------------------------------------------------------------------------
  8991. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  8992. {
  8993. return true;
  8994. }
  8995. //---------------------------------------------------------------------------
  8996. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  8997. {
  8998. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  8999. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9000. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9001. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9002. bool Result = true;
  9003. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9004. {
  9005. TTBCustomItem * Item = *i;
  9006. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9007. if (Label != NULL)
  9008. {
  9009. // optimization
  9010. if (Label->Visible == SelectiveToolbarText)
  9011. {
  9012. Result = false;
  9013. }
  9014. else
  9015. {
  9016. Label->Visible = SelectiveToolbarText;
  9017. }
  9018. }
  9019. else
  9020. {
  9021. // optimization
  9022. if (Item->DisplayMode == DisplayMode)
  9023. {
  9024. Result = false;
  9025. }
  9026. else
  9027. {
  9028. Item->DisplayMode = DisplayMode;
  9029. }
  9030. }
  9031. ++i;
  9032. }
  9033. if (Result)
  9034. {
  9035. UpdateNewSessionTab();
  9036. }
  9037. return Result;
  9038. }
  9039. //---------------------------------------------------------------------------
  9040. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9041. {
  9042. DebugFail();
  9043. }
  9044. //---------------------------------------------------------------------------
  9045. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9046. {
  9047. // Among other, a lock level is non zero, while directory is being loaded,
  9048. // even when it happens as a result of dirview navigation,
  9049. // i.e. when TNonVisualDataModule is NOT FBusy.
  9050. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9051. // Among other this prevents a panel auto update to occur while
  9052. // directory is changing.
  9053. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9054. }
  9055. //---------------------------------------------------------------------------
  9056. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  9057. {
  9058. // While the window is disabled, we still seem to process menu shortcuts at least,
  9059. // so stop it at least here.
  9060. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9061. return
  9062. (Allowed == aaUpdate) ||
  9063. !NonVisualDataModule->Busy;
  9064. }
  9065. //---------------------------------------------------------------------------
  9066. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9067. {
  9068. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9069. }
  9070. //---------------------------------------------------------------------------
  9071. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9072. {
  9073. // This is somewhat redundant to LockWindow() call from
  9074. // TTerminalManager::TerminalReadDirectoryProgress.
  9075. // But disabling window is known not to block keyboard shorcuts
  9076. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9077. if (Busy > 0)
  9078. {
  9079. if (NonVisualDataModule->Busy)
  9080. {
  9081. State = false;
  9082. }
  9083. else
  9084. {
  9085. NonVisualDataModule->StartBusy();
  9086. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9087. LockWindow();
  9088. }
  9089. }
  9090. else if (Busy < 0)
  9091. {
  9092. UnlockWindow();
  9093. NonVisualDataModule->EndBusy();
  9094. }
  9095. else
  9096. {
  9097. State = NonVisualDataModule->Busy;
  9098. }
  9099. }
  9100. //---------------------------------------------------------------------------
  9101. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9102. {
  9103. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9104. // no context menu for "New session tab"
  9105. if ((Index >= 0) && (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != NULL))
  9106. {
  9107. SessionsPageControl->ActivePageIndex = Index;
  9108. if (DebugAlwaysTrue(SessionTabSwitched()))
  9109. {
  9110. // copied from TControl.WMContextMenu
  9111. SendCancelMode(SessionsPageControl);
  9112. // explicit popup instead of using PopupMenu property
  9113. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9114. // while connecting yet not-connected session and hence
  9115. // allowing an access to commands over not-completelly connected session
  9116. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9117. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  9118. PopupMenu->PopupComponent = SessionsPageControl;
  9119. PopupMenu->Popup(Point.x, Point.y);
  9120. }
  9121. }
  9122. Handled = true;
  9123. }
  9124. //---------------------------------------------------------------------------
  9125. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9126. {
  9127. return (Terminal != NULL) && Terminal->Active && Terminal->IsCapable[fcChangePassword];
  9128. }
  9129. //---------------------------------------------------------------------------
  9130. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9131. {
  9132. Configuration->Usage->Inc(L"PasswordChanges");
  9133. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9134. ChangePasswordData->DisableAuthentationsExceptPassword();
  9135. ChangePasswordData->ChangePassword = true;
  9136. std::unique_ptr<TTerminal> ChangePasswordSession(
  9137. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9138. ChangePasswordSession->AutoReadDirectory = false;
  9139. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9140. {
  9141. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9142. }
  9143. else
  9144. {
  9145. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9146. }
  9147. }
  9148. //---------------------------------------------------------------------------
  9149. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9150. {
  9151. // No nice way to assert SSH2
  9152. return (Terminal != NULL) && Terminal->Active && (Terminal->FSProtocol == cfsSFTP);
  9153. }
  9154. //---------------------------------------------------------------------------
  9155. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9156. {
  9157. TOperationVisualizer OperationVisualizer;
  9158. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9159. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9160. }
  9161. //---------------------------------------------------------------------------
  9162. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9163. {
  9164. TForm::ChangeScale(M, D);
  9165. int APixelsPerInch = GetControlPixelsPerInch(this);
  9166. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9167. try
  9168. {
  9169. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9170. }
  9171. __finally
  9172. {
  9173. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9174. }
  9175. }
  9176. //---------------------------------------------------------------------------
  9177. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9178. {
  9179. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9180. }
  9181. //---------------------------------------------------------------------
  9182. class TPublicControl : public TControl
  9183. {
  9184. friend class TCustomScpExplorerForm;
  9185. };
  9186. //---------------------------------------------------------------------------
  9187. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9188. {
  9189. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9190. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9191. }
  9192. //---------------------------------------------------------------------------
  9193. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9194. {
  9195. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9196. OpenBookmark(Side, Bookmark);
  9197. }
  9198. //---------------------------------------------------------------------------
  9199. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9200. {
  9201. DoBookmarkClick(osLocal, Sender);
  9202. }
  9203. //---------------------------------------------------------------------------
  9204. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9205. {
  9206. DoBookmarkClick(osRemote, Sender);
  9207. }
  9208. //---------------------------------------------------------------------------
  9209. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9210. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9211. {
  9212. if (BookmarkList != NULL)
  9213. {
  9214. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9215. if (!WinConfiguration->UseLocationProfiles)
  9216. {
  9217. std::unique_ptr<TStringList> Directories(new TStringList());
  9218. Directories->CaseSensitive = (Side == osRemote);
  9219. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9220. {
  9221. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9222. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9223. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9224. {
  9225. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9226. Item->Caption = EscapeHotkey(Directory);
  9227. Item->ShortCut = Bookmark->ShortCut;
  9228. Item->OnClick = OnBookmarkClick;
  9229. Item->Tag = reinterpret_cast<int>(Bookmark);
  9230. Directories->Add(Directory);
  9231. Menu->Add(Item.release());
  9232. }
  9233. }
  9234. }
  9235. else
  9236. {
  9237. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9238. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9239. {
  9240. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9241. if (!Bookmark->Node.IsEmpty())
  9242. {
  9243. Folders->Add(Bookmark->Node);
  9244. }
  9245. }
  9246. for (int Index = 0; Index < Folders->Count; Index++)
  9247. {
  9248. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9249. Item->Caption = Folders->Strings[Index];
  9250. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction->ImageIndex;
  9251. Folders->Objects[Index] = Item.get();
  9252. Menu->Add(Item.release());
  9253. }
  9254. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9255. {
  9256. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9257. TTBCustomItem * Parent;
  9258. if (!Bookmark->Node.IsEmpty())
  9259. {
  9260. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9261. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9262. }
  9263. else
  9264. {
  9265. Parent = Menu;
  9266. }
  9267. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9268. Item->Caption = Bookmark->Name;
  9269. Item->ShortCut = Bookmark->ShortCut;
  9270. Item->OnClick = OnBookmarkClick;
  9271. Item->Tag = reinterpret_cast<int>(Bookmark);
  9272. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9273. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9274. {
  9275. // No hint for location profiles that are actually mere "bookmarks"
  9276. }
  9277. else
  9278. {
  9279. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  9280. UnicodeString LongHint;
  9281. if (!Bookmark->Local.IsEmpty())
  9282. {
  9283. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  9284. }
  9285. if (!Bookmark->Remote.IsEmpty())
  9286. {
  9287. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  9288. }
  9289. AddToList(Hint, LongHint, L"|");
  9290. Item->Hint = Hint;
  9291. }
  9292. Parent->Add(Item.release());
  9293. }
  9294. }
  9295. AddMenuSeparator(Menu);
  9296. }
  9297. }
  9298. //---------------------------------------------------------------------------
  9299. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  9300. {
  9301. Menu->Clear();
  9302. std::unique_ptr<TTBCustomItem> Item;
  9303. Item.reset(new TTBXItem(Owner));
  9304. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  9305. Item->Options = Item->Options << tboDefault;
  9306. Menu->Add(Item.release());
  9307. AddMenuSeparator(Menu);
  9308. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  9309. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  9310. Item.reset(new TTBXItem(Owner));
  9311. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction : NonVisualDataModule->RemoteAddBookmarkAction;
  9312. Menu->Add(Item.release());
  9313. }
  9314. //---------------------------------------------------------------------------
  9315. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  9316. {
  9317. bool Result = true;
  9318. try
  9319. {
  9320. if (Side == osRemote)
  9321. {
  9322. Terminal->ExceptionOnFail = true;
  9323. }
  9324. try
  9325. {
  9326. DirView(Side)->Path = Path;
  9327. }
  9328. __finally
  9329. {
  9330. if (Side == osRemote)
  9331. {
  9332. Terminal->ExceptionOnFail = false;
  9333. }
  9334. }
  9335. }
  9336. catch (Exception & E)
  9337. {
  9338. if ((Side != osRemote) || Terminal->Active)
  9339. {
  9340. ShowExtendedExceptionEx(Terminal, &E);
  9341. Result = false;
  9342. }
  9343. else
  9344. {
  9345. throw;
  9346. }
  9347. }
  9348. return Result;
  9349. }
  9350. //---------------------------------------------------------------------------
  9351. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  9352. {
  9353. // Make sure there some feedback even when loading tine local folders
  9354. TInstantOperationVisualizer Visualizer;
  9355. DirView(Side)->ReloadDirectory();
  9356. }
  9357. //---------------------------------------------------------------------------
  9358. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  9359. {
  9360. if (Index == SessionsPageControl->TabIndex)
  9361. {
  9362. CloseSession();
  9363. }
  9364. else
  9365. {
  9366. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  9367. TTerminal * Terminal = GetSessionTabTerminal(TabSheet);
  9368. TTerminalManager * Manager = TTerminalManager::Instance();
  9369. if (!Terminal->Active)
  9370. {
  9371. Manager->FreeTerminal(Terminal);
  9372. }
  9373. else
  9374. {
  9375. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  9376. if (CanCloseQueue(Queue))
  9377. {
  9378. Manager->FreeTerminal(Terminal);
  9379. }
  9380. }
  9381. }
  9382. }
  9383. //---------------------------------------------------------------------------
  9384. void __fastcall TCustomScpExplorerForm::SessionsPageControlCloseButtonClick(TPageControl * /*Sender*/, int Index)
  9385. {
  9386. CloseSessionTab(Index);
  9387. }
  9388. //---------------------------------------------------------------------------
  9389. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side)
  9390. {
  9391. if (DebugAlwaysTrue(GetSide(Side) == osRemote))
  9392. {
  9393. TInstantOperationVisualizer Visualizer;
  9394. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  9395. // before we create a new data object
  9396. ClipboardClear();
  9397. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  9398. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  9399. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  9400. FClipboardFakeMonitors.reset(StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated));
  9401. if (FClipboardDragDropFilesEx.get() == NULL)
  9402. {
  9403. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  9404. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  9405. }
  9406. FClipboardDragDropFilesEx->FileList->Clear();
  9407. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  9408. FClipboardDragDropFilesEx->CopyToClipboard();
  9409. FClipboardTerminal = FTerminal;
  9410. // Need full paths, as cwd can be different once files are pasted
  9411. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(false, true)));
  9412. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  9413. {
  9414. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  9415. }
  9416. }
  9417. }
  9418. //---------------------------------------------------------------------------
  9419. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  9420. {
  9421. if (OpenClipboard(0))
  9422. {
  9423. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  9424. CloseClipboard();
  9425. }
  9426. }
  9427. //---------------------------------------------------------------------------
  9428. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  9429. {
  9430. return !FClipboardFakeDirectory.IsEmpty();
  9431. }
  9432. //---------------------------------------------------------------------------
  9433. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  9434. {
  9435. RemoveDir(ApiPath(FClipboardFakeDirectory));
  9436. FClipboardFakeDirectory = UnicodeString();
  9437. FClipboardTerminal = NULL;
  9438. // Also called in Idle()
  9439. FClipboardFakeMonitors.reset(NULL);
  9440. }
  9441. //---------------------------------------------------------------------------
  9442. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  9443. {
  9444. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  9445. {
  9446. ClipboardStop();
  9447. }
  9448. }
  9449. //---------------------------------------------------------------------------
  9450. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  9451. {
  9452. TTerminalManager * Manager = TTerminalManager::Instance();
  9453. Manager->ActiveTerminal = FClipboardTerminal;
  9454. TTransferOperationParam Params;
  9455. Params.TargetDirectory = TargetDirectory;
  9456. Params.DragDrop = DragDrop;
  9457. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  9458. }
  9459. //---------------------------------------------------------------------------
  9460. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  9461. {
  9462. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  9463. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  9464. {
  9465. RemoveDir(ApiPath(FileName));
  9466. if (!NonVisualDataModule->Busy)
  9467. {
  9468. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  9469. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  9470. }
  9471. }
  9472. }
  9473. //---------------------------------------------------------------------------