CustomScpExplorer.cpp 298 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277
  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. //---------------------------------------------------------------------------
  33. #pragma package(smart_init)
  34. #pragma link "CustomDirView"
  35. #pragma link "CustomUnixDirView"
  36. #pragma link "IEListView"
  37. #pragma link "NortonLikeListView"
  38. #pragma link "UnixDirView"
  39. #pragma link "CustomDriveView"
  40. #pragma link "UnixDriveView"
  41. #pragma link "CustomDriveView"
  42. #pragma link "TB2Dock"
  43. #pragma link "TBXStatusBars"
  44. #pragma link "TB2Item"
  45. #pragma link "TB2Toolbar"
  46. #pragma link "ThemePageControl"
  47. #pragma link "PathLabel"
  48. #ifndef NO_RESOURCES
  49. #pragma resource "*.dfm"
  50. #endif
  51. //---------------------------------------------------------------------------
  52. #define SAVE_SELECTION(DIRVIEW) \
  53. UnicodeString FocusFile = L""; \
  54. UnicodeString LastFocusedFile = L""; \
  55. if (DIRVIEW->ItemFocused) LastFocusedFile = DIRVIEW->ItemFocused->Caption; \
  56. { TListItem * ClosestUnselected = DIRVIEW->ClosestUnselected(DIRVIEW->ItemFocused); \
  57. if (ClosestUnselected) FocusFile = ClosestUnselected->Caption; }
  58. #define RESTORE_SELECTION(DIRVIEW) \
  59. if (!LastFocusedFile.IsEmpty() && \
  60. (!DIRVIEW->ItemFocused || (DIRVIEW->ItemFocused->Caption != LastFocusedFile))) \
  61. { \
  62. TListItem *ItemToSelect = DIRVIEW->FindFileItem(FocusFile); \
  63. if (ItemToSelect) \
  64. { \
  65. DIRVIEW->ItemFocused = ItemToSelect; \
  66. DIRVIEW->ItemFocused->MakeVisible(False); \
  67. } \
  68. }
  69. //---------------------------------------------------------------------------
  70. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  71. static const int SessionPanelCount = 4;
  72. //---------------------------------------------------------------------------
  73. class TMutexGuard
  74. {
  75. public:
  76. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  77. int Timeout = 5000)
  78. {
  79. FMutex = NULL;
  80. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  81. if (WaitResult == WAIT_TIMEOUT)
  82. {
  83. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  84. }
  85. else
  86. {
  87. FMutex = AMutex;
  88. }
  89. }
  90. ~TMutexGuard()
  91. {
  92. if (FMutex != NULL)
  93. {
  94. ReleaseMutex(FMutex);
  95. }
  96. }
  97. private:
  98. HANDLE FMutex;
  99. };
  100. //---------------------------------------------------------------------------
  101. class TWindowLock
  102. {
  103. public:
  104. TWindowLock(TCustomScpExplorerForm * Form) :
  105. FForm(Form)
  106. {
  107. FForm->LockWindow();
  108. }
  109. ~TWindowLock()
  110. {
  111. FForm->UnlockWindow();
  112. }
  113. private:
  114. TCustomScpExplorerForm * FForm;
  115. };
  116. //---------------------------------------------------------------------------
  117. struct TTransferOperationParam
  118. {
  119. TTransferOperationParam();
  120. UnicodeString TargetDirectory;
  121. bool Temp;
  122. bool DragDrop;
  123. int Options;
  124. TAutoSwitch Queue;
  125. };
  126. //---------------------------------------------------------------------------
  127. TTransferOperationParam::TTransferOperationParam()
  128. {
  129. Temp = false;
  130. DragDrop = false;
  131. Options = 0;
  132. Queue = asAuto;
  133. }
  134. //---------------------------------------------------------------------------
  135. class TTransferPresetNoteData : public TObject
  136. {
  137. public:
  138. UnicodeString Message;
  139. };
  140. //---------------------------------------------------------------------------
  141. class TTerminalNoteData : public TObject
  142. {
  143. public:
  144. TTerminal * Terminal;
  145. };
  146. //---------------------------------------------------------------------------
  147. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  148. FFormRestored(false),
  149. TForm(Owner)
  150. {
  151. FCurrentSide = osRemote;
  152. FEverShown = false;
  153. FDocks = new TList();
  154. RestoreParams();
  155. ConfigurationChanged();
  156. RemoteDirView->Invalidate();
  157. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  158. NonVisualDataModule->ScpExplorer = this;
  159. FAutoOperation = false;
  160. FOnFileOperationFinished = NULL;
  161. FForceExecution = false;
  162. FIgnoreNextDialogChar = 0;
  163. FErrorList = NULL;
  164. FSynchronizeProgressForm = NULL;
  165. FProgressForm = NULL;
  166. FRefreshLocalDirectory = false;
  167. FRefreshRemoteDirectory = false;
  168. FDDMoveSlipped = false;
  169. FDDExtMapFile = NULL;
  170. // CreateMutexW keeps failing with ERROR_NOACCESS
  171. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  172. DebugAssert(FDDExtMutex != NULL);
  173. FDDTargetControl = NULL;
  174. FDelayedDeletionTimer = NULL;
  175. FDelayedDeletionList = new TStringList();
  176. FDDFileList = NULL;
  177. FPendingTempSpaceWarn = false;
  178. FCapturedLog = NULL;
  179. FDragDropOperation = false;
  180. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  181. FAllowTransferPresetAutoSelect = true;
  182. FCopyParamDefault = L"";
  183. FSynchronizeController = NULL;
  184. FPendingQueueActionItem = NULL;
  185. FLockLevel = 0;
  186. FLockSuspendLevel = 0;
  187. FDisabledOnLockSuspend = false;
  188. FAlternativeDelete = false;
  189. FTrayIcon = new ::TTrayIcon(0);
  190. FTrayIcon->OnClick = TrayIconClick;
  191. FMaxQueueLength = 0;
  192. FLastContextPopupScreenPoint = TPoint(-1, -1);
  193. FTransferResumeList = NULL;
  194. FMoveToQueue = false;
  195. FStandaloneEditing = false;
  196. FOnFeedSynchronizeError = NULL;
  197. FNeedSession = false;
  198. FEditorManager = new TEditorManager();
  199. FEditorManager->OnFileChange = ExecutedFileChanged;
  200. FEditorManager->OnFileReload = ExecutedFileReload;
  201. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  202. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  203. FLocalEditors = new TList();
  204. FQueueStatus = NULL;
  205. FQueueStatusSection = new TCriticalSection();
  206. FQueueStatusInvalidated = false;
  207. FQueueItemInvalidated = false;
  208. FQueueActedItem = NULL;
  209. FQueueController = new TQueueController(QueueView3);
  210. FUserActionTimer = new TTimer(this);
  211. FUserActionTimer->Enabled = false;
  212. FUserActionTimer->Interval = 10;
  213. FUserActionTimer->OnTimer = UserActionTimer;
  214. FNotes = new TStringList();
  215. FNoteTimer = new TTimer(this);
  216. FNoteTimer->Enabled = false;
  217. FNoteTimer->OnTimer = NoteTimer;
  218. FOnNoteClick = NULL;
  219. FOle32Library = LoadLibrary(L"Ole32.dll");
  220. FDragMoveCursor = FOle32Library != NULL ?
  221. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  222. UseSystemSettings(this);
  223. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  224. static_cast<TObject*>(GetComponent(fcTransferList)));
  225. DebugAssert(TransferList != NULL);
  226. FTransferListHoverIndex = -1;
  227. TransferList->OnChange = TransferListChange;
  228. TransferList->OnDrawItem = TransferListDrawItem;
  229. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))));
  230. SetSubmenu(NonVisualDataModule->ColorMenuItem);
  231. UseDesktopFont(SessionsPageControl);
  232. UpdateSessionsPageControlHeight();
  233. UseDesktopFont(RemoteDirView);
  234. UseDesktopFont(RemoteDriveView);
  235. UseDesktopFont(QueueView3);
  236. UseDesktopFont(QueueLabel);
  237. UseDesktopFont(RemoteStatusBar);
  238. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  239. QueueSplitter->ShowHint = true;
  240. RemotePanelSplitter->ShowHint = true;
  241. UpdateImages();
  242. FCustomCommandMenu = CreateTBXPopupMenu(this);
  243. FCustomCommandLocalFileList = NULL;
  244. FCustomCommandRemoteFileList = NULL;
  245. FSessionColors = new TPngImageList(this);
  246. FSessionColors->ColorDepth = cd32Bit;
  247. AddFixedSessionImages();
  248. SessionsPageControl->Images = FSessionColors;
  249. QueueLabel->FocusControl = QueueView3;
  250. UpdateQueueLabel();
  251. FRemoteDirViewWasFocused = true;
  252. CreateHiddenWindow();
  253. StartUpdates();
  254. }
  255. //---------------------------------------------------------------------------
  256. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  257. {
  258. // this has to be one of the very first things to do
  259. StopUpdateThread();
  260. SessionsPageControl->Images = NULL;
  261. SAFE_DESTROY(FSessionColors);
  262. SAFE_DESTROY(FSessionsDragDropFilesEx);
  263. delete FCustomCommandLocalFileList;
  264. delete FCustomCommandRemoteFileList;
  265. delete FCustomCommandMenu;
  266. delete FTrayIcon;
  267. FTrayIcon = NULL;
  268. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  269. delete FEditorManager;
  270. ForceCloseLocalEditors();
  271. delete FLocalEditors;
  272. HideFileFindDialog();
  273. if (FDelayedDeletionTimer)
  274. {
  275. DoDelayedDeletion(NULL);
  276. SAFE_DESTROY(FDelayedDeletionTimer);
  277. }
  278. SAFE_DESTROY(FDelayedDeletionList);
  279. // sometimes we do not get DDEnd so the list is not released
  280. SAFE_DESTROY(FDDFileList);
  281. DebugAssert(FSynchronizeController == NULL);
  282. CloseHandle(FDDExtMutex);
  283. FDDExtMutex = NULL;
  284. FreeLibrary(FOle32Library);
  285. FOle32Library = NULL;
  286. FDragMoveCursor = NULL;
  287. DebugAssert(!FErrorList);
  288. if (FEverShown)
  289. {
  290. // when window is never shown (like when running command-line operation),
  291. // particularly window site is not restored correctly (BoundsRect value set
  292. // in RestoreForm gets lost during handle allocation), so we do not want
  293. // it to be stored
  294. StoreParams();
  295. }
  296. Terminal = NULL;
  297. Queue = NULL;
  298. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  299. NonVisualDataModule->ScpExplorer = NULL;
  300. delete FQueueController;
  301. FQueueController = NULL;
  302. delete FQueueStatusSection;
  303. FQueueStatusSection = NULL;
  304. delete FQueueStatus;
  305. FQueueStatus = NULL;
  306. delete FUserActionTimer;
  307. FUserActionTimer = NULL;
  308. delete FNoteTimer;
  309. delete FNotes;
  310. delete FNoteData;
  311. SAFE_DESTROY(FDocks);
  312. SAFE_DESTROY(FHistoryMenu[0][0]);
  313. SAFE_DESTROY(FHistoryMenu[0][1]);
  314. SAFE_DESTROY(FHistoryMenu[1][0]);
  315. SAFE_DESTROY(FHistoryMenu[1][1]);
  316. if (FHiddenWindow != NULL)
  317. {
  318. DestroyWindow(FHiddenWindow);
  319. FHiddenWindow = NULL;
  320. }
  321. }
  322. //---------------------------------------------------------------------------
  323. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  324. {
  325. std::unique_ptr<TSessionData> Data;
  326. if (!Session.IsEmpty())
  327. {
  328. bool DefaultsOnly;
  329. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  330. }
  331. TTerminalManager * Manager = TTerminalManager::Instance();
  332. for (int Index = 0; Index < Manager->Count; Index++)
  333. {
  334. TTerminal * Terminal = Manager->Terminals[Index];
  335. if (Session.IsEmpty() ||
  336. Terminal->SessionData->IsSameSite(Data.get()))
  337. {
  338. if (Path.IsEmpty())
  339. {
  340. Terminal->ClearCaches();
  341. }
  342. else
  343. {
  344. Terminal->DirectoryModified(Path, true);
  345. }
  346. }
  347. }
  348. // We should flag a pending refresh for the background terminals or busy foreground terminals
  349. if ((Terminal != NULL) && Terminal->Active &&
  350. CanCommandLineFromAnotherInstance() &&
  351. (Session.IsEmpty() || Terminal->SessionData->IsSameSite(Data.get())) &&
  352. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  353. {
  354. Terminal->ReloadDirectory();
  355. }
  356. }
  357. //---------------------------------------------------------------------------
  358. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  359. {
  360. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  361. size_t MessageSize = sizeof(TCopyDataMessage);
  362. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  363. if (Result)
  364. {
  365. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  366. Result = (Message.Version == TCopyDataMessage::Version1);
  367. if (Result)
  368. {
  369. switch (Message.Command)
  370. {
  371. case TCopyDataMessage::CommandCanCommandLine:
  372. Result = CanCommandLineFromAnotherInstance();
  373. break;
  374. case TCopyDataMessage::CommandCommandLine:
  375. {
  376. UnicodeString CommandLine(Message.CommandLine);
  377. Result = CommandLineFromAnotherInstance(CommandLine);
  378. }
  379. break;
  380. case TCopyDataMessage::RefreshPanel:
  381. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  382. break;
  383. case TCopyDataMessage::MainWindowCheck:
  384. Result = true;
  385. break;
  386. default:
  387. Result = false;
  388. break;
  389. }
  390. }
  391. }
  392. Message.Result = Result ? 1 : 0;
  393. }
  394. //---------------------------------------------------------------------------
  395. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  396. {
  397. WNDCLASS WindowClass = {0};
  398. WindowClass.lpfnWndProc = DefWindowProc;
  399. WindowClass.hInstance = HInstance;
  400. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  401. FHiddenWindow = NULL;
  402. if (RegisterClass(&WindowClass))
  403. {
  404. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  405. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  406. }
  407. }
  408. //---------------------------------------------------------------------------
  409. bool __fastcall TCustomScpExplorerForm::CanConsole()
  410. {
  411. return (Terminal != NULL) && (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  412. }
  413. //---------------------------------------------------------------------------
  414. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  415. {
  416. bool Result = !NonVisualDataModule->Busy;
  417. return Result;
  418. }
  419. //---------------------------------------------------------------------------
  420. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  421. const UnicodeString & CommandLine)
  422. {
  423. TProgramParams Params(CommandLine);
  424. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  425. if (Result)
  426. {
  427. NonVisualDataModule->StartBusy();
  428. try
  429. {
  430. // this action is initiated from another process,
  431. // so it's likely that our window is not visible,
  432. // and user won't see what is going on
  433. Application->BringToFront();
  434. // reload sessions as we may be asked to open a session
  435. // just stored by another instance
  436. StoredSessions->Load();
  437. UnicodeString SessionName = Params.Param[1];
  438. std::unique_ptr<TObjectList> DataList(new TObjectList());
  439. UnicodeString DownloadFile; // unused
  440. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this);
  441. if (DataList->Count > 0)
  442. {
  443. TTerminalManager * Manager = TTerminalManager::Instance();
  444. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  445. }
  446. }
  447. __finally
  448. {
  449. NonVisualDataModule->EndBusy();
  450. }
  451. }
  452. return Result;
  453. }
  454. //---------------------------------------------------------------------------
  455. void __fastcall TCustomScpExplorerForm::SetTerminal(TTerminal * value)
  456. {
  457. if (FTerminal != value)
  458. {
  459. TerminalChanging();
  460. FTerminal = value;
  461. bool PrevAllowTransferPresetAutoSelect = FAllowTransferPresetAutoSelect;
  462. FAllowTransferPresetAutoSelect = false;
  463. try
  464. {
  465. TerminalChanged();
  466. }
  467. __finally
  468. {
  469. FAllowTransferPresetAutoSelect = PrevAllowTransferPresetAutoSelect;
  470. }
  471. if (Terminal != NULL)
  472. {
  473. TransferPresetAutoSelect();
  474. }
  475. }
  476. }
  477. //---------------------------------------------------------------------------
  478. void __fastcall TCustomScpExplorerForm::TerminalChanging()
  479. {
  480. if (FTerminal != NULL)
  481. {
  482. UpdateTerminal(Terminal);
  483. }
  484. }
  485. //---------------------------------------------------------------------------
  486. void __fastcall TCustomScpExplorerForm::TerminalChanged()
  487. {
  488. RemoteDirView->Terminal = Terminal;
  489. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  490. TManagedTerminal * ManagedTerminal = NULL;
  491. if (Terminal != NULL)
  492. {
  493. if (Terminal->Active)
  494. {
  495. Terminal->RefreshDirectory();
  496. }
  497. ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  498. DebugAssert(ManagedTerminal != NULL);
  499. if (WinConfiguration->PreservePanelState)
  500. {
  501. if (ManagedTerminal->RemoteExplorerState != NULL)
  502. {
  503. RemoteDirView->RestoreState(ManagedTerminal->RemoteExplorerState);
  504. }
  505. else
  506. {
  507. RemoteDirView->ClearState();
  508. }
  509. }
  510. InitStatusBar();
  511. }
  512. DoTerminalListChanged(false);
  513. if (ManagedTerminal != NULL)
  514. {
  515. // this has to be set only after the tab is switched from DoTerminalListChanged,
  516. // otherwise we are changing color of wrong tab
  517. SessionColor = (TColor)ManagedTerminal->StateData->Color;
  518. }
  519. UpdateTransferList();
  520. // Update panels Enable state before refreshing the labels
  521. UpdateControls();
  522. }
  523. //---------------------------------------------------------------------------
  524. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  525. {
  526. if (Queue != value)
  527. {
  528. if (FQueue != NULL)
  529. {
  530. FQueue->OnListUpdate = NULL;
  531. FQueue->OnQueueItemUpdate = NULL;
  532. }
  533. FQueue = value;
  534. if (FQueue != NULL)
  535. {
  536. DebugAssert(FQueue->OnListUpdate == NULL);
  537. FQueue->OnListUpdate = QueueListUpdate;
  538. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  539. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  540. }
  541. QueueChanged();
  542. }
  543. }
  544. //---------------------------------------------------------------------------
  545. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  546. TListItem * Item)
  547. {
  548. if (FQueueActedItem == Item)
  549. {
  550. FQueueActedItem = NULL;
  551. if ((QueueView3->PopupMenu != NULL) &&
  552. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  553. {
  554. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  555. ReleaseCapture();
  556. }
  557. }
  558. if (Item->Data == FPendingQueueActionItem)
  559. {
  560. FPendingQueueActionItem = NULL;
  561. }
  562. }
  563. //---------------------------------------------------------------------------
  564. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  565. {
  566. {
  567. TGuard Guard(FQueueStatusSection);
  568. FQueueStatusInvalidated = false;
  569. if (FQueue != NULL)
  570. {
  571. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  572. }
  573. }
  574. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  575. {
  576. FMaxQueueLength = FQueueStatus->Count;
  577. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  578. }
  579. FQueueController->UpdateQueueStatus(FQueueStatus);
  580. SetQueueProgress();
  581. UpdateQueueView();
  582. bool IsEmpty = (FQueueStatus == NULL) || (FQueueStatus->Count == 0);
  583. if (IsEmpty && (Terminal != NULL))
  584. {
  585. TOnceDoneOperation OnceDoneOperation =
  586. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  587. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  588. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  589. {
  590. FQueue->Enabled = false;
  591. }
  592. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  593. {
  594. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  595. }
  596. }
  597. }
  598. //---------------------------------------------------------------------------
  599. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  600. {
  601. UnicodeString Result;
  602. if (FQueueStatus != NULL)
  603. {
  604. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  605. {
  606. TFileOperationProgressType * ProgressData =
  607. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  608. if ((ProgressData != NULL) && ProgressData->InProgress)
  609. {
  610. Result = TProgressForm::ProgressStr(ProgressData);
  611. }
  612. }
  613. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  614. {
  615. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  616. }
  617. }
  618. return Result;
  619. }
  620. //---------------------------------------------------------------------------
  621. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  622. {
  623. ComponentVisible[fcQueueView] =
  624. (WinConfiguration->QueueView.Show == qvShow) ||
  625. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  626. (FQueueStatus != NULL) && (FQueueStatus->Count > 0));
  627. }
  628. //---------------------------------------------------------------------------
  629. void __fastcall TCustomScpExplorerForm::QueueChanged()
  630. {
  631. if (FQueueStatus != NULL)
  632. {
  633. delete FQueueStatus;
  634. FQueueStatus = NULL;
  635. }
  636. UpdateQueueStatus(true);
  637. }
  638. //---------------------------------------------------------------------------
  639. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  640. {
  641. if (FQueue == Queue)
  642. {
  643. FQueueStatusInvalidated = true;
  644. }
  645. }
  646. //---------------------------------------------------------------------------
  647. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  648. TQueueItem * Item)
  649. {
  650. if (FQueue == Queue)
  651. {
  652. TGuard Guard(FQueueStatusSection);
  653. // this may be running in parallel with QueueChanged
  654. if (FQueueStatus != NULL)
  655. {
  656. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  657. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  658. {
  659. FRefreshLocalDirectory = (QueueItem == NULL) ||
  660. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  661. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  662. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  663. }
  664. if (QueueItem != NULL)
  665. {
  666. QueueItem->UserData = (void*)true;
  667. FQueueItemInvalidated = true;
  668. }
  669. }
  670. }
  671. }
  672. //---------------------------------------------------------------------------
  673. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  674. {
  675. // during standalone editing, we have no way to see/control queue,
  676. // so we have to always popup prompts automatically
  677. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  678. }
  679. //---------------------------------------------------------------------------
  680. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  681. {
  682. if (FQueueStatus != NULL)
  683. {
  684. bool QueueAutoPopup = IsQueueAutoPopup();
  685. bool NeedRefresh = FQueueController->NeedRefresh();
  686. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  687. FQueueItemInvalidated = false;
  688. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  689. bool Updated = false;
  690. TQueueItemProxy * QueueItem;
  691. bool WasUserAction;
  692. for (int Index = 0; Index < Limit; Index++)
  693. {
  694. bool Update = false;
  695. QueueItem = FQueueStatus->Items[Index];
  696. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  697. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  698. (FPendingQueueActionItem == NULL))
  699. {
  700. FPendingQueueActionItem = QueueItem;
  701. FUserActionTimer->Enabled = true;
  702. }
  703. if ((bool)QueueItem->UserData)
  704. {
  705. QueueItem->UserData = (void*)false;
  706. QueueItem->Update();
  707. Updated = true;
  708. Update = true;
  709. }
  710. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  711. {
  712. Update = true;
  713. }
  714. if (Update)
  715. {
  716. FQueueController->RefreshQueueItem(QueueItem);
  717. }
  718. }
  719. if (Updated)
  720. {
  721. NonVisualDataModule->UpdateNonVisibleActions();
  722. SetQueueProgress();
  723. }
  724. }
  725. }
  726. //---------------------------------------------------------------------------
  727. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  728. {
  729. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  730. }
  731. //---------------------------------------------------------------------------
  732. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  733. {
  734. if (ProgressData->Operation != foCalculateSize)
  735. {
  736. // implies TBPF_NORMAL
  737. FTaskbarList->SetProgressValue(GetMainForm()->Handle, ProgressData->OverallProgress(), 100);
  738. }
  739. else
  740. {
  741. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  742. }
  743. }
  744. //---------------------------------------------------------------------------
  745. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  746. {
  747. TTerminalManager::Instance()->QueueStatusUpdated();
  748. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  749. {
  750. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  751. {
  752. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  753. {
  754. TFileOperationProgressType * ProgressData = NULL;
  755. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  756. {
  757. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  758. }
  759. if ((ProgressData != NULL) &&
  760. ProgressData->InProgress)
  761. {
  762. SetTaskbarListProgressValue(ProgressData);
  763. }
  764. else
  765. {
  766. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  767. }
  768. }
  769. else
  770. {
  771. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  772. }
  773. }
  774. else
  775. {
  776. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  777. }
  778. }
  779. UpdateQueueLabel();
  780. }
  781. //---------------------------------------------------------------------------
  782. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  783. {
  784. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  785. if (FQueueStatus != NULL)
  786. {
  787. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  788. if (ActiveAndPendingPrimaryCount > 0)
  789. {
  790. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  791. }
  792. }
  793. QueueLabel->Caption = Caption;
  794. }
  795. //---------------------------------------------------------------------------
  796. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  797. {
  798. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  799. static_cast<TComponent*>(GetComponent(fcTransferList)));
  800. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  801. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  802. TransferList->Strings->BeginUpdate();
  803. try
  804. {
  805. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  806. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  807. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  808. if (FTransferDropDownHint.IsEmpty())
  809. {
  810. FTransferDropDownHint = TransferDropDown->Hint;
  811. }
  812. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  813. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  814. TransferDropDown->Text = StripHotkey(Name);
  815. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  816. (FTransferDropDownHint, StripHotkey(Name),
  817. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  818. FLAGMASK(Terminal != NULL, Terminal->UsableCopyParamAttrs(0).General))));
  819. // update the label, otherwise when it is updated only on the first draw
  820. // of the list, it is drawn "bold" for some reason
  821. FTransferListHoverIndex = TransferList->ItemIndex;
  822. UpdateTransferLabel();
  823. }
  824. __finally
  825. {
  826. TransferList->Strings->EndUpdate();
  827. }
  828. }
  829. //---------------------------------------------------------------------------
  830. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  831. {
  832. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  833. DebugAssert(Toolbar != NULL);
  834. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  835. }
  836. //---------------------------------------------------------------------------
  837. void __fastcall TCustomScpExplorerForm::UpdateActions()
  838. {
  839. TForm::UpdateActions();
  840. if (ComponentVisible[fcCustomCommandsBand])
  841. {
  842. UpdateCustomCommandsToolbar();
  843. }
  844. }
  845. //---------------------------------------------------------------------------
  846. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  847. {
  848. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  849. }
  850. //---------------------------------------------------------------------------
  851. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  852. {
  853. DebugAssert(Configuration && RemoteDirView);
  854. RemoteDirView->DDAllowMove = WinConfiguration->DDAllowMoveInit;
  855. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  856. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  857. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  858. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  859. if (RemoteDirView->RowSelect != WinConfiguration->FullRowSelect)
  860. {
  861. RemoteDirView->RowSelect = WinConfiguration->FullRowSelect;
  862. // selection is not redrawn automatically when RowSelect changes
  863. RemoteDirView->Invalidate();
  864. }
  865. RemoteDriveView->DDAllowMove = WinConfiguration->DDAllowMoveInit;
  866. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  867. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  868. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  869. UpdateSessionsPageControlHeight();
  870. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  871. UpdateToolbarDisplayMode();
  872. UpdateTransferList();
  873. if (Terminal != NULL)
  874. {
  875. TransferPresetAutoSelect();
  876. }
  877. UpdateQueueView();
  878. UpdateControls();
  879. // this can be called even before constuctor finishes.
  880. if (FEditorManager != NULL)
  881. {
  882. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  883. }
  884. // this can be called even before constuctor finishes.
  885. if (FLocalEditors != NULL)
  886. {
  887. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  888. {
  889. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  890. }
  891. }
  892. if (ComponentVisible[fcCustomCommandsBand])
  893. {
  894. UpdateCustomCommandsToolbar();
  895. }
  896. // show only when keeping queue items forever,
  897. // otherwise, its enough to have in in the context menu
  898. QueueDeleteAllDoneQueueToolbarItem->Visible =
  899. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  900. }
  901. //---------------------------------------------------------------------------
  902. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  903. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  904. void * /*Arg*/)
  905. {
  906. if (Token != NULL)
  907. {
  908. TForm * Editor = dynamic_cast<TForm*>(Token);
  909. DebugAssert(Editor != NULL);
  910. ReconfigureEditorForm(Editor);
  911. }
  912. }
  913. //---------------------------------------------------------------------------
  914. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  915. {
  916. WinConfiguration->DDTransferConfirmation = asOn;
  917. }
  918. //---------------------------------------------------------------------------
  919. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  920. TTransferDirection Direction, TTransferType Type, bool Temp,
  921. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  922. bool Confirm, bool DragDrop, int Options)
  923. {
  924. bool Result = true;
  925. DebugAssert(Terminal && Terminal->Active);
  926. // Temp means d&d here so far, may change in future!
  927. if (Temp && (Direction == tdToLocal) && (Type == ttMove) &&
  928. !WinConfiguration->DDAllowMove)
  929. {
  930. TMessageParams Params(mpNeverAskAgainCheck);
  931. unsigned int Answer = MessageDialog(LoadStr(DND_DOWNLOAD_MOVE_WARNING), qtWarning,
  932. qaOK | qaCancel, HELP_DND_DOWNLOAD_MOVE_WARNING, &Params);
  933. if (Answer == qaNeverAskAgain)
  934. {
  935. WinConfiguration->DDAllowMove = true;
  936. }
  937. else if (Answer == qaCancel)
  938. {
  939. Result = false;
  940. }
  941. }
  942. // these parameters are known in advance
  943. int Params =
  944. FLAGMASK(Type == ttMove, cpDelete);
  945. bool ToTemp = (Temp && (Direction == tdToLocal));
  946. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  947. {
  948. bool DisableNewerOnly =
  949. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  950. ToTemp;
  951. Options |=
  952. FLAGMASK(ToTemp, coTemp) |
  953. coDoNotShowAgain;
  954. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  955. int CopyParamAttrs = (Direction == tdToRemote ?
  956. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  957. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  958. int OutputOptions =
  959. FLAGMASK(DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto),
  960. cooDoNotShowAgain);
  961. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  962. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  963. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions);
  964. if (Result)
  965. {
  966. if (FLAGSET(OutputOptions, cooDoNotShowAgain))
  967. {
  968. if (DragDrop)
  969. {
  970. if (WinConfiguration->DDTransferConfirmation == asAuto)
  971. {
  972. PopupTrayBalloon(NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation,
  973. NULL, 0, EnableDDTransferConfirmation, NULL);
  974. }
  975. WinConfiguration->DDTransferConfirmation = asOff;
  976. }
  977. else
  978. {
  979. WinConfiguration->ConfirmTransferring = false;
  980. }
  981. }
  982. else
  983. {
  984. // User exclicitly unchecked "do not show again",
  985. // so show him the dialog the next time
  986. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  987. {
  988. WinConfiguration->DDTransferConfirmation = asOn;
  989. }
  990. }
  991. }
  992. }
  993. if (Result && CopyParam.Queue && !ToTemp)
  994. {
  995. Configuration->Usage->Inc(L"TransfersOnBackground");
  996. // these parameter are known only after transfer dialog
  997. Params |=
  998. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  999. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1000. Result = false;
  1001. ClearTransferSourceSelection(Direction);
  1002. }
  1003. return Result;
  1004. }
  1005. //---------------------------------------------------------------------------
  1006. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1007. {
  1008. if (FOnFileOperationFinished != NULL)
  1009. {
  1010. FOnFileOperationFinished(UnicodeString(), true);
  1011. }
  1012. else
  1013. {
  1014. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1015. if (HasDirView[Side])
  1016. {
  1017. DirView(Side)->SelectAll(smNone);
  1018. }
  1019. }
  1020. }
  1021. //---------------------------------------------------------------------------
  1022. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1023. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1024. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1025. int Params)
  1026. {
  1027. DebugAssert(Queue != NULL);
  1028. bool SingleFile = false;
  1029. if (FileList->Count == 1)
  1030. {
  1031. if (Direction == tdToRemote)
  1032. {
  1033. UnicodeString FileName = FileList->Strings[0];
  1034. SingleFile = FileExists(ApiPath(FileName));
  1035. }
  1036. else
  1037. {
  1038. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1039. SingleFile = !File->IsDirectory;
  1040. }
  1041. }
  1042. TQueueItem * QueueItem;
  1043. if (Direction == tdToRemote)
  1044. {
  1045. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1046. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1047. }
  1048. else
  1049. {
  1050. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1051. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1052. }
  1053. AddQueueItem(Queue, QueueItem, Terminal);
  1054. }
  1055. //---------------------------------------------------------------------------
  1056. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TTerminal * Terminal)
  1057. {
  1058. if (Queue->IsEmpty)
  1059. {
  1060. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  1061. ManagedTerminal->QueueOperationStart = Now();
  1062. }
  1063. Queue->AddItem(QueueItem);
  1064. }
  1065. //---------------------------------------------------------------------------
  1066. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1067. {
  1068. }
  1069. //---------------------------------------------------------------------------
  1070. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1071. {
  1072. DebugAssert(FDocks != NULL);
  1073. for (int Index = 0; Index < ComponentCount; Index++)
  1074. {
  1075. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1076. if ((Dock != NULL) && (Dock->Tag == 0))
  1077. {
  1078. FDocks->Add(Dock);
  1079. }
  1080. }
  1081. CollectItemsWithTextDisplayMode(this);
  1082. ConfigurationChanged();
  1083. QueuePanel->Height =
  1084. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1085. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1086. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1087. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1088. }
  1089. //---------------------------------------------------------------------------
  1090. void __fastcall TCustomScpExplorerForm::StoreParams()
  1091. {
  1092. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1093. WinConfiguration->QueueView.HeightPixelsPerInch = GetControlPixelsPerInch(this);
  1094. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1095. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1096. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1097. }
  1098. //---------------------------------------------------------------------------
  1099. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1100. {
  1101. if (!FFormRestored)
  1102. {
  1103. FFormRestored = true;
  1104. RestoreFormParams();
  1105. }
  1106. TForm::CreateParams(Params);
  1107. }
  1108. //---------------------------------------------------------------------------
  1109. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1110. {
  1111. DebugAssert(FDocks != NULL);
  1112. for (int Index = 0; Index < FDocks->Count; Index++)
  1113. {
  1114. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1115. }
  1116. }
  1117. //---------------------------------------------------------------------------
  1118. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr)
  1119. {
  1120. SetDockAllowDrag(true);
  1121. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1122. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1123. }
  1124. //---------------------------------------------------------------------------
  1125. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1126. {
  1127. UnicodeString Result;
  1128. SetDockAllowDrag(true);
  1129. Result = ::GetToolbarsLayoutStr(this);
  1130. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1131. return Result;
  1132. }
  1133. //---------------------------------------------------------------------------
  1134. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1135. TFileOperationProgressType & ProgressData)
  1136. {
  1137. // Download to temporary local directory
  1138. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1139. {
  1140. bool Continue = true;
  1141. FPendingTempSpaceWarn = false;
  1142. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1143. if (!Continue)
  1144. {
  1145. Abort();
  1146. }
  1147. }
  1148. // operation is being executed and we still didn't show up progress form
  1149. if (ProgressData.InProgress && !FProgressForm)
  1150. {
  1151. bool AllowSkip =
  1152. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1153. Terminal->IsCapable[fsSkipTransfer];
  1154. FProgressForm = new TProgressForm(Application, (FTransferResumeList != NULL), AllowSkip);
  1155. // As progress window has delayed show now, we need to lock ourselves,
  1156. // (at least) until then
  1157. LockWindow();
  1158. FProgressForm->DeleteToRecycleBin = (ProgressData.Side == osLocal ?
  1159. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete) :
  1160. ((Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1161. !Terminal->SessionData->RecycleBinPath.IsEmpty()));
  1162. // When main window is hidden, synchronisation form does not exist,
  1163. // we suppose "/upload" or URL download mode
  1164. if (!Visible && (ProgressData.Operation != foCalculateSize) &&
  1165. (ProgressData.Operation != foCalculateChecksum) &&
  1166. (FSynchronizeProgressForm == NULL) &&
  1167. !FStandaloneEditing)
  1168. {
  1169. FProgressForm->OnceDoneOperation = odoDisconnect;
  1170. }
  1171. if (FTaskbarList != NULL)
  1172. {
  1173. // Actually, do not know what hides the progress once the operation finishes
  1174. SetTaskbarListProgressState(TBPF_NORMAL);
  1175. }
  1176. }
  1177. // operation is finished (or terminated), so we hide progress form
  1178. else if (!ProgressData.InProgress && FProgressForm)
  1179. {
  1180. UnlockWindow();
  1181. SAFE_DESTROY(FProgressForm);
  1182. SetQueueProgress();
  1183. if (ProgressData.Operation == foCalculateSize)
  1184. {
  1185. // When calculating size before transfer, the abort caused by
  1186. // cancel flag set due to "MoveToQueue" below,
  1187. // is not propagated back to us in ExecuteFileOperation,
  1188. // as it is silently swallowed.
  1189. // So we explicitly (re)throw it here.
  1190. if (FMoveToQueue)
  1191. {
  1192. Abort();
  1193. }
  1194. }
  1195. else
  1196. {
  1197. if ((ProgressData.Cancel == csContinue) &&
  1198. !FAutoOperation)
  1199. {
  1200. OperationComplete(ProgressData.StartTime);
  1201. }
  1202. }
  1203. }
  1204. if (FProgressForm)
  1205. {
  1206. FProgressForm->SetProgressData(ProgressData);
  1207. if (FTaskbarList != NULL)
  1208. {
  1209. DebugAssert(ProgressData.InProgress);
  1210. SetTaskbarListProgressValue(&ProgressData);
  1211. }
  1212. if (FProgressForm->Cancel > csContinue)
  1213. {
  1214. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1215. if (FProgressForm->Cancel == csCancelFile)
  1216. {
  1217. FProgressForm->ClearCancel();
  1218. }
  1219. // cancel cancels even the move
  1220. FMoveToQueue = false;
  1221. }
  1222. else if (FProgressForm->MoveToQueue)
  1223. {
  1224. FMoveToQueue = true;
  1225. ProgressData.SetCancelAtLeast(csCancel);
  1226. }
  1227. if ((FTransferResumeList != NULL) &&
  1228. ProgressData.InProgress &&
  1229. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1230. !ProgressData.FullFileName.IsEmpty())
  1231. {
  1232. if ((FTransferResumeList->Count == 0) ||
  1233. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1234. {
  1235. // note that we do not recognize directories from files here
  1236. FTransferResumeList->Add(ProgressData.FullFileName);
  1237. }
  1238. }
  1239. }
  1240. }
  1241. //---------------------------------------------------------------------------
  1242. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1243. const TDateTime & StartTime)
  1244. {
  1245. if (GUIConfiguration->BeepOnFinish &&
  1246. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1247. {
  1248. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1249. DWORD Sound;
  1250. if (!ExtractFileExt(BeepSound).IsEmpty())
  1251. {
  1252. Sound = SND_FILENAME;
  1253. }
  1254. else
  1255. {
  1256. Sound = SND_ALIAS;
  1257. }
  1258. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1259. }
  1260. }
  1261. //---------------------------------------------------------------------------
  1262. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1263. TFileOperationProgressType & ProgressData)
  1264. {
  1265. FileOperationProgress(ProgressData);
  1266. }
  1267. //---------------------------------------------------------------------------
  1268. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1269. bool DragDrop)
  1270. {
  1271. return (!DragDrop && (DropSourceControl == NULL)) ||
  1272. (DropSourceControl == RemoteDirView);
  1273. }
  1274. //---------------------------------------------------------------------------
  1275. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1276. TFileOperation Operation, TOperationSide Side,
  1277. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1278. TOnceDoneOperation & OnceDoneOperation)
  1279. {
  1280. if (!FAutoOperation)
  1281. {
  1282. // no selection on "/upload", form servers only as event handler
  1283. // (it is not displayed)
  1284. if (PanelOperation(Side, FDragDropOperation) &&
  1285. Visible && (Operation != foCalculateSize) &&
  1286. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1287. {
  1288. if (FOnFileOperationFinished != NULL)
  1289. {
  1290. FOnFileOperationFinished(FileName, Success);
  1291. }
  1292. else
  1293. {
  1294. TCustomDirView * DView = DirView(Side);
  1295. UnicodeString FileNameOnly = ExtractFileName(FileName, (Side == osRemote));
  1296. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1297. // this can happen when local drive is unplugged in the middle of the operation
  1298. if (Item != NULL)
  1299. {
  1300. if (Success) Item->Selected = false;
  1301. DView->MakeProgressVisible(Item);
  1302. }
  1303. }
  1304. }
  1305. if ((Operation == foCopy) || (Operation == foMove))
  1306. {
  1307. if (Side == osLocal)
  1308. {
  1309. Configuration->Usage->Inc(L"UploadedFiles");
  1310. }
  1311. else
  1312. {
  1313. Configuration->Usage->Inc(L"DownloadedFiles");
  1314. }
  1315. }
  1316. }
  1317. if (Success && (FSynchronizeController != NULL))
  1318. {
  1319. if (Operation == foCopy)
  1320. {
  1321. DebugAssert(Side == osLocal);
  1322. FSynchronizeController->LogOperation(soUpload, FileName);
  1323. }
  1324. else if (Operation == foDelete)
  1325. {
  1326. DebugAssert(Side == osRemote);
  1327. FSynchronizeController->LogOperation(soDelete, FileName);
  1328. }
  1329. }
  1330. if (FProgressForm)
  1331. {
  1332. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1333. }
  1334. }
  1335. //---------------------------------------------------------------------------
  1336. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1337. TFileOperation Operation, TOperationSide Side,
  1338. bool Temp, const UnicodeString & FileName, Boolean Success,
  1339. TOnceDoneOperation & OnceDoneOperation)
  1340. {
  1341. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1342. OnceDoneOperation);
  1343. }
  1344. //---------------------------------------------------------------------------
  1345. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1346. {
  1347. DebugAssert(GetSide(Side) == osRemote);
  1348. DebugUsedParam(Side);
  1349. return RemoteDirView;
  1350. }
  1351. //---------------------------------------------------------------------------
  1352. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1353. {
  1354. DebugAssert(GetSide(Side) == osRemote);
  1355. DebugUsedParam(Side);
  1356. return (Terminal != NULL);
  1357. }
  1358. //---------------------------------------------------------------------------
  1359. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1360. TOperationSide Side, int FilesOnly)
  1361. {
  1362. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1363. }
  1364. //---------------------------------------------------------------------------
  1365. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1366. TOperationSide Side, int FilesOnly)
  1367. {
  1368. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1369. }
  1370. //---------------------------------------------------------------------------
  1371. struct THistoryItemData
  1372. {
  1373. short int Side;
  1374. short int Index;
  1375. };
  1376. //---------------------------------------------------------------------------
  1377. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1378. {
  1379. DirView(Side)->HistoryGo(Index);
  1380. }
  1381. //---------------------------------------------------------------------------
  1382. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1383. {
  1384. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1385. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1386. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1387. }
  1388. //---------------------------------------------------------------------------
  1389. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1390. bool Back)
  1391. {
  1392. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1393. {
  1394. TCustomDirView * DView = DirView(Side);
  1395. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1396. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1397. if (ICount > 10)
  1398. {
  1399. ICount = 10;
  1400. }
  1401. Menu->Items->Clear();
  1402. THistoryItemData Data;
  1403. Data.Side = Side;
  1404. for (short int i = 1; i <= ICount; i++)
  1405. {
  1406. TTBCustomItem * Item = new TTBXItem(Menu);
  1407. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1408. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, (Side == osRemote));
  1409. Item->Hint = DView->HistoryPath[Data.Index];
  1410. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1411. Item->Tag = *reinterpret_cast<int*>(&Data);
  1412. Item->OnClick = HistoryItemClick;
  1413. Menu->Items->Add(Item);
  1414. }
  1415. }
  1416. }
  1417. //---------------------------------------------------------------------------
  1418. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1419. TOperationSide Side, bool Back)
  1420. {
  1421. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1422. {
  1423. // workaround
  1424. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1425. // operator new allocates memory in Pascal code, but calls inline
  1426. // contructor in C++, leading in problems, the function does
  1427. // both in Pascal code
  1428. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1429. UpdateHistoryMenu(Side, Back);
  1430. }
  1431. return FHistoryMenu[Side == osLocal][Back];
  1432. }
  1433. //---------------------------------------------------------------------------
  1434. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1435. TCustomDirView *Sender)
  1436. {
  1437. TOperationSide Side = (Sender == DirView(osRemote) ? osRemote : osLocal);
  1438. UpdateHistoryMenu(Side, true);
  1439. UpdateHistoryMenu(Side, false);
  1440. }
  1441. //---------------------------------------------------------------------------
  1442. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1443. {
  1444. // remote custom commands can be executed only if the server supports shell commands
  1445. // or have secondary shell
  1446. return (FTerminal != NULL) && (FTerminal->IsCapable[fcSecondaryShell] || FTerminal->IsCapable[fcShellAnyCommand]);
  1447. }
  1448. //---------------------------------------------------------------------------
  1449. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1450. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1451. {
  1452. int Result;
  1453. TFileCustomCommand RemoteCustomCommand;
  1454. TLocalCustomCommand LocalCustomCommand;
  1455. TFileCustomCommand * NonInteractiveCustomCommand =
  1456. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1457. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1458. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1459. if (FLAGCLEAR(Command.Params, ccLocal))
  1460. {
  1461. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1462. // custom command that does not operate with files can be executed anytime ...
  1463. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1464. {
  1465. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1466. {
  1467. // ... but do not show such command in remote file menu (TODO)
  1468. Result = AllowedState;
  1469. }
  1470. else
  1471. {
  1472. Result = -1;
  1473. }
  1474. }
  1475. else
  1476. {
  1477. if ((ListType == ccltAll) || (ListType == ccltFile))
  1478. {
  1479. Result = ((FCurrentSide == osRemote) && DirView(osRemote)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1480. }
  1481. else
  1482. {
  1483. Result = -1;
  1484. }
  1485. }
  1486. }
  1487. else
  1488. {
  1489. // custom command that does not operate with files can be executed anytime
  1490. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1491. {
  1492. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1493. }
  1494. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1495. {
  1496. if ((ListType == ccltAll) || (ListType == ccltFile))
  1497. {
  1498. // special case is "diff"-style command that can be executed over any side,
  1499. // if we have both sides
  1500. Result =
  1501. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1502. // directly (instead of EnableSelectedOperation) to pass
  1503. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1504. ((HasDirView[osLocal] && DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1505. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1506. }
  1507. else if (ListType == ccltBoth)
  1508. {
  1509. Result = 1;
  1510. }
  1511. else
  1512. {
  1513. Result = -1;
  1514. }
  1515. }
  1516. else
  1517. {
  1518. if ((ListType == ccltAll) || (ListType == ccltFile))
  1519. {
  1520. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1521. }
  1522. else
  1523. {
  1524. Result = -1;
  1525. }
  1526. }
  1527. }
  1528. return Result;
  1529. }
  1530. //---------------------------------------------------------------------------
  1531. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1532. TStrings * FileList, const TCustomCommandType & ACommand,
  1533. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1534. {
  1535. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1536. {
  1537. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1538. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1539. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1540. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1541. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1542. bool Capture =
  1543. FLAGSET(ACommand.Params, ccShowResults) ||
  1544. FLAGSET(ACommand.Params, ccCopyResults) ||
  1545. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1546. TCaptureOutputEvent OutputEvent = NULL;
  1547. DebugAssert(FCapturedLog == NULL);
  1548. if (Capture)
  1549. {
  1550. FCapturedLog = new TStringList();
  1551. OutputEvent = TerminalCaptureLog;
  1552. }
  1553. try
  1554. {
  1555. if (!RemoteCustomCommand.IsFileCommand(Command))
  1556. {
  1557. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1558. OutputEvent);
  1559. }
  1560. else
  1561. {
  1562. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1563. }
  1564. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1565. {
  1566. if (FLAGSET(ACommand.Params, ccCopyResults))
  1567. {
  1568. CopyToClipboard(FCapturedLog);
  1569. }
  1570. if (FLAGSET(ACommand.Params, ccShowResults))
  1571. {
  1572. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1573. }
  1574. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1575. {
  1576. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1577. }
  1578. }
  1579. }
  1580. __finally
  1581. {
  1582. SAFE_DESTROY(FCapturedLog);
  1583. }
  1584. }
  1585. }
  1586. //---------------------------------------------------------------------------
  1587. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1588. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1589. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1590. {
  1591. TStrings * LocalFileList = NULL;
  1592. TStrings * RemoteFileList = NULL;
  1593. TStrings * RemoteFileListFull = NULL;
  1594. try
  1595. {
  1596. if (LocalFileCommand)
  1597. {
  1598. if (ALocalFileList == NULL)
  1599. {
  1600. DebugAssert(HasDirView[osLocal]);
  1601. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1602. // directly (instead of EnableSelectedOperation) to pass
  1603. // false to FocusedFileOnlyWhenFocused
  1604. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1605. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1606. }
  1607. else
  1608. {
  1609. LocalFileList = ALocalFileList;
  1610. }
  1611. if (FileListCommand)
  1612. {
  1613. if (LocalFileList->Count != 1)
  1614. {
  1615. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1616. }
  1617. }
  1618. else
  1619. {
  1620. if ((LocalFileList->Count != 1) &&
  1621. (FileList->Count != 1) &&
  1622. (LocalFileList->Count != FileList->Count))
  1623. {
  1624. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1625. }
  1626. }
  1627. }
  1628. UnicodeString RootTempDir;
  1629. UnicodeString TempDir;
  1630. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1631. if (!RemoteFiles)
  1632. {
  1633. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1634. }
  1635. try
  1636. {
  1637. TDateTimes RemoteFileTimes;
  1638. if (RemoteFiles)
  1639. {
  1640. RemoteFileList = FileList;
  1641. }
  1642. else
  1643. {
  1644. RemoteFileList = new TStringList();
  1645. TMakeLocalFileListParams MakeFileListParam;
  1646. MakeFileListParam.FileList = RemoteFileList;
  1647. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1648. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1649. MakeFileListParam.Recursive =
  1650. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1651. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1652. if (!MakeFileListParam.Recursive)
  1653. {
  1654. RemoteFileTimes.clear();
  1655. RemoteFileListFull = new TStringList();
  1656. MakeFileListParam.FileList = RemoteFileListFull;
  1657. MakeFileListParam.Recursive = true;
  1658. MakeFileListParam.IncludeDirs = false;
  1659. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1660. }
  1661. else
  1662. {
  1663. RemoteFileListFull = RemoteFileList;
  1664. }
  1665. }
  1666. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1667. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1668. if (!NonBlocking)
  1669. {
  1670. Progress.Start(foCustomCommand, osRemote, FileListCommand ? 1 : FileList->Count);
  1671. DebugAssert(FProgressForm != NULL);
  1672. FProgressForm->ReadOnly = true;
  1673. }
  1674. try
  1675. {
  1676. if (FileListCommand)
  1677. {
  1678. UnicodeString LocalFile;
  1679. // MakeFileList does not delimit filenames
  1680. UnicodeString FileList = MakeFileList(RemoteFileList);
  1681. if (LocalFileCommand)
  1682. {
  1683. DebugAssert(LocalFileList->Count == 1);
  1684. LocalFile = LocalFileList->Strings[0];
  1685. }
  1686. TLocalCustomCommand CustomCommand(Data,
  1687. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1688. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1689. if (NonBlocking)
  1690. {
  1691. DebugAssert(!POutput);
  1692. ExecuteShellChecked(ShellCommand);
  1693. }
  1694. else
  1695. {
  1696. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1697. }
  1698. }
  1699. else if (LocalFileCommand)
  1700. {
  1701. if (LocalFileList->Count == 1)
  1702. {
  1703. UnicodeString LocalFile = LocalFileList->Strings[0];
  1704. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1705. {
  1706. UnicodeString FileName = RemoteFileList->Strings[Index];
  1707. TLocalCustomCommand CustomCommand(Data,
  1708. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  1709. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1710. }
  1711. }
  1712. else if (RemoteFileList->Count == 1)
  1713. {
  1714. UnicodeString FileName = RemoteFileList->Strings[0];
  1715. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1716. {
  1717. TLocalCustomCommand CustomCommand(
  1718. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1719. FileName, LocalFileList->Strings[Index], L"");
  1720. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1721. }
  1722. }
  1723. else
  1724. {
  1725. if (LocalFileList->Count != RemoteFileList->Count)
  1726. {
  1727. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  1728. }
  1729. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1730. {
  1731. UnicodeString FileName = RemoteFileList->Strings[Index];
  1732. TLocalCustomCommand CustomCommand(
  1733. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1734. FileName, LocalFileList->Strings[Index], L"");
  1735. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1736. }
  1737. }
  1738. }
  1739. else
  1740. {
  1741. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1742. {
  1743. TLocalCustomCommand CustomCommand(Data,
  1744. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1745. RemoteFileList->Strings[Index], L"", L"");
  1746. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1747. }
  1748. }
  1749. }
  1750. __finally
  1751. {
  1752. if (!NonBlocking)
  1753. {
  1754. Progress.Stop();
  1755. }
  1756. }
  1757. DebugAssert(!FAutoOperation);
  1758. if (!RemoteFiles)
  1759. {
  1760. TempDir = IncludeTrailingBackslash(TempDir);
  1761. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  1762. {
  1763. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  1764. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  1765. // Skip directories, process only nested files.
  1766. // The check is redundant as FileAge fails for directories anyway.
  1767. !DirectoryExists(FileName))
  1768. {
  1769. UnicodeString RemoteDir =
  1770. UnixExtractFileDir(
  1771. UnixIncludeTrailingBackslash(FTerminal->CurrentDirectory) +
  1772. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  1773. TDateTime NewTime;
  1774. if (FileAge(FileName, NewTime) &&
  1775. (NewTime != RemoteFileTimes[Index]))
  1776. {
  1777. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  1778. TemporaryFileCopyParam(CopyParam);
  1779. CopyParam.FileMask = L"";
  1780. FAutoOperation = true;
  1781. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  1782. TemporaryFilesList->Add(FileName);
  1783. FTerminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  1784. }
  1785. }
  1786. }
  1787. }
  1788. }
  1789. __finally
  1790. {
  1791. FAutoOperation = false;
  1792. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  1793. {
  1794. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  1795. }
  1796. }
  1797. }
  1798. __finally
  1799. {
  1800. if (RemoteFileList != FileList)
  1801. {
  1802. delete RemoteFileList;
  1803. }
  1804. if (LocalFileList != ALocalFileList)
  1805. {
  1806. delete LocalFileList;
  1807. }
  1808. if (RemoteFileListFull != RemoteFileList)
  1809. {
  1810. delete RemoteFileListFull;
  1811. }
  1812. }
  1813. }
  1814. //---------------------------------------------------------------------------
  1815. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithRemoteFiles(
  1816. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  1817. bool FileListCommand, UnicodeString * POutput)
  1818. {
  1819. std::unique_ptr<TStrings> SelectedFileList(DirView(osLocal)->CreateFileList(false, true, NULL));
  1820. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  1821. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  1822. {
  1823. UnicodeString FileName = SelectedFileList->Strings[Index];
  1824. if (DirectoryExists(FileName))
  1825. {
  1826. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  1827. {
  1828. LocalFileList->Add(FileName);
  1829. }
  1830. if (FLAGSET(ACommand.Params, ccRecursive))
  1831. {
  1832. TMakeLocalFileListParams MakeFileListParam;
  1833. MakeFileListParam.FileList = LocalFileList.get();
  1834. MakeFileListParam.FileTimes = NULL;
  1835. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1836. MakeFileListParam.Recursive = true;
  1837. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  1838. }
  1839. }
  1840. else
  1841. {
  1842. LocalFileList->Add(FileName);
  1843. }
  1844. }
  1845. if (FileListCommand)
  1846. {
  1847. UnicodeString FileList = MakeFileList(LocalFileList.get());
  1848. TLocalCustomCommand CustomCommand(
  1849. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1850. L"", L"", FileList);
  1851. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1852. }
  1853. else
  1854. {
  1855. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1856. Progress.Start(foCustomCommand, osRemote, FileListCommand ? 1 : LocalFileList->Count);
  1857. DebugAssert(FProgressForm != NULL);
  1858. FProgressForm->ReadOnly = true;
  1859. try
  1860. {
  1861. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1862. {
  1863. UnicodeString FileName = LocalFileList->Strings[Index];
  1864. TLocalCustomCommand CustomCommand(
  1865. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1866. FileName, L"", L"");
  1867. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1868. }
  1869. }
  1870. __finally
  1871. {
  1872. Progress.Stop();
  1873. }
  1874. }
  1875. }
  1876. //---------------------------------------------------------------------------
  1877. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  1878. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  1879. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1880. {
  1881. TLocalCustomCommand LocalCustomCommand(Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory());
  1882. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1883. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  1884. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1885. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  1886. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  1887. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  1888. std::unique_ptr<UnicodeString> POutput;
  1889. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  1890. FLAGSET(ACommand.Params, ccCopyResults))
  1891. {
  1892. POutput.reset(new UnicodeString());
  1893. }
  1894. if (!LocalCustomCommand.IsFileCommand(Command))
  1895. {
  1896. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  1897. }
  1898. // remote files?
  1899. else if ((FCurrentSide == osRemote) || LocalFileCommand)
  1900. {
  1901. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  1902. }
  1903. // local files
  1904. else
  1905. {
  1906. LocalCustomCommandWithRemoteFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  1907. }
  1908. if (POutput.get() != NULL)
  1909. {
  1910. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  1911. int P = POutput->Pos(sLineBreak);
  1912. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  1913. {
  1914. POutput->SetLength(P - 1);
  1915. }
  1916. // Copy even empty output to clipboard
  1917. if (FLAGSET(ACommand.Params, ccCopyResults))
  1918. {
  1919. CopyToClipboard(*POutput);
  1920. }
  1921. // But do not show an empty message box.
  1922. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  1923. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  1924. !POutput->IsEmpty())
  1925. {
  1926. TClipboardHandler ClipboardHandler;
  1927. ClipboardHandler.Text = *POutput;
  1928. TMessageParams Params;
  1929. TQueryButtonAlias Aliases[1];
  1930. Aliases[0].Button = qaRetry;
  1931. Aliases[0].Alias = LoadStr(URL_LINK_COPY); // misuse
  1932. Aliases[0].OnClick = &ClipboardHandler.Copy;
  1933. Params.Aliases = Aliases;
  1934. Params.AliasesCount = LENOF(Aliases);
  1935. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  1936. }
  1937. }
  1938. }
  1939. //---------------------------------------------------------------------------
  1940. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  1941. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  1942. {
  1943. TCustomCommandData Data(Terminal);
  1944. UnicodeString Site = Terminal->SessionData->SessionKey;
  1945. UnicodeString HelpKeyword = ACommand.HomePage;
  1946. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  1947. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  1948. {
  1949. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  1950. if (FLAGCLEAR(ACommand.Params, ccLocal))
  1951. {
  1952. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, Terminal->CurrentDirectory));
  1953. }
  1954. else
  1955. {
  1956. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory()));
  1957. }
  1958. if (!DoCustomCommandOptionsDialog(
  1959. &ACommand, CustomCommandOptions.get(), TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site))
  1960. {
  1961. Abort();
  1962. }
  1963. }
  1964. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  1965. if (FLAGCLEAR(ACommand.Params, ccLocal))
  1966. {
  1967. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  1968. }
  1969. else
  1970. {
  1971. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  1972. }
  1973. }
  1974. //---------------------------------------------------------------------------
  1975. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  1976. const TCustomCommandType & Command)
  1977. {
  1978. DebugAssert(FCustomCommandLocalFileList != NULL);
  1979. DebugAssert(FCustomCommandRemoteFileList != NULL);
  1980. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  1981. TStrings * LocalFileList = new TStringList();
  1982. TStrings * RemoteFileList = new TStringList();
  1983. try
  1984. {
  1985. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  1986. {
  1987. LocalFileList->Clear();
  1988. LocalFileList->AddObject(
  1989. FCustomCommandLocalFileList->Strings[Index],
  1990. FCustomCommandLocalFileList->Objects[Index]);
  1991. RemoteFileList->Clear();
  1992. RemoteFileList->AddObject(
  1993. FCustomCommandRemoteFileList->Strings[Index],
  1994. FCustomCommandRemoteFileList->Objects[Index]);
  1995. CustomCommand(RemoteFileList, Command, LocalFileList);
  1996. }
  1997. }
  1998. __finally
  1999. {
  2000. delete LocalFileList;
  2001. delete RemoteFileList;
  2002. }
  2003. }
  2004. //---------------------------------------------------------------------------
  2005. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2006. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2007. {
  2008. delete FCustomCommandLocalFileList;
  2009. delete FCustomCommandRemoteFileList;
  2010. // takeover ownership,
  2011. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2012. FCustomCommandLocalFileList = LocalFileList;
  2013. FCustomCommandRemoteFileList = RemoteFileList;
  2014. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2015. if (Button != NULL)
  2016. {
  2017. FCustomCommandMenu->Items->Clear();
  2018. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth);
  2019. MenuPopup(FCustomCommandMenu, Button);
  2020. }
  2021. else
  2022. {
  2023. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2024. }
  2025. }
  2026. //---------------------------------------------------------------------------
  2027. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2028. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2029. {
  2030. DebugAssert(FCapturedLog != NULL);
  2031. if ((OutputType == cotOutput) || (OutputType == cotError))
  2032. {
  2033. FCapturedLog->Add(AddedLine);
  2034. }
  2035. }
  2036. //---------------------------------------------------------------------------
  2037. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2038. TOperationSide Side)
  2039. {
  2040. return (Side == osRemote) &&
  2041. ((Control == RemoteDirView) || (Control == RemoteDriveView));
  2042. }
  2043. //---------------------------------------------------------------------------
  2044. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2045. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2046. {
  2047. TTBItemOptions O;
  2048. O = Item->Options;
  2049. TCustomDirView * DView = DirView(Side);
  2050. if ((DView->ItemFocused != NULL) &&
  2051. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2052. // when resolving links is disabled, default action is to enter the directory,
  2053. // no matter what DoubleClickAction is configured to
  2054. ((Side != osRemote) || Terminal->ResolvingSymlinks) &&
  2055. // Can only Edit files, but can Open/Copy even directories
  2056. ((DoubleClickAction != dcaEdit) ||
  2057. !DView->ItemIsDirectory(DView->ItemFocused)))
  2058. {
  2059. Item->Options = O << tboDefault;
  2060. }
  2061. else
  2062. {
  2063. Item->Options = O >> tboDefault;
  2064. }
  2065. }
  2066. //---------------------------------------------------------------------------
  2067. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2068. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2069. {
  2070. TCustomDirView * DView = DirView(Side);
  2071. TListItem * Item = DView->ItemFocused;
  2072. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2073. EnableFocusedOperation[Side])
  2074. {
  2075. TPoint ClientPoint;
  2076. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2077. TPoint(0, 0) : MousePos;
  2078. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2079. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2080. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2081. }
  2082. }
  2083. //---------------------------------------------------------------------------
  2084. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2085. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2086. {
  2087. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2088. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2089. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2090. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2091. Handled = true;
  2092. }
  2093. //---------------------------------------------------------------------------
  2094. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2095. {
  2096. }
  2097. //---------------------------------------------------------------------------
  2098. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2099. {
  2100. DebugAssert(FErrorList == NULL);
  2101. if (WinConfiguration->ContinueOnError)
  2102. {
  2103. FErrorList = new TStringList();
  2104. Configuration->Usage->Inc(L"ContinuationsOnError");
  2105. }
  2106. NonVisualDataModule->StartBusy();
  2107. }
  2108. //---------------------------------------------------------------------------
  2109. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2110. {
  2111. NonVisualDataModule->EndBusy();
  2112. if (FErrorList)
  2113. {
  2114. HandleErrorList(FErrorList);
  2115. }
  2116. }
  2117. //---------------------------------------------------------------------------
  2118. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2119. const TCopyParamType & CopyParam)
  2120. {
  2121. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2122. {
  2123. Configuration->Usage->Inc(L"FileMaskUses");
  2124. }
  2125. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2126. {
  2127. Configuration->Usage->Inc(L"OperationMaskUses");
  2128. }
  2129. }
  2130. //---------------------------------------------------------------------------
  2131. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2132. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2133. {
  2134. void * BatchStorage;
  2135. BatchStart(BatchStorage);
  2136. bool Result;
  2137. try
  2138. {
  2139. if ((Operation == foCopy) || (Operation == foMove))
  2140. {
  2141. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2142. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2143. UnicodeString TargetDirectory;
  2144. bool Temp = false;
  2145. bool DragDrop = false;
  2146. int Options = 0;
  2147. TAutoSwitch UseQueue = asAuto;
  2148. if (Param != NULL)
  2149. {
  2150. TTransferOperationParam& TParam =
  2151. *static_cast<TTransferOperationParam*>(Param);
  2152. TargetDirectory = TParam.TargetDirectory;
  2153. Temp = TParam.Temp;
  2154. DragDrop = TParam.DragDrop;
  2155. Options = TParam.Options;
  2156. UseQueue = TParam.Queue;
  2157. }
  2158. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2159. switch (UseQueue)
  2160. {
  2161. case asOn:
  2162. CopyParam.Queue = true;
  2163. break;
  2164. case asOff:
  2165. CopyParam.Queue = false;
  2166. break;
  2167. case asAuto:
  2168. default:
  2169. // keep default
  2170. break;
  2171. }
  2172. Result =
  2173. CopyParamDialog(Direction, Type, Temp, FileList, TargetDirectory,
  2174. CopyParam, !NoConfirmation, DragDrop, Options);
  2175. if (Result)
  2176. {
  2177. DebugAssert(Terminal);
  2178. bool SelectionRestored = false;
  2179. TCustomDirView * DView = NULL;
  2180. if (HasDirView[Side])
  2181. {
  2182. DView = DirView(Side);
  2183. DView->SaveSelection();
  2184. DView->SaveSelectedNames();
  2185. }
  2186. UpdateCopyParamCounters(CopyParam);
  2187. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2188. DebugAssert(FTransferResumeList == NULL);
  2189. FTransferResumeList =
  2190. Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2191. FMoveToQueue = false;
  2192. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2193. try
  2194. {
  2195. TStrings * PermanentFileList;
  2196. std::unique_ptr<TStrings> PermanentFileListOwner;
  2197. try
  2198. {
  2199. if (Side == osLocal)
  2200. {
  2201. PermanentFileList = FileList;
  2202. Params |= FLAGMASK(Temp, cpTemporary);
  2203. Terminal->CopyToRemote(FileList, TargetDirectory, &CopyParam, Params, NULL);
  2204. if (Operation == foMove)
  2205. {
  2206. ReloadLocalDirectory();
  2207. if (DView != NULL)
  2208. {
  2209. DView->RestoreSelection();
  2210. }
  2211. SelectionRestored = true;
  2212. }
  2213. }
  2214. else
  2215. {
  2216. // Clone the file list as it may refer to current directory files,
  2217. // which get destroyed, when the source directory is reloaded after foMove operation.
  2218. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2219. // But for a hotfix, we are not going to do such a big change.
  2220. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2221. PermanentFileList = PermanentFileListOwner.get();
  2222. try
  2223. {
  2224. Terminal->CopyToLocal(
  2225. FileList, TargetDirectory, &CopyParam, Params, NULL);
  2226. }
  2227. __finally
  2228. {
  2229. if (Operation == foMove)
  2230. {
  2231. if (DView != NULL)
  2232. {
  2233. DView->RestoreSelection();
  2234. }
  2235. SelectionRestored = true;
  2236. }
  2237. ReloadLocalDirectory(TargetDirectory);
  2238. }
  2239. }
  2240. }
  2241. catch (EAbort &)
  2242. {
  2243. if (FMoveToQueue)
  2244. {
  2245. Params |=
  2246. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2247. DebugAssert(CopyParam.TransferSkipList == NULL);
  2248. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2249. if (TransferResumeList->Count > 0)
  2250. {
  2251. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2252. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2253. }
  2254. CopyParam.TransferSkipList = TransferResumeList.release();
  2255. // not really needed, just to keep it consistent with TransferResumeList
  2256. FTransferResumeList = NULL;
  2257. FMoveToQueue = false;
  2258. Configuration->Usage->Inc("MovesToBackground");
  2259. AddQueueItem(Queue, Direction, PermanentFileList, TargetDirectory, CopyParam, Params);
  2260. ClearTransferSourceSelection(Direction);
  2261. }
  2262. throw;
  2263. }
  2264. }
  2265. __finally
  2266. {
  2267. if (!SelectionRestored && (DView != NULL))
  2268. {
  2269. DView->DiscardSavedSelection();
  2270. }
  2271. FTransferResumeList = NULL;
  2272. }
  2273. }
  2274. }
  2275. else if (Operation == foRename)
  2276. {
  2277. DebugAssert(DirView(Side)->ItemFocused);
  2278. DirView(Side)->ItemFocused->EditCaption();
  2279. Result = true;
  2280. }
  2281. else if (Operation == foDelete)
  2282. {
  2283. DebugAssert(FileList->Count);
  2284. // We deliberately do not toggle alternative flag (Param), but use OR,
  2285. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2286. // shortcut of CurrentDeleteAlternativeAction
  2287. bool Alternative =
  2288. bool(Param) || UseAlternativeFunction();
  2289. bool Recycle;
  2290. if (Side == osLocal)
  2291. {
  2292. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2293. }
  2294. else
  2295. {
  2296. Recycle = (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2297. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2298. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2299. }
  2300. Result =
  2301. !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2302. if (!Result)
  2303. {
  2304. UnicodeString Query;
  2305. if (FileList->Count == 1)
  2306. {
  2307. if (Side == osLocal)
  2308. {
  2309. Query = ExtractFileName(FileList->Strings[0]);
  2310. }
  2311. else
  2312. {
  2313. Query = UnixExtractFileName(FileList->Strings[0]);
  2314. }
  2315. Query = FMTLOAD(
  2316. (Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2317. }
  2318. else
  2319. {
  2320. Query = FMTLOAD(
  2321. (Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2322. }
  2323. TMessageParams Params(mpNeverAskAgainCheck);
  2324. Params.ImageName = L"Delete file";
  2325. unsigned int Answer = MessageDialog(MainInstructions(Query), qtConfirmation,
  2326. qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2327. if (Answer == qaNeverAskAgain)
  2328. {
  2329. Result = true;
  2330. if (Recycle)
  2331. {
  2332. WinConfiguration->ConfirmRecycling = false;
  2333. }
  2334. else
  2335. {
  2336. WinConfiguration->ConfirmDeleting = false;
  2337. }
  2338. }
  2339. else
  2340. {
  2341. Result = (Answer == qaOK);
  2342. }
  2343. }
  2344. if (Result)
  2345. {
  2346. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2347. }
  2348. }
  2349. else if (Operation == foSetProperties)
  2350. {
  2351. RemoteDirView->SaveSelectedNames();
  2352. Result = SetProperties(Side, FileList);
  2353. }
  2354. else if (Operation == foCustomCommand)
  2355. {
  2356. DebugAssert(Param);
  2357. DebugAssert(Side == osRemote);
  2358. RemoteDirView->SaveSelectedNames();
  2359. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2360. CustomCommand(FileList, *Command, NULL);
  2361. Result = true;
  2362. }
  2363. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2364. {
  2365. DebugAssert(Side == osRemote);
  2366. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2367. (Operation == foRemoteMove), reinterpret_cast<TTerminal *>(Param));
  2368. }
  2369. else if (Operation == foLock)
  2370. {
  2371. DebugAssert(Side == osRemote);
  2372. LockFiles(FileList, true);
  2373. Result = true;
  2374. }
  2375. else if (Operation == foUnlock)
  2376. {
  2377. DebugAssert(Side == osRemote);
  2378. LockFiles(FileList, false);
  2379. Result = true;
  2380. }
  2381. else
  2382. {
  2383. DebugFail();
  2384. }
  2385. }
  2386. __finally
  2387. {
  2388. BatchEnd(BatchStorage);
  2389. }
  2390. return Result;
  2391. }
  2392. //---------------------------------------------------------------------------
  2393. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2394. {
  2395. if (Side == osCurrent)
  2396. {
  2397. Side = FCurrentSide;
  2398. }
  2399. return Side;
  2400. }
  2401. //---------------------------------------------------------------------------
  2402. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2403. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2404. {
  2405. Side = GetSide(Side);
  2406. bool Result;
  2407. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, (Side == osLocal), NULL);
  2408. try
  2409. {
  2410. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2411. }
  2412. __finally
  2413. {
  2414. delete FileList;
  2415. }
  2416. return Result;
  2417. }
  2418. //---------------------------------------------------------------------------
  2419. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2420. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2421. bool NoConfirmation, void * Param)
  2422. {
  2423. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2424. {
  2425. if ((Operation == foCopy) || (Operation == foMove))
  2426. {
  2427. if (GetSide(Side) == osLocal)
  2428. {
  2429. Configuration->Usage->Inc(L"UploadsCommand");
  2430. }
  2431. else
  2432. {
  2433. Configuration->Usage->Inc(L"DownloadsCommand");
  2434. }
  2435. }
  2436. }
  2437. }
  2438. //---------------------------------------------------------------------------
  2439. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2440. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2441. {
  2442. TTransferOperationParam Param;
  2443. if ((WinConfiguration->Interface != ifCommander) ||
  2444. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2445. {
  2446. Flags &= ~cocShortCutHint;
  2447. }
  2448. TCustomDirView * DView = DirView(Side);
  2449. Param.Options =
  2450. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2451. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2452. if (FLAGSET(Flags, cocQueue))
  2453. {
  2454. Param.Queue = asOn;
  2455. }
  2456. else if (FLAGSET(Flags, cocNonQueue))
  2457. {
  2458. Param.Queue = asOff;
  2459. }
  2460. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2461. }
  2462. //---------------------------------------------------------------------------
  2463. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2464. {
  2465. try
  2466. {
  2467. if (ErrorList->Count)
  2468. {
  2469. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2470. if (MessageDialog(Message, qtError,
  2471. qaOK | qaCancel, HELP_NONE) == qaOK)
  2472. {
  2473. unsigned int Answer;
  2474. int Index = 0;
  2475. do
  2476. {
  2477. DebugAssert(Index >= 0 && Index < ErrorList->Count);
  2478. TQueryButtonAlias Aliases[2];
  2479. Aliases[0].Button = qaYes;
  2480. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2481. Aliases[1].Button = qaNo;
  2482. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2483. TMessageParams Params;
  2484. Params.Aliases = Aliases;
  2485. Params.AliasesCount = LENOF(Aliases);
  2486. Answer = MoreMessageDialog(
  2487. FMTLOAD(ERROR_LIST_NUMBER, (Index+1, ErrorList->Count, ErrorList->Strings[Index])),
  2488. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2489. (Index ? qaYes : 0) | (Index < ErrorList->Count - 1 ? qaNo : 0) |
  2490. qaOK, HELP_NONE, &Params);
  2491. if (Answer == qaNo)
  2492. {
  2493. Index++;
  2494. }
  2495. if (Answer == qaYes)
  2496. {
  2497. Index--;
  2498. }
  2499. }
  2500. while (Answer != qaOK);
  2501. }
  2502. }
  2503. }
  2504. __finally
  2505. {
  2506. TStrings * List = ErrorList;
  2507. ErrorList = NULL;
  2508. for (int i = 0; i < List->Count; i++)
  2509. {
  2510. delete List->Objects[i];
  2511. }
  2512. delete List;
  2513. }
  2514. }
  2515. //---------------------------------------------------------------------------
  2516. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2517. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2518. {
  2519. // needed for checking filemasks, as there's no directory object
  2520. // associated with the file object
  2521. File->FullFileName = FullFileName;
  2522. TFileMasks::TParams MaskParams;
  2523. MaskParams.Size = File->Size;
  2524. MaskParams.Modification = File->Modification;
  2525. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2526. }
  2527. //---------------------------------------------------------------------------
  2528. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2529. {
  2530. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2531. Side = GetSide(Side);
  2532. TCustomDirView * DView = DirView(Side);
  2533. TListItem * FocusedItem = DView->ItemFocused;
  2534. UnicodeString Name;
  2535. if ((FocusedItem != NULL) && !DView->ItemIsParentDirectory(FocusedItem))
  2536. {
  2537. Name = DView->ItemFileName(FocusedItem);
  2538. }
  2539. else
  2540. {
  2541. Name = LoadStr(NEW_FILE);
  2542. }
  2543. UnicodeString Names = Name;
  2544. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2545. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2546. HELP_EDIT_NEW, History.get(), true))
  2547. {
  2548. while (!Names.IsEmpty())
  2549. {
  2550. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2551. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2552. UnicodeString TargetFileName;
  2553. UnicodeString LocalFileName;
  2554. UnicodeString RootTempDir;
  2555. UnicodeString TempDir;
  2556. UnicodeString RemoteDirectory;
  2557. bool ExistingFile = false;
  2558. if (Side == osRemote)
  2559. {
  2560. Name = AbsolutePath(FTerminal->CurrentDirectory, Name);
  2561. TRemoteFile * File = NULL;
  2562. if (FTerminal->FileExists(Name, &File))
  2563. {
  2564. try
  2565. {
  2566. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2567. ExistingFile = true;
  2568. }
  2569. __finally
  2570. {
  2571. delete File;
  2572. }
  2573. }
  2574. if (!ExistingFile)
  2575. {
  2576. RemoteDirectory = UnixExtractFilePath(Name);
  2577. TemporaryDirectoryForRemoteFiles(
  2578. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2579. TargetFileName = UnixExtractFileName(Name);
  2580. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2581. LocalFileName = TempDir +
  2582. // We probably do not want to trim the VMS version here
  2583. FTerminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2584. }
  2585. }
  2586. else
  2587. {
  2588. if (ExtractFilePath(Name).IsEmpty())
  2589. {
  2590. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2591. }
  2592. else
  2593. {
  2594. LocalFileName = ExpandFileName(Name);
  2595. }
  2596. TargetFileName = ExtractFileName(Name);
  2597. }
  2598. if (!ExistingFile)
  2599. {
  2600. if (!FileExists(ApiPath(LocalFileName)))
  2601. {
  2602. int File = FileCreate(ApiPath(LocalFileName));
  2603. if (File < 0)
  2604. {
  2605. if (!RootTempDir.IsEmpty())
  2606. {
  2607. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2608. }
  2609. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2610. }
  2611. else
  2612. {
  2613. FileClose(File);
  2614. }
  2615. }
  2616. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2617. const TEditorData * ExternalEditor = NULL;
  2618. TFileMasks::TParams MaskParams; // size not known
  2619. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2620. false, MaskParams);
  2621. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2622. ExternalEditor, RootTempDir, RemoteDirectory);
  2623. }
  2624. }
  2625. }
  2626. }
  2627. //---------------------------------------------------------------------------
  2628. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2629. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2630. {
  2631. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2632. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2633. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2634. return
  2635. ((ExecuteFileBy == efInternalEditor)) ||
  2636. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2637. }
  2638. //---------------------------------------------------------------------------
  2639. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2640. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2641. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2642. UnicodeString RemoteDirectory)
  2643. {
  2644. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2645. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2646. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2647. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2648. Side = GetSide(Side);
  2649. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2650. if (Side == osRemote)
  2651. {
  2652. Data->Terminal = Terminal;
  2653. Data->Queue = Queue;
  2654. Data->SessionData = CloneCurrentSessionData();
  2655. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2656. Data->RemoteDirectory = RemoteDirectory;
  2657. Data->SessionName = Terminal->SessionData->SessionName;
  2658. Data->LocalRootDirectory = LocalRootDirectory;
  2659. Data->OriginalFileName = OriginalFileName;
  2660. Data->Command = L""; // will be changed later for external editor
  2661. }
  2662. if (ExecuteFileBy == efInternalEditor)
  2663. {
  2664. if (Side == osRemote)
  2665. {
  2666. UnicodeString Caption = UnixIncludeTrailingBackslash(RemoteDirectory) + OriginalFileName +
  2667. L" - " + Terminal->SessionData->SessionName;
  2668. TForm * Editor;
  2669. try
  2670. {
  2671. Editor = ShowEditorForm(FileName, this, FEditorManager->FileChanged,
  2672. FEditorManager->FileReload, FEditorManager->FileClosed,
  2673. SaveAllInternalEditors, AnyInternalEditorModified,
  2674. Caption, FStandaloneEditing, SessionColor);
  2675. }
  2676. catch(...)
  2677. {
  2678. if (!LocalRootDirectory.IsEmpty())
  2679. {
  2680. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  2681. }
  2682. throw;
  2683. }
  2684. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  2685. }
  2686. else
  2687. {
  2688. DebugAssert(!FStandaloneEditing);
  2689. TForm * Editor =
  2690. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  2691. SaveAllInternalEditors, AnyInternalEditorModified,
  2692. L"", false, SessionColor);
  2693. FLocalEditors->Add(Editor);
  2694. }
  2695. }
  2696. else
  2697. {
  2698. HANDLE Process;
  2699. if (ExecuteFileBy == efExternalEditor)
  2700. {
  2701. UnicodeString Program, Params, Dir;
  2702. Data->Command = ExternalEditor->ExternalEditor;
  2703. ReformatFileNameCommand(Data->Command);
  2704. SplitCommand(Data->Command, Program, Params, Dir);
  2705. Params = ExpandFileNameCommand(Params, FileName);
  2706. Program = ExpandEnvironmentVariables(Program);
  2707. if (!ExecuteShell(Program, Params, Process))
  2708. {
  2709. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  2710. }
  2711. }
  2712. else
  2713. {
  2714. DebugAssert(Side == osRemote);
  2715. if (!ExecuteShell(FileName, L"", Process))
  2716. {
  2717. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  2718. }
  2719. }
  2720. if ((Side == osLocal) ||
  2721. ((ExecuteFileBy == efShell) &&
  2722. !WinConfiguration->Editor.SDIShellEditor) ||
  2723. ((ExecuteFileBy == efExternalEditor) &&
  2724. !ExternalEditor->SDIExternalEditor))
  2725. {
  2726. // no need for handle
  2727. if (Process != NULL)
  2728. {
  2729. DebugCheck(CloseHandle(Process));
  2730. }
  2731. Process = INVALID_HANDLE_VALUE;
  2732. }
  2733. else
  2734. {
  2735. if (Process == NULL)
  2736. {
  2737. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  2738. }
  2739. }
  2740. if (Side == osRemote)
  2741. {
  2742. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  2743. }
  2744. }
  2745. }
  2746. //---------------------------------------------------------------------------
  2747. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  2748. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2749. void * /*Arg*/)
  2750. {
  2751. if (Token != NULL)
  2752. {
  2753. EditorFormFileSave(static_cast<TForm *>(Token));
  2754. }
  2755. }
  2756. //---------------------------------------------------------------------------
  2757. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  2758. {
  2759. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  2760. {
  2761. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  2762. }
  2763. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  2764. }
  2765. //---------------------------------------------------------------------------
  2766. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  2767. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2768. void * Arg)
  2769. {
  2770. if ((Token != NULL) &&
  2771. IsEditorFormModified(static_cast<TForm *>(Token)))
  2772. {
  2773. *static_cast<bool *>(Arg) = true;
  2774. }
  2775. }
  2776. //---------------------------------------------------------------------------
  2777. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  2778. TObject * /*Sender*/, bool & Modified)
  2779. {
  2780. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  2781. {
  2782. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  2783. {
  2784. Modified = true;
  2785. }
  2786. }
  2787. if (!Modified)
  2788. {
  2789. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  2790. }
  2791. }
  2792. //---------------------------------------------------------------------------
  2793. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  2794. {
  2795. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  2796. }
  2797. //---------------------------------------------------------------------------
  2798. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  2799. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  2800. UnicodeString & Result, UnicodeString & RootDirectory)
  2801. {
  2802. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  2803. {
  2804. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  2805. Result = RootDirectory;
  2806. }
  2807. else
  2808. {
  2809. RootDirectory = L"";
  2810. Result = WinConfiguration->ExpandedTemporaryDirectory();
  2811. Result = IncludeTrailingBackslash(Result);
  2812. }
  2813. if (WinConfiguration->TemporaryDirectoryAppendSession)
  2814. {
  2815. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(Terminal->SessionData->SessionName));
  2816. }
  2817. if (WinConfiguration->TemporaryDirectoryAppendPath)
  2818. {
  2819. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  2820. {
  2821. RemoteDirectory.Delete(1, 1);
  2822. }
  2823. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  2824. }
  2825. if (!ForceDirectories(ApiPath(Result)))
  2826. {
  2827. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  2828. }
  2829. }
  2830. //---------------------------------------------------------------------------
  2831. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  2832. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  2833. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  2834. {
  2835. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2836. if (ForceText)
  2837. {
  2838. CopyParam.TransferMode = tmAscii;
  2839. }
  2840. TemporaryFileCopyParam(CopyParam);
  2841. if (AllFiles)
  2842. {
  2843. CopyParam.IncludeFileMask = TFileMasks();
  2844. }
  2845. if (TempDir.IsEmpty())
  2846. {
  2847. TemporaryDirectoryForRemoteFiles(FTerminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  2848. }
  2849. DebugAssert(!FAutoOperation);
  2850. FAutoOperation = AutoOperation;
  2851. Terminal->ExceptionOnFail = true;
  2852. try
  2853. {
  2854. try
  2855. {
  2856. // turn off confirmations, as for MDI editors we may possibly download
  2857. // the same file over
  2858. Terminal->CopyToLocal(
  2859. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  2860. if (GetTargetNames)
  2861. {
  2862. for (int i = 0; i < FileList->Count; i++)
  2863. {
  2864. FileList->Strings[i] =
  2865. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  2866. }
  2867. }
  2868. }
  2869. catch(...)
  2870. {
  2871. if (!RootTempDir.IsEmpty())
  2872. {
  2873. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2874. }
  2875. throw;
  2876. }
  2877. }
  2878. __finally
  2879. {
  2880. FAutoOperation = false;
  2881. Terminal->ExceptionOnFail = false;
  2882. }
  2883. }
  2884. //---------------------------------------------------------------------------
  2885. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  2886. {
  2887. // Do not waste time checking for default editor list the next time,
  2888. // or testing if default editor is not notepad.
  2889. bool TryNextTime = false;
  2890. UnicodeString UsageState;
  2891. if (!WinConfiguration->EditorList->IsDefaultList())
  2892. {
  2893. UsageState = "H";
  2894. }
  2895. else
  2896. {
  2897. UnicodeString Executable;
  2898. UnicodeString ExecutableDescription;
  2899. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  2900. {
  2901. UnicodeString Message =
  2902. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  2903. unsigned int Answer =
  2904. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  2905. if (Answer != qaOK)
  2906. {
  2907. UsageState = "R";
  2908. }
  2909. else
  2910. {
  2911. UsageState = "A";
  2912. TEditorData EditorData;
  2913. EditorData.Editor = edExternal;
  2914. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  2915. EditorData.DecideExternalEditorText();
  2916. TEditorList EditorList;
  2917. EditorList = *WinConfiguration->EditorList;
  2918. EditorList.Insert(0, new TEditorPreferences(EditorData));
  2919. WinConfiguration->EditorList = &EditorList;
  2920. }
  2921. }
  2922. }
  2923. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  2924. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  2925. }
  2926. //---------------------------------------------------------------------------
  2927. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  2928. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  2929. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  2930. {
  2931. if (ExecuteFileBy == efDefaultEditor)
  2932. {
  2933. if (!WinConfiguration->OfferedEditorAutoConfig)
  2934. {
  2935. EditorAutoConfig();
  2936. }
  2937. const TEditorPreferences * Editor =
  2938. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  2939. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  2940. {
  2941. ExecuteFileBy = efInternalEditor;
  2942. ExternalEditor = NULL;
  2943. }
  2944. else if (Editor->Data->Editor == edOpen)
  2945. {
  2946. ExecuteFileBy = efShell;
  2947. ExternalEditor = NULL;
  2948. }
  2949. else
  2950. {
  2951. ExecuteFileBy = efExternalEditor;
  2952. ExternalEditor = Editor->Data;
  2953. }
  2954. }
  2955. }
  2956. //---------------------------------------------------------------------------
  2957. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  2958. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  2959. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  2960. {
  2961. UnicodeString OriginalFileName;
  2962. UnicodeString LocalRootDirectory;
  2963. UnicodeString RemoteDirectory;
  2964. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  2965. (Side == osLocal), MaskParams);
  2966. UnicodeString Counter;
  2967. UnicodeString LocalFileName;
  2968. if (Side == osRemote)
  2969. {
  2970. // We need to trim VMS version here, so that we use name without version
  2971. // when uploading back to create a new version of the file
  2972. OriginalFileName = FTerminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  2973. RemoteDirectory = UnixExtractFilePath(FullFileName);
  2974. TObject * Token = NULL;
  2975. UnicodeString LocalDirectory;
  2976. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  2977. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  2978. LocalDirectory))
  2979. {
  2980. if (Token != NULL)
  2981. {
  2982. TForm * Form = dynamic_cast<TForm *>(Token);
  2983. if (Form->WindowState == wsMinimized)
  2984. {
  2985. ShowWindow(Form->Handle, SW_RESTORE);
  2986. }
  2987. else
  2988. {
  2989. Form->SetFocus();
  2990. }
  2991. Abort();
  2992. }
  2993. else
  2994. {
  2995. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  2996. }
  2997. }
  2998. TStringList * FileList1 = new TStringList();
  2999. try
  3000. {
  3001. FileList1->AddObject(FullFileName, Object);
  3002. TemporarilyDownloadFiles(FileList1,
  3003. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3004. LocalRootDirectory, LocalDirectory, true, true, true);
  3005. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3006. }
  3007. __finally
  3008. {
  3009. delete FileList1;
  3010. }
  3011. switch (ExecuteFileBy)
  3012. {
  3013. case efShell:
  3014. Counter = "RemoteFilesExecuted";
  3015. break;
  3016. case efInternalEditor:
  3017. Counter = "RemoteFilesOpenedInInternalEditor";
  3018. break;
  3019. case efExternalEditor:
  3020. Counter = "RemoteFilesOpenedInExternalEditor";
  3021. break;
  3022. default:
  3023. DebugFail();
  3024. }
  3025. }
  3026. else
  3027. {
  3028. LocalFileName = FullFileName;
  3029. OriginalFileName = ExtractFileName(FullFileName);
  3030. switch (ExecuteFileBy)
  3031. {
  3032. case efShell:
  3033. Counter = "LocalFilesExecuted";
  3034. break;
  3035. case efInternalEditor:
  3036. Counter = "LocalFilesOpenedInInternalEditor";
  3037. break;
  3038. case efExternalEditor:
  3039. Counter = "LocalFilesOpenedInExternalEditor";
  3040. break;
  3041. default:
  3042. DebugFail();
  3043. }
  3044. }
  3045. Configuration->Usage->Inc(Counter);
  3046. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3047. ExternalEditor, LocalRootDirectory, RemoteDirectory);
  3048. }
  3049. //---------------------------------------------------------------------------
  3050. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3051. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3052. bool AllSelected, bool OnFocused)
  3053. {
  3054. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3055. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3056. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3057. Side = GetSide(Side);
  3058. TCustomDirView * DView = DirView(Side);
  3059. TStrings * FileList = AllSelected ?
  3060. DView->CreateFileList(OnFocused, Side == osLocal) :
  3061. DView->CreateFocusedFileList(Side == osLocal);
  3062. try
  3063. {
  3064. DebugAssert(AllSelected || (FileList->Count == 1));
  3065. for (int i = 0; i < FileList->Count; i++)
  3066. {
  3067. UnicodeString ListFileName = FileList->Strings[i];
  3068. UnicodeString FileNameOnly = (Side == osRemote) ?
  3069. UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3070. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3071. if (!DView->ItemIsDirectory(Item))
  3072. {
  3073. UnicodeString FullFileName;
  3074. if (Side == osRemote)
  3075. {
  3076. FullFileName = RemoteDirView->Path + ListFileName;
  3077. }
  3078. else
  3079. {
  3080. FullFileName = ListFileName;
  3081. }
  3082. TFileMasks::TParams MaskParams;
  3083. MaskParams.Size = DView->ItemFileSize(Item);
  3084. TDateTimePrecision Precision;
  3085. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3086. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3087. FileList->Objects[i], MaskParams);
  3088. }
  3089. }
  3090. }
  3091. __finally
  3092. {
  3093. delete FileList;
  3094. }
  3095. }
  3096. //---------------------------------------------------------------------------
  3097. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3098. {
  3099. // do not forget to add additional options to TemporarilyDownloadFiles, and AS
  3100. CopyParam.FileNameCase = ncNoChange;
  3101. CopyParam.PreserveRights = false;
  3102. CopyParam.PreserveReadOnly = false;
  3103. CopyParam.ReplaceInvalidChars = true;
  3104. CopyParam.IncludeFileMask = TFileMasks();
  3105. CopyParam.NewerOnly = false;
  3106. CopyParam.FileMask = L"";
  3107. }
  3108. //---------------------------------------------------------------------------
  3109. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3110. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3111. {
  3112. TTerminalManager * Manager = TTerminalManager::Instance();
  3113. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3114. {
  3115. if (!NonVisualDataModule->Busy)
  3116. {
  3117. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3118. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3119. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3120. if (Data->Terminal == NULL)
  3121. {
  3122. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3123. if (SameSiteTerminal != NULL)
  3124. {
  3125. UnicodeString Message =
  3126. FMTLOAD(EDIT_SESSION_REATTACH,
  3127. (FileNameOnly, Data->SessionName, FileNameOnly));
  3128. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3129. {
  3130. Data->Terminal = Terminal;
  3131. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3132. Data->SessionName = Terminal->SessionData->SessionName;
  3133. // We might also overwrite session data
  3134. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3135. }
  3136. else
  3137. {
  3138. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3139. Abort();
  3140. }
  3141. }
  3142. }
  3143. // foreground session should reconnect itself
  3144. else if (Terminal != Data->Terminal)
  3145. {
  3146. UnicodeString Message =
  3147. MainInstructions(
  3148. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3149. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3150. {
  3151. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3152. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3153. }
  3154. else
  3155. {
  3156. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3157. Abort();
  3158. }
  3159. }
  3160. }
  3161. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3162. {
  3163. Configuration->Usage->Inc(L"EditInactiveSession");
  3164. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3165. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3166. (ExtractFileName(FileName), Data->SessionName)));
  3167. }
  3168. }
  3169. TStrings * FileList = new TStringList();
  3170. try
  3171. {
  3172. FileList->Add(FileName);
  3173. // consider using the same settings (preset) as when the file was downloaded
  3174. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3175. TemporaryFileCopyParam(CopyParam);
  3176. if (Data->ForceText)
  3177. {
  3178. CopyParam.TransferMode = tmAscii;
  3179. }
  3180. // so i do not need to worry if masking algorithm works in all cases
  3181. // ("" means "copy file name", no masking is actually done)
  3182. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3183. {
  3184. CopyParam.FileMask = L"";
  3185. }
  3186. else
  3187. {
  3188. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3189. }
  3190. DebugAssert(Data->Queue != NULL);
  3191. int Params = cpNoConfirmation | cpTemporary;
  3192. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3193. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3194. QueueItem->CompleteEvent = UploadCompleteEvent;
  3195. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3196. }
  3197. __finally
  3198. {
  3199. delete FileList;
  3200. }
  3201. }
  3202. //---------------------------------------------------------------------------
  3203. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3204. const UnicodeString FileName, const TEditedFileData * Data)
  3205. {
  3206. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3207. DebugAssert(!NonVisualDataModule->Busy);
  3208. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3209. {
  3210. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3211. (ExtractFileName(FileName), Data->SessionName)));
  3212. }
  3213. TTerminal * PrevTerminal = TTerminalManager::Instance()->ActiveTerminal;
  3214. TTerminalManager::Instance()->ActiveTerminal = Data->Terminal;
  3215. NonVisualDataModule->StartBusy();
  3216. try
  3217. {
  3218. std::unique_ptr<TRemoteFile> File;
  3219. UnicodeString RemoteFileName =
  3220. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3221. FTerminal->ExceptionOnFail = true;
  3222. try
  3223. {
  3224. TRemoteFile * AFile = NULL;
  3225. FTerminal->ReadFile(RemoteFileName, AFile);
  3226. File.reset(AFile);
  3227. if (!File->HaveFullFileName)
  3228. {
  3229. File->FullFileName = RemoteFileName;
  3230. }
  3231. }
  3232. __finally
  3233. {
  3234. FTerminal->ExceptionOnFail = false;
  3235. }
  3236. std::unique_ptr<TStrings> FileList(new TStringList());
  3237. FileList->AddObject(RemoteFileName, File.get());
  3238. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3239. UnicodeString TempDir = ExtractFilePath(FileName);
  3240. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3241. TempDir, true, true, true);
  3242. // sanity check, the target file name should be still the same
  3243. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3244. }
  3245. __finally
  3246. {
  3247. NonVisualDataModule->EndBusy();
  3248. // it actually may not exist anymore...
  3249. TTerminalManager::Instance()->ActiveTerminal = PrevTerminal;
  3250. }
  3251. }
  3252. //---------------------------------------------------------------------------
  3253. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3254. const TEditedFileData * Data, bool & KeepOpen)
  3255. {
  3256. // Command is set for external editors only (not for "shell" open).
  3257. if (!Data->Command.IsEmpty())
  3258. {
  3259. bool AnyFound = false;
  3260. bool AnyMDI = false;
  3261. bool AnyNonMDI = false;
  3262. bool AnyDetect = false;
  3263. TEditorList * EditorList = new TEditorList();
  3264. try
  3265. {
  3266. *EditorList = *WinConfiguration->EditorList;
  3267. for (int i = 0; i < EditorList->Count; i++)
  3268. {
  3269. const TEditorPreferences * Editor = EditorList->Editors[i];
  3270. if ((Editor->Data->Editor == edExternal) &&
  3271. (Editor->Data->ExternalEditor == Data->Command))
  3272. {
  3273. AnyFound = true;
  3274. if (Editor->Data->SDIExternalEditor)
  3275. {
  3276. AnyNonMDI = true;
  3277. if (Editor->Data->DetectMDIExternalEditor)
  3278. {
  3279. AnyDetect = true;
  3280. }
  3281. }
  3282. else
  3283. {
  3284. AnyMDI = true;
  3285. }
  3286. }
  3287. }
  3288. bool EnableMDI = false;
  3289. bool DisableDetect = false;
  3290. if (AnyMDI)
  3291. {
  3292. KeepOpen = true;
  3293. if (AnyNonMDI)
  3294. {
  3295. // there is at least one instance of the editor with MDI support enabled,
  3296. // and one with disabled, enable it for all instances
  3297. EnableMDI = true;
  3298. }
  3299. }
  3300. else if (AnyFound && !AnyDetect)
  3301. {
  3302. // at least once instance found but all have MDI autodetection disabled
  3303. // => close the file (default action)
  3304. }
  3305. else
  3306. {
  3307. // no instance of the editor has MDI support enabled
  3308. TMessageParams Params;
  3309. if (AnyFound)
  3310. {
  3311. // there is at least one instance of the editor
  3312. Params.Params |= mpNeverAskAgainCheck;
  3313. }
  3314. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3315. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3316. switch (Answer)
  3317. {
  3318. case qaNeverAskAgain:
  3319. DisableDetect = true;
  3320. break;
  3321. case qaNo:
  3322. EnableMDI = true;
  3323. KeepOpen = true;
  3324. break;
  3325. }
  3326. }
  3327. if (AnyFound && (EnableMDI || DisableDetect))
  3328. {
  3329. bool Changed = false;
  3330. for (int i = 0; i < EditorList->Count; i++)
  3331. {
  3332. const TEditorPreferences * Editor = EditorList->Editors[i];
  3333. if ((Editor->Data->Editor == edExternal) &&
  3334. (Editor->Data->ExternalEditor == Data->Command) &&
  3335. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3336. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3337. {
  3338. Changed = true;
  3339. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3340. if (EnableMDI)
  3341. {
  3342. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3343. }
  3344. if (DisableDetect)
  3345. {
  3346. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3347. }
  3348. EditorList->Change(i, UpdatedEditor);
  3349. }
  3350. }
  3351. if (Changed)
  3352. {
  3353. WinConfiguration->EditorList = EditorList;
  3354. }
  3355. }
  3356. }
  3357. __finally
  3358. {
  3359. delete EditorList;
  3360. }
  3361. }
  3362. else
  3363. {
  3364. // "open" case
  3365. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3366. qaOK, HELP_APP_EARLY_CLOSED);
  3367. }
  3368. }
  3369. //---------------------------------------------------------------------------
  3370. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3371. {
  3372. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3373. }
  3374. //---------------------------------------------------------------------------
  3375. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3376. {
  3377. SideEnter(osRemote);
  3378. }
  3379. //---------------------------------------------------------------------------
  3380. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3381. {
  3382. MakeNextInTabOrder(RemoteDirView, RemoteDriveView);
  3383. SideEnter(osRemote);
  3384. }
  3385. //---------------------------------------------------------------------------
  3386. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3387. {
  3388. FCurrentSide = Side;
  3389. if (Visible)
  3390. {
  3391. UpdateControls();
  3392. }
  3393. }
  3394. //---------------------------------------------------------------------------
  3395. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3396. TStrings * FileList, bool Alternative)
  3397. {
  3398. DebugAssert(Terminal);
  3399. TCustomDirView * DView = DirView(Side);
  3400. DView->SaveSelection();
  3401. DView->SaveSelectedNames();
  3402. DebugAssert(!FAlternativeDelete);
  3403. FAlternativeDelete = Alternative;
  3404. try
  3405. {
  3406. if (Side == osRemote)
  3407. {
  3408. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3409. }
  3410. else
  3411. {
  3412. try
  3413. {
  3414. Terminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3415. }
  3416. __finally
  3417. {
  3418. ReloadLocalDirectory();
  3419. }
  3420. }
  3421. FAlternativeDelete = false;
  3422. }
  3423. catch(...)
  3424. {
  3425. FAlternativeDelete = false;
  3426. DView->DiscardSavedSelection();
  3427. throw;
  3428. }
  3429. DView->RestoreSelection();
  3430. }
  3431. //---------------------------------------------------------------------------
  3432. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3433. {
  3434. DebugAssert(Terminal);
  3435. RemoteDirView->SaveSelection();
  3436. RemoteDirView->SaveSelectedNames();
  3437. try
  3438. {
  3439. if (Lock)
  3440. {
  3441. Terminal->LockFiles(FileList);
  3442. }
  3443. else
  3444. {
  3445. Terminal->UnlockFiles(FileList);
  3446. }
  3447. }
  3448. catch(...)
  3449. {
  3450. RemoteDirView->DiscardSavedSelection();
  3451. throw;
  3452. }
  3453. RemoteDirView->RestoreSelection();
  3454. }
  3455. //---------------------------------------------------------------------------
  3456. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TTerminal *& Session,
  3457. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3458. bool NoConfirmation, bool Move)
  3459. {
  3460. if (RemoteDriveView->DropTarget != NULL)
  3461. {
  3462. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3463. }
  3464. else if (RemoteDirView->DropTarget != NULL)
  3465. {
  3466. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3467. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3468. }
  3469. else
  3470. {
  3471. Target = RemoteDirView->Path;
  3472. }
  3473. if (Session == NULL)
  3474. {
  3475. Session = TTerminalManager::Instance()->ActiveTerminal;
  3476. }
  3477. Target = UnixIncludeTrailingBackslash(Target);
  3478. if (FileList->Count == 1)
  3479. {
  3480. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3481. }
  3482. else
  3483. {
  3484. FileMask = AnyMask;
  3485. }
  3486. DirectCopy = FTerminal->IsCapable[fcRemoteCopy] || FTerminal->IsCapable[fcSecondaryShell];
  3487. bool Result = true;
  3488. if (!NoConfirmation)
  3489. {
  3490. bool Multi = (FileList->Count > 1);
  3491. if (Move)
  3492. {
  3493. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3494. }
  3495. else
  3496. {
  3497. DebugAssert(Terminal != NULL);
  3498. // update Terminal->StateData->RemoteDirectory
  3499. UpdateTerminal(Terminal);
  3500. TStrings * Sessions = TTerminalManager::Instance()->TerminalList;
  3501. TStrings * Directories = new TStringList;
  3502. try
  3503. {
  3504. for (int Index = 0; Index < Sessions->Count; Index++)
  3505. {
  3506. TManagedTerminal * Terminal =
  3507. dynamic_cast<TManagedTerminal *>(Sessions->Objects[Index]);
  3508. Directories->Add(Terminal->StateData->RemoteDirectory);
  3509. }
  3510. TDirectRemoteCopy AllowDirectCopy;
  3511. if (FTerminal->IsCapable[fcRemoteCopy] || FTerminal->CommandSessionOpened)
  3512. {
  3513. DebugAssert(DirectCopy);
  3514. AllowDirectCopy = drcAllow;
  3515. }
  3516. else if (FTerminal->IsCapable[fcSecondaryShell])
  3517. {
  3518. DebugAssert(DirectCopy);
  3519. AllowDirectCopy = drcConfirmCommandSession;
  3520. }
  3521. else
  3522. {
  3523. DebugAssert(!DirectCopy);
  3524. AllowDirectCopy = drcDisallow;
  3525. }
  3526. void * ASession = Session;
  3527. Result = DoRemoteCopyDialog(Sessions, Directories, AllowDirectCopy,
  3528. Multi, ASession, Target, FileMask, DirectCopy, TTerminalManager::Instance()->ActiveTerminal);
  3529. Session = static_cast<TTerminal *>(ASession);
  3530. }
  3531. __finally
  3532. {
  3533. delete Directories;
  3534. }
  3535. }
  3536. }
  3537. return Result;
  3538. }
  3539. //---------------------------------------------------------------------------
  3540. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3541. TStrings * FileList, bool NoConfirmation, bool Move, TTerminal * Session)
  3542. {
  3543. bool DirectCopy;
  3544. UnicodeString Target, FileMask;
  3545. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3546. if (Result)
  3547. {
  3548. if (!Move && !DirectCopy)
  3549. {
  3550. Configuration->Usage->Inc("RemoteCopyTemp");
  3551. UnicodeString RootTempDir;
  3552. UnicodeString TempDir;
  3553. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3554. TStrings * TemporaryFilesList = new TStringList();
  3555. try
  3556. {
  3557. TMakeLocalFileListParams MakeFileListParam;
  3558. MakeFileListParam.FileList = TemporaryFilesList;
  3559. MakeFileListParam.FileTimes = NULL;
  3560. MakeFileListParam.IncludeDirs = true;
  3561. MakeFileListParam.Recursive = false;
  3562. ProcessLocalDirectory(TempDir, FTerminal->MakeLocalFileList, &MakeFileListParam);
  3563. TTerminalManager::Instance()->ActiveTerminal = Session;
  3564. if (TemporaryFilesList->Count > 0)
  3565. {
  3566. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3567. CopyParam.FileMask = FileMask;
  3568. DebugAssert(!FAutoOperation);
  3569. FAutoOperation = true;
  3570. FTerminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3571. }
  3572. }
  3573. __finally
  3574. {
  3575. delete TemporaryFilesList;
  3576. FAutoOperation = false;
  3577. if (!RootTempDir.IsEmpty())
  3578. {
  3579. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3580. }
  3581. }
  3582. }
  3583. else
  3584. {
  3585. RemoteDirView->SaveSelection();
  3586. RemoteDirView->SaveSelectedNames();
  3587. try
  3588. {
  3589. if (Move)
  3590. {
  3591. Configuration->Usage->Inc("RemoteMove");
  3592. Terminal->MoveFiles(FileList, Target, FileMask);
  3593. }
  3594. else
  3595. {
  3596. Configuration->Usage->Inc("RemoteCopyDirect");
  3597. DebugAssert(DirectCopy);
  3598. DebugAssert(Session == FTerminal);
  3599. if (FTerminal->IsCapable[fcRemoteCopy] ||
  3600. FTerminal->CommandSessionOpened ||
  3601. CommandSessionFallback())
  3602. {
  3603. Terminal->CopyFiles(FileList, Target, FileMask);
  3604. }
  3605. }
  3606. }
  3607. catch(...)
  3608. {
  3609. RemoteDirView->DiscardSavedSelection();
  3610. throw;
  3611. }
  3612. RemoteDirView->RestoreSelection();
  3613. }
  3614. }
  3615. return Result;
  3616. }
  3617. //---------------------------------------------------------------------------
  3618. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3619. {
  3620. Side = GetSide(Side);
  3621. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3622. TRemoteProperties * AProperties = (Side == osRemote ? &Properties : NULL);
  3623. UnicodeString Name = LoadStr(NEW_FOLDER);
  3624. int AllowedChanges =
  3625. FLAGMASK(Terminal->IsCapable[fcModeChanging], cpMode);
  3626. bool SaveSettings = false;
  3627. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  3628. {
  3629. TWindowLock Lock(this);
  3630. if (Side == osRemote)
  3631. {
  3632. if (SaveSettings)
  3633. {
  3634. GUIConfiguration->NewDirectoryProperties = Properties;
  3635. }
  3636. RemoteDirView->CreateDirectoryEx(Name, &Properties);
  3637. }
  3638. else
  3639. {
  3640. DirView(Side)->CreateDirectory(Name);
  3641. }
  3642. }
  3643. }
  3644. //---------------------------------------------------------------------------
  3645. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  3646. {
  3647. TWindowLock Lock(this);
  3648. DirView(Side)->ExecuteHomeDirectory();
  3649. }
  3650. //---------------------------------------------------------------------------
  3651. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  3652. {
  3653. DoOpenDirectoryDialog(odBrowse, Side);
  3654. }
  3655. //---------------------------------------------------------------------------
  3656. bool __fastcall TCustomScpExplorerForm::OpenBookmark(UnicodeString Local, UnicodeString Remote)
  3657. {
  3658. UnicodeString Path;
  3659. if (FCurrentSide == osRemote)
  3660. {
  3661. Path = Remote;
  3662. }
  3663. else
  3664. {
  3665. Path = Local;
  3666. }
  3667. bool Result = !Path.IsEmpty();
  3668. if (Result)
  3669. {
  3670. // While we might get here when the session is closed (from location profiles),
  3671. // it's not a problem as the Path setter is noop then
  3672. DirView(FCurrentSide)->Path = Path;
  3673. }
  3674. return Result;
  3675. }
  3676. //---------------------------------------------------------------------------
  3677. void __fastcall TCustomScpExplorerForm::CalculateSize(
  3678. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  3679. bool & Close)
  3680. {
  3681. // terminal can be already closed (e.g. dropped connection)
  3682. if (Terminal != NULL)
  3683. {
  3684. try
  3685. {
  3686. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  3687. }
  3688. catch(...)
  3689. {
  3690. if (!Terminal->Active)
  3691. {
  3692. Close = true;
  3693. }
  3694. throw;
  3695. }
  3696. }
  3697. }
  3698. //---------------------------------------------------------------------------
  3699. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  3700. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  3701. bool & Close)
  3702. {
  3703. // terminal can be already closed (e.g. dropped connection)
  3704. if (Terminal != NULL)
  3705. {
  3706. Configuration->Usage->Inc(L"ChecksumCalculated");
  3707. try
  3708. {
  3709. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  3710. }
  3711. catch(...)
  3712. {
  3713. if (!Terminal->Active)
  3714. {
  3715. Close = true;
  3716. }
  3717. throw;
  3718. }
  3719. }
  3720. }
  3721. //---------------------------------------------------------------------------
  3722. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  3723. {
  3724. bool Result;
  3725. if (Side == osRemote)
  3726. {
  3727. TRemoteTokenList * GroupList = NULL;
  3728. TRemoteTokenList * UserList = NULL;
  3729. try
  3730. {
  3731. TRemoteProperties CurrentProperties;
  3732. if (Terminal->LoadFilesProperties(FileList))
  3733. {
  3734. RemoteDirView->Invalidate();
  3735. }
  3736. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  3737. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  3738. if (CapableGroupChanging || CapableOwnerChanging)
  3739. {
  3740. if (CapableGroupChanging)
  3741. {
  3742. GroupList = Terminal->Groups->Duplicate();
  3743. }
  3744. if (CapableOwnerChanging)
  3745. {
  3746. UserList = Terminal->Users->Duplicate();
  3747. }
  3748. TRemoteDirectory * Files = Terminal->Files;
  3749. int Count = Files->Count;
  3750. if (Count > 100)
  3751. {
  3752. Count = 100;
  3753. }
  3754. for (int Index = 0; Index < Count; Index++)
  3755. {
  3756. TRemoteFile * File = Files->Files[Index];
  3757. if (CapableGroupChanging)
  3758. {
  3759. GroupList->AddUnique(File->Group);
  3760. }
  3761. if (CapableOwnerChanging)
  3762. {
  3763. UserList->AddUnique(File->Owner);
  3764. }
  3765. }
  3766. // if we haven't collected tokens for all files in current directory,
  3767. // make sure we collect them at least for all selected files.
  3768. // (note that so far the files in FileList has to be from current direcotry)
  3769. if (Count < Files->Count)
  3770. {
  3771. for (int Index = 0; Index < FileList->Count; Index++)
  3772. {
  3773. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  3774. if (CapableGroupChanging)
  3775. {
  3776. GroupList->AddUnique(File->Group);
  3777. }
  3778. if (CapableOwnerChanging)
  3779. {
  3780. UserList->AddUnique(File->Owner);
  3781. }
  3782. }
  3783. }
  3784. }
  3785. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  3786. int Flags = 0;
  3787. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  3788. if (CapableOwnerChanging) Flags |= cpOwner;
  3789. if (CapableGroupChanging) Flags |= cpGroup;
  3790. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  3791. if (Terminal->IsCapable[fcCalculatingChecksum])
  3792. {
  3793. CalculateChecksumEvent = CalculateChecksum;
  3794. }
  3795. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  3796. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  3797. TRemoteProperties NewProperties = CurrentProperties;
  3798. Result =
  3799. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  3800. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  3801. Terminal->IsCapable[fcGroupOwnerChangingByID],
  3802. CalculateSize, CalculateChecksumEvent);
  3803. if (Result)
  3804. {
  3805. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  3806. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  3807. }
  3808. }
  3809. __finally
  3810. {
  3811. delete GroupList;
  3812. delete UserList;
  3813. }
  3814. }
  3815. else
  3816. {
  3817. DirView(Side)->DisplayPropertiesMenu();
  3818. Result = true;
  3819. }
  3820. return Result;
  3821. }
  3822. //---------------------------------------------------------------------------
  3823. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  3824. {
  3825. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  3826. {
  3827. FIgnoreNextDialogChar = Key;
  3828. }
  3829. Key = 0;
  3830. }
  3831. //---------------------------------------------------------------------------
  3832. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  3833. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  3834. {
  3835. const TCustomCommandType * Command = List->Find(KeyShortCut);
  3836. if (Command != NULL)
  3837. {
  3838. KeyProcessed(Key, Shift);
  3839. if (CustomCommandState(*Command, false, ccltAll) > 0)
  3840. {
  3841. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  3842. false, false, const_cast<TCustomCommandType *>(Command));
  3843. }
  3844. }
  3845. }
  3846. //---------------------------------------------------------------------------
  3847. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  3848. {
  3849. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  3850. {
  3851. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  3852. }
  3853. if (!DirView(osCurrent)->IsEditing())
  3854. {
  3855. TShortCut KeyShortCut = ShortCut(Key, Shift);
  3856. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  3857. {
  3858. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  3859. if (((Action->ShortCut == KeyShortCut) ||
  3860. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  3861. AllowedAction(Action, aaShortCut))
  3862. {
  3863. // Has to be called before the action as the dialog char is already in queue.
  3864. // So when the action consumes message queue, we already need to have the
  3865. // FIgnoreNextDialogChar set
  3866. KeyProcessed(Key, Shift);
  3867. // Reset reference to previous component (when menu/toolbar was clicked).
  3868. // Needed to detect that action was invoked by keyboard shortcut
  3869. // in TNonVisualDataModule::ExplorerActionsExecute
  3870. Action->ActionComponent = NULL;
  3871. Action->Execute();
  3872. return;
  3873. }
  3874. }
  3875. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  3876. {
  3877. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  3878. {
  3879. KeyProcessed(Key, Shift);
  3880. TTerminalManager::Instance()->ActiveTerminalIndex = i;
  3881. return;
  3882. }
  3883. }
  3884. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  3885. {
  3886. KeyProcessed(Key, Shift);
  3887. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  3888. }
  3889. if (IsCustomShortCut(KeyShortCut))
  3890. {
  3891. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, KeyShortCut);
  3892. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, KeyShortCut);
  3893. if (WinConfiguration->SharedBookmarks != NULL)
  3894. {
  3895. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(KeyShortCut);
  3896. if ((Bookmark != NULL) &&
  3897. OpenBookmark(Bookmark->Local, Bookmark->Remote))
  3898. {
  3899. KeyProcessed(Key, Shift);
  3900. }
  3901. }
  3902. }
  3903. }
  3904. TForm::KeyDown(Key, Shift);
  3905. }
  3906. //---------------------------------------------------------------------------
  3907. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  3908. {
  3909. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  3910. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  3911. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  3912. DebugAssert(Terminal);
  3913. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  3914. bool SecurityEnabled = !SessionInfo.SecurityProtocolName.IsEmpty();
  3915. SessionStatusBar->Panels->Items[Offset + 0]->Enabled = SecurityEnabled;
  3916. // expanded from ?: to avoid memory leaks
  3917. if (SecurityEnabled)
  3918. {
  3919. SessionStatusBar->Panels->Items[Offset + 0]->Hint =
  3920. FMTLOAD(STATUS_SECURE, (SessionInfo.SecurityProtocolName));
  3921. }
  3922. else
  3923. {
  3924. SessionStatusBar->Panels->Items[Offset + 0]->Hint = LoadStr(STATUS_INSECURE);
  3925. }
  3926. if (FileSystemInfo.ProtocolName.IsEmpty())
  3927. {
  3928. SessionStatusBar->Panels->Items[Offset + 1]->Caption = SessionInfo.ProtocolName;
  3929. }
  3930. else
  3931. {
  3932. SessionStatusBar->Panels->Items[Offset + 1]->Caption = FileSystemInfo.ProtocolName;
  3933. }
  3934. SessionStatusBar->Panels->Items[Offset + 1]->Hint = LoadStr(STATUS_PROTOCOL_HINT);
  3935. SessionStatusBar->Panels->Items[Offset + 2]->Enabled =
  3936. (!SessionInfo.CSCompression.IsEmpty() || !SessionInfo.SCCompression.IsEmpty());
  3937. if (SessionInfo.CSCompression == SessionInfo.SCCompression)
  3938. {
  3939. SessionStatusBar->Panels->Items[Offset + 2]->Hint =
  3940. FMTLOAD(STATUS_COMPRESSION_HINT, (DefaultStr(SessionInfo.CSCompression, LoadStr(NO_STR))));
  3941. }
  3942. else
  3943. {
  3944. SessionStatusBar->Panels->Items[Offset + 2]->Hint = FMTLOAD(STATUS_COMPRESSION2_HINT,
  3945. (DefaultStr(SessionInfo.CSCompression, LoadStr(NO_STR)),
  3946. DefaultStr(SessionInfo.SCCompression, LoadStr(NO_STR))));
  3947. }
  3948. SessionStatusBar->Panels->Items[Offset + 3]->Hint = LoadStr(STATUS_DURATION_HINT);
  3949. UpdateStatusBar();
  3950. }
  3951. //---------------------------------------------------------------------------
  3952. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  3953. {
  3954. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  3955. DebugAssert(SessionStatusBar != NULL);
  3956. if (!Terminal || !Terminal->Active || Terminal->Status < ssOpened)
  3957. {
  3958. // note: (Terminal->Status < sshReady) currently never happens here,
  3959. // so STATUS_CONNECTING is never used
  3960. SessionStatusBar->SimplePanel = true;
  3961. SessionStatusBar->SimpleText = LoadStr(
  3962. !Terminal || !Terminal->Active ? STATUS_NOT_CONNECTED : STATUS_CONNECTING);
  3963. }
  3964. else
  3965. {
  3966. DebugAssert(Terminal);
  3967. SessionStatusBar->SimplePanel = false;
  3968. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  3969. if (!FNote.IsEmpty())
  3970. {
  3971. SessionStatusBar->Panels->Items[0]->Caption = FNote;
  3972. }
  3973. else
  3974. {
  3975. UpdateStatusPanelText(SessionStatusBar->Panels->Items[0]);
  3976. }
  3977. SessionStatusBar->Panels->Items[0]->Hint = FNoteHints;
  3978. SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1]->Caption =
  3979. FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  3980. }
  3981. }
  3982. //---------------------------------------------------------------------------
  3983. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  3984. {
  3985. Panel->Caption = L"";
  3986. }
  3987. //---------------------------------------------------------------------------
  3988. void __fastcall TCustomScpExplorerForm::Idle()
  3989. {
  3990. if (FShowing || FStandaloneEditing)
  3991. {
  3992. FEditorManager->Check();
  3993. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  3994. // that needs to know if queue view is visible (and it may be closed after queue update)
  3995. TTerminalManager::Instance()->Idle();
  3996. }
  3997. if (FShowing)
  3998. {
  3999. if (!NonVisualDataModule->Busy &&
  4000. // Menu is opened or drag&drop is going on
  4001. (Mouse->Capture == NULL))
  4002. {
  4003. if (FRefreshRemoteDirectory)
  4004. {
  4005. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4006. {
  4007. Terminal->RefreshDirectory();
  4008. }
  4009. FRefreshRemoteDirectory = false;
  4010. }
  4011. if (FRefreshLocalDirectory)
  4012. {
  4013. ReloadLocalDirectory();
  4014. FRefreshLocalDirectory = false;
  4015. }
  4016. if (WinConfiguration->RefreshRemotePanel)
  4017. {
  4018. TManagedTerminal * ManagedTerminal =
  4019. dynamic_cast<TManagedTerminal *>(Terminal);
  4020. if ((ManagedTerminal != NULL) && (Terminal->Status == ssOpened) &&
  4021. (Now() - ManagedTerminal->DirectoryLoaded >
  4022. WinConfiguration->RefreshRemotePanelInterval))
  4023. {
  4024. RemoteDirView->ReloadDirectory();
  4025. }
  4026. }
  4027. }
  4028. }
  4029. if (FShowing || FStandaloneEditing)
  4030. {
  4031. if (FQueueStatusInvalidated)
  4032. {
  4033. UpdateQueueStatus(false);
  4034. }
  4035. RefreshQueueItems();
  4036. }
  4037. if (FShowing)
  4038. {
  4039. UpdateStatusBar();
  4040. }
  4041. FIgnoreNextDialogChar = 0;
  4042. }
  4043. //---------------------------------------------------------------------------
  4044. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4045. {
  4046. try
  4047. {
  4048. FUserActionTimer->Enabled = false;
  4049. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4050. {
  4051. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4052. {
  4053. FPendingQueueActionItem->ProcessUserAction();
  4054. }
  4055. }
  4056. }
  4057. __finally
  4058. {
  4059. FPendingQueueActionItem = NULL;
  4060. }
  4061. }
  4062. //---------------------------------------------------------------------------
  4063. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4064. {
  4065. if (WinConfiguration->MinimizeToTray)
  4066. {
  4067. UpdateTrayIcon();
  4068. FTrayIcon->Visible = true;
  4069. if (Visible)
  4070. {
  4071. ShowWindow(Handle, SW_HIDE);
  4072. }
  4073. }
  4074. }
  4075. //---------------------------------------------------------------------------
  4076. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4077. {
  4078. // WORKAROUND
  4079. // When restoring maximized window from minimization,
  4080. // rarely some controls do not align properly.
  4081. // Two instances seen (both for Commander):
  4082. // - When restoring, window is temporarily narrower (not maximized),
  4083. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4084. // Once maximized already, top dock shinks back, but the session PageControl,
  4085. // do not align up, leaving space between TopDock and PageControl.
  4086. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4087. for (int Index = 0; Index < ControlCount; Index++)
  4088. {
  4089. RealignControl(Controls[Index]);
  4090. }
  4091. if (FTrayIcon->Visible)
  4092. {
  4093. FTrayIcon->Visible = false;
  4094. if (Visible)
  4095. {
  4096. ShowWindow(Handle, SW_SHOW);
  4097. }
  4098. }
  4099. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4100. {
  4101. FNeedSession = false;
  4102. NonVisualDataModule->StartBusy();
  4103. try
  4104. {
  4105. NeedSession(false);
  4106. }
  4107. __finally
  4108. {
  4109. NonVisualDataModule->EndBusy();
  4110. }
  4111. }
  4112. }
  4113. //---------------------------------------------------------------------------
  4114. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4115. {
  4116. FTrayIcon->Hint = Caption;
  4117. }
  4118. //---------------------------------------------------------------------------
  4119. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4120. {
  4121. UpdateTrayIcon();
  4122. }
  4123. //---------------------------------------------------------------------------
  4124. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4125. {
  4126. // workaround
  4127. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4128. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4129. // But WM_ACTIVATEAPP message can be received even when
  4130. // the main window is minimized to the tray and internal editor window is focused
  4131. // (after another application was previously active)
  4132. if (::IsIconic(Handle))
  4133. {
  4134. if (!IsAppIconic())
  4135. {
  4136. SetAppIconic(true);
  4137. }
  4138. }
  4139. ::ApplicationRestore();
  4140. Application->BringToFront();
  4141. }
  4142. //---------------------------------------------------------------------------
  4143. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4144. {
  4145. RestoreApp();
  4146. }
  4147. //---------------------------------------------------------------------------
  4148. void __fastcall TCustomScpExplorerForm::NewSession(bool FromSite, const UnicodeString & SessionUrl)
  4149. {
  4150. if (OpenInNewWindow())
  4151. {
  4152. // todo: Pass FromSite
  4153. ExecuteNewInstance(SessionUrl);
  4154. }
  4155. else
  4156. {
  4157. TTerminalManager::Instance()->NewSession(FromSite, SessionUrl);
  4158. }
  4159. }
  4160. //---------------------------------------------------------------------------
  4161. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4162. {
  4163. // current working directories become defaults here, what is not right
  4164. TSessionData * SessionData = CloneCurrentSessionData();
  4165. try
  4166. {
  4167. if (OpenInNewWindow())
  4168. {
  4169. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->Name;
  4170. StoredSessions->NewSession(SessionName, SessionData);
  4171. // modified only, explicit
  4172. StoredSessions->Save(false, true);
  4173. // encode session name because of slashes in hierarchical sessions
  4174. ExecuteNewInstance(EncodeUrlString(SessionName));
  4175. }
  4176. else
  4177. {
  4178. TTerminalManager * Manager = TTerminalManager::Instance();
  4179. TTerminal * Terminal = Manager->NewTerminal(SessionData);
  4180. Manager->ActiveTerminal = Terminal;
  4181. }
  4182. }
  4183. __finally
  4184. {
  4185. delete SessionData;
  4186. }
  4187. }
  4188. //---------------------------------------------------------------------------
  4189. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4190. {
  4191. DebugAssert(FQueue != NULL);
  4192. bool Result = FQueue->IsEmpty;
  4193. if (!Result)
  4194. {
  4195. SetFocus();
  4196. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4197. }
  4198. return Result;
  4199. }
  4200. //---------------------------------------------------------------------------
  4201. void __fastcall TCustomScpExplorerForm::CloseSession()
  4202. {
  4203. if (CanCloseQueue())
  4204. {
  4205. TTerminalManager::Instance()->FreeActiveTerminal();
  4206. }
  4207. }
  4208. //---------------------------------------------------------------------------
  4209. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4210. {
  4211. if (OpenInNewWindow())
  4212. {
  4213. // encode session name because of slashes in hierarchical sessions
  4214. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4215. }
  4216. else
  4217. {
  4218. TTerminalManager * Manager = TTerminalManager::Instance();
  4219. TTerminal * Terminal = Manager->NewTerminal(Data);
  4220. Manager->ActiveTerminal = Terminal;
  4221. }
  4222. }
  4223. //---------------------------------------------------------------------------
  4224. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4225. {
  4226. if (OpenInNewWindow())
  4227. {
  4228. ExecuteNewInstance(Name);
  4229. }
  4230. else
  4231. {
  4232. TTerminalManager * Manager = TTerminalManager::Instance();
  4233. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4234. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4235. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  4236. }
  4237. }
  4238. //---------------------------------------------------------------------------
  4239. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4240. bool &CanClose)
  4241. {
  4242. if (Terminal != NULL)
  4243. {
  4244. if (Terminal->Active && WinConfiguration->ConfirmClosingSession)
  4245. {
  4246. unsigned int Result;
  4247. TMessageParams Params(mpNeverAskAgainCheck);
  4248. UnicodeString Message;
  4249. int Answers = qaOK | qaCancel;
  4250. if (TTerminalManager::Instance()->Count > 1)
  4251. {
  4252. if (!WinConfiguration->AutoSaveWorkspace)
  4253. {
  4254. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE2);
  4255. Answers = qaYes | qaNo | qaCancel;
  4256. }
  4257. else
  4258. {
  4259. Message = MainInstructions(LoadStr(CLOSE_SESSIONS));
  4260. }
  4261. }
  4262. else
  4263. {
  4264. Message = MainInstructions(FMTLOAD(CLOSE_SESSION, (Terminal->SessionData->SessionName)));
  4265. }
  4266. if (WinConfiguration->AutoSaveWorkspace)
  4267. {
  4268. Message =
  4269. FORMAT("%s\n\n%s", (Message,
  4270. FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()))));
  4271. }
  4272. SetFocus();
  4273. Result = MessageDialog(Message, qtConfirmation,
  4274. Answers, HELP_NONE, &Params);
  4275. if (Result == qaNeverAskAgain)
  4276. {
  4277. WinConfiguration->ConfirmClosingSession = false;
  4278. }
  4279. if (Result == qaNo)
  4280. {
  4281. CanClose = SaveWorkspace(true);
  4282. // note that the workspace will be saved redundatly again from FormClose
  4283. }
  4284. else
  4285. {
  4286. CanClose =
  4287. (Result == qaOK) ||
  4288. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4289. (Result == qaNeverAskAgain);
  4290. }
  4291. }
  4292. if (CanClose)
  4293. {
  4294. CanClose = CanCloseQueue();
  4295. }
  4296. }
  4297. if (CanClose)
  4298. {
  4299. CanClose =
  4300. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4301. FEditorManager->CloseExternalFilesWithoutProcess();
  4302. if (CanClose)
  4303. {
  4304. while (CanClose && (FLocalEditors->Count > 0))
  4305. {
  4306. int PrevCount = FLocalEditors->Count;
  4307. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4308. CanClose = (FLocalEditors->Count < PrevCount);
  4309. }
  4310. if (CanClose)
  4311. {
  4312. CanClose = FEditorManager->Empty(true);
  4313. if (!CanClose)
  4314. {
  4315. SetFocus();
  4316. CanClose =
  4317. (MessageDialog(
  4318. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4319. }
  4320. }
  4321. }
  4322. }
  4323. }
  4324. //---------------------------------------------------------------------------
  4325. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4326. {
  4327. TForm * Form = dynamic_cast<TForm *>(Sender);
  4328. DebugAssert(Form != NULL);
  4329. Form->Close();
  4330. }
  4331. //---------------------------------------------------------------------------
  4332. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4333. {
  4334. TForm * Form = dynamic_cast<TForm *>(Sender);
  4335. delete Form;
  4336. }
  4337. //---------------------------------------------------------------------------
  4338. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4339. {
  4340. while (FLocalEditors->Count > 0)
  4341. {
  4342. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4343. }
  4344. }
  4345. //---------------------------------------------------------------------------
  4346. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4347. TObject *Sender)
  4348. {
  4349. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4350. try
  4351. {
  4352. SetProperties(osRemote, FileList);
  4353. }
  4354. __finally
  4355. {
  4356. delete FileList;
  4357. }
  4358. }
  4359. //---------------------------------------------------------------------------
  4360. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4361. {
  4362. if (value)
  4363. {
  4364. if (Component == fcCustomCommandsBand)
  4365. {
  4366. UpdateCustomCommandsToolbar();
  4367. }
  4368. }
  4369. }
  4370. //---------------------------------------------------------------------------
  4371. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4372. {
  4373. TControl * Control = GetComponent(Component);
  4374. DebugAssert(Control);
  4375. bool Changed = (Control->Visible != value);
  4376. if (Changed)
  4377. {
  4378. ComponentShowing(Component, value);
  4379. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4380. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4381. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4382. if (value)
  4383. {
  4384. int RemainingHeight = Control->Parent->ClientHeight;
  4385. int RemainingWidth = Control->Parent->ClientWidth;
  4386. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4387. {
  4388. TControl * ChildControl = Control->Parent->Controls[i];
  4389. if (ChildControl->Visible)
  4390. {
  4391. switch (ChildControl->Align)
  4392. {
  4393. case alTop:
  4394. case alBottom:
  4395. RemainingHeight -= ChildControl->Height;
  4396. break;
  4397. case alLeft:
  4398. case alRight:
  4399. RemainingWidth -= ChildControl->Width;
  4400. break;
  4401. }
  4402. }
  4403. }
  4404. int Reserve = ScaleByTextHeight(this, 32);
  4405. // queue in explorer, trees in commander
  4406. if (Control->Height > RemainingHeight - Reserve)
  4407. {
  4408. Control->Height = RemainingHeight / 2;
  4409. }
  4410. if (Control->Width > RemainingWidth - Reserve)
  4411. {
  4412. Control->Width = RemainingWidth / 2;
  4413. }
  4414. }
  4415. Control->Visible = value;
  4416. if (WasFocused && Visible)
  4417. {
  4418. DirView(osCurrent)->SetFocus();
  4419. }
  4420. FixControlsPlacement();
  4421. }
  4422. }
  4423. //---------------------------------------------------------------------------
  4424. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4425. {
  4426. TControl * Control = GetComponent(Component);
  4427. if (Control == NULL)
  4428. {
  4429. return false;
  4430. }
  4431. else
  4432. {
  4433. return Control->Visible;
  4434. }
  4435. }
  4436. //---------------------------------------------------------------------------
  4437. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4438. {
  4439. if (RemoteDirView->ItemFocused != NULL)
  4440. {
  4441. RemoteDirView->ItemFocused->MakeVisible(false);
  4442. }
  4443. QueueSplitter->Visible = QueuePanel->Visible;
  4444. RemotePanelSplitter->Visible = RemoteDriveView->Visible;
  4445. }
  4446. //---------------------------------------------------------------------------
  4447. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4448. {
  4449. switch (Component) {
  4450. case fcStatusBar: return RemoteStatusBar;
  4451. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4452. case fcQueueView: return QueuePanel;
  4453. case fcQueueToolbar: return QueueDock;
  4454. case fcRemoteTree: return RemoteDriveView;
  4455. case fcSessionsTabs: return SessionsPageControl;
  4456. default: return NULL;
  4457. }
  4458. }
  4459. //---------------------------------------------------------------------------
  4460. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4461. TObject *Sender, TListColumn *Column, TPoint &Point)
  4462. {
  4463. DebugAssert(NonVisualDataModule && Column && Sender);
  4464. NonVisualDataModule->ListColumn = Column;
  4465. TPoint ScreenPoint = ((TControl*)Sender)->ClientToScreen(Point);
  4466. TPopupMenu * DirViewColumnMenu;
  4467. if (Sender == RemoteDirView)
  4468. {
  4469. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4470. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4471. (Column->Index == uvName);
  4472. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4473. (Column->Index == uvSize);
  4474. }
  4475. else
  4476. {
  4477. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4478. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4479. (Column->Index == dvName);
  4480. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4481. (Column->Index == dvSize);
  4482. }
  4483. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4484. }
  4485. //---------------------------------------------------------------------------
  4486. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4487. TObject *Sender, TListItem *Item, bool &AllowExec)
  4488. {
  4489. DoDirViewExecFile(Sender, Item, AllowExec);
  4490. }
  4491. //---------------------------------------------------------------------------
  4492. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4493. TListItem * Item, bool & AllowExec)
  4494. {
  4495. DebugAssert(Sender && Item && Configuration);
  4496. DebugAssert(AllowExec);
  4497. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  4498. bool Remote = (ADirView == DirView(osRemote));
  4499. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks;
  4500. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  4501. // on files only (not directories)
  4502. // and only when symlinks are resolved (apply to remote panel only)
  4503. if (!ADirView->ItemIsDirectory(Item) &&
  4504. (ResolvedSymlinks || FForceExecution))
  4505. {
  4506. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  4507. !FForceExecution &&
  4508. ResolvedSymlinks)
  4509. {
  4510. if (WinConfiguration->DoubleClickAction == dcaCopy)
  4511. {
  4512. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  4513. UnlockWindow();
  4514. try
  4515. {
  4516. ExecuteFileOperation(foCopy,
  4517. (ADirView == DirView(osRemote) ? osRemote : osLocal),
  4518. true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  4519. }
  4520. __finally
  4521. {
  4522. LockWindow();
  4523. }
  4524. AllowExec = false;
  4525. }
  4526. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  4527. {
  4528. if (!Remote || !WinConfiguration->DisableOpenEdit)
  4529. {
  4530. ExecuteFile(osCurrent, efDefaultEditor);
  4531. AllowExec = false;
  4532. }
  4533. }
  4534. else
  4535. {
  4536. DebugFail();
  4537. }
  4538. }
  4539. // if we have not done anything special, fall back to default behavior
  4540. if (AllowExec)
  4541. {
  4542. if (Remote && !WinConfiguration->DisableOpenEdit)
  4543. {
  4544. ExecuteFile(osRemote, efShell);
  4545. AllowExec = false;
  4546. }
  4547. }
  4548. }
  4549. }
  4550. //---------------------------------------------------------------------------
  4551. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  4552. {
  4553. return ((Side == osRemote) || (Side == osCurrent));
  4554. }
  4555. //---------------------------------------------------------------------------
  4556. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  4557. {
  4558. DebugFail();
  4559. }
  4560. //---------------------------------------------------------------------------
  4561. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  4562. {
  4563. DebugFail();
  4564. }
  4565. //---------------------------------------------------------------------------
  4566. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  4567. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, bool UseDefaults)
  4568. {
  4569. TSynchronizeParamType Params;
  4570. Params.LocalDirectory = LocalDirectory;
  4571. Params.RemoteDirectory = RemoteDirectory;
  4572. int UnusedParams =
  4573. (GUIConfiguration->SynchronizeParams &
  4574. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize));
  4575. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  4576. Params.Options = GUIConfiguration->SynchronizeOptions;
  4577. bool SaveSettings = false;
  4578. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  4579. &DoSynchronizeTooManyDirectories);
  4580. DebugAssert(FSynchronizeController == NULL);
  4581. FSynchronizeController = &Controller;
  4582. bool Result;
  4583. try
  4584. {
  4585. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4586. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  4587. int Options =
  4588. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  4589. DebugAssert(FOnFeedSynchronizeError == NULL);
  4590. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  4591. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  4592. FOnFeedSynchronizeError, UseDefaults);
  4593. if (Result)
  4594. {
  4595. if (SaveSettings)
  4596. {
  4597. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  4598. GUIConfiguration->SynchronizeOptions = Params.Options;
  4599. }
  4600. else
  4601. {
  4602. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  4603. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  4604. FLAGSET(Params.Options, soSynchronize))
  4605. {
  4606. GUIConfiguration->SynchronizeOptions =
  4607. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  4608. soSynchronize;
  4609. }
  4610. }
  4611. LocalDirectory = Params.LocalDirectory;
  4612. RemoteDirectory = Params.RemoteDirectory;
  4613. }
  4614. }
  4615. __finally
  4616. {
  4617. FSynchronizeController = NULL;
  4618. DebugAssert(FOnFeedSynchronizeError == NULL);
  4619. FOnFeedSynchronizeError = NULL;
  4620. }
  4621. return Result;
  4622. }
  4623. //---------------------------------------------------------------------------
  4624. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  4625. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  4626. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  4627. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  4628. TSynchronizeOptions * Options, bool Full)
  4629. {
  4630. if (Terminal->Status == ssOpened)
  4631. {
  4632. try
  4633. {
  4634. int PParams = Params.Params;
  4635. if (!Full)
  4636. {
  4637. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache |
  4638. TTerminal::spDelayProgress | TTerminal::spSubDirs;
  4639. }
  4640. else
  4641. {
  4642. // if keepuptodate is non-recursive,
  4643. // full sync before has to be non-recursive as well
  4644. if (FLAGCLEAR(Params.Options, soRecurse))
  4645. {
  4646. PParams |= TTerminal::spNoRecurse;
  4647. }
  4648. }
  4649. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  4650. PParams, Checklist, Options);
  4651. }
  4652. catch (Exception & E)
  4653. {
  4654. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  4655. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  4656. {
  4657. // noop, already logged in MoreMessageDialog
  4658. }
  4659. else
  4660. {
  4661. ShowExtendedExceptionEx(Terminal, &E);
  4662. throw;
  4663. }
  4664. }
  4665. }
  4666. }
  4667. //---------------------------------------------------------------------------
  4668. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  4669. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  4670. const UnicodeString ErrorStr)
  4671. {
  4672. if (!Directory.IsEmpty())
  4673. {
  4674. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  4675. }
  4676. else
  4677. {
  4678. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  4679. }
  4680. }
  4681. //---------------------------------------------------------------------------
  4682. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  4683. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  4684. {
  4685. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  4686. {
  4687. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  4688. }
  4689. else
  4690. {
  4691. TMessageParams Params(mpNeverAskAgainCheck);
  4692. unsigned int Result = MessageDialog(
  4693. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  4694. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  4695. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  4696. {
  4697. MaxDirectories *= 2;
  4698. if (Result == qaNeverAskAgain)
  4699. {
  4700. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  4701. }
  4702. }
  4703. else
  4704. {
  4705. Abort();
  4706. }
  4707. }
  4708. }
  4709. //---------------------------------------------------------------------------
  4710. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  4711. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  4712. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  4713. TSynchronizeOptions * Options)
  4714. {
  4715. DebugAssert(!FAutoOperation);
  4716. void * BatchStorage;
  4717. BatchStart(BatchStorage);
  4718. FAutoOperation = true;
  4719. bool AnyOperation = false;
  4720. TDateTime StartTime = Now();
  4721. TSynchronizeChecklist * AChecklist = NULL;
  4722. try
  4723. {
  4724. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  4725. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  4726. {
  4727. FSynchronizeProgressForm->Start();
  4728. }
  4729. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  4730. static_cast<TTerminal::TSynchronizeMode>(Mode),
  4731. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  4732. Options);
  4733. SAFE_DESTROY(FSynchronizeProgressForm);
  4734. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, false);
  4735. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  4736. {
  4737. FSynchronizeProgressForm->Start();
  4738. }
  4739. for (int Index = 0; !AnyOperation && (Index < AChecklist->Count); Index++)
  4740. {
  4741. AnyOperation = AChecklist->Item[Index]->Checked;
  4742. }
  4743. // No need to call if !AnyOperation
  4744. Terminal->SynchronizeApply(AChecklist, LocalDirectory, RemoteDirectory,
  4745. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory);
  4746. }
  4747. __finally
  4748. {
  4749. if (Checklist == NULL)
  4750. {
  4751. delete AChecklist;
  4752. }
  4753. else
  4754. {
  4755. *Checklist = AChecklist;
  4756. }
  4757. FAutoOperation = false;
  4758. SAFE_DESTROY(FSynchronizeProgressForm);
  4759. BatchEnd(BatchStorage);
  4760. ReloadLocalDirectory();
  4761. if (AnyOperation)
  4762. {
  4763. OperationComplete(StartTime);
  4764. }
  4765. }
  4766. }
  4767. //---------------------------------------------------------------------------
  4768. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  4769. {
  4770. // can be called from command line
  4771. return Visible &&
  4772. ((DirView(osRemote)->SelCount > 0) ||
  4773. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  4774. }
  4775. //---------------------------------------------------------------------------
  4776. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  4777. {
  4778. LogSynchronizeEvent(Terminal, Message);
  4779. }
  4780. //---------------------------------------------------------------------------
  4781. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  4782. int Params, TSynchronizeOptions & Options)
  4783. {
  4784. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  4785. {
  4786. Options.Filter = new TStringList();
  4787. Options.Filter->CaseSensitive = false;
  4788. Options.Filter->Duplicates = Types::dupAccept;
  4789. if (DirView(osRemote)->SelCount > 0)
  4790. {
  4791. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  4792. }
  4793. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  4794. {
  4795. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  4796. }
  4797. Options.Filter->Sort();
  4798. }
  4799. }
  4800. //---------------------------------------------------------------------------
  4801. bool __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  4802. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  4803. TSynchronizeMode & Mode, bool & SaveMode, bool UseDefaults)
  4804. {
  4805. bool Result;
  4806. int Params = GUIConfiguration->SynchronizeParams;
  4807. bool SaveSettings = false;
  4808. int Options =
  4809. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  4810. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  4811. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4812. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  4813. Result = UseDefaults ||
  4814. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  4815. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs);
  4816. if (Result)
  4817. {
  4818. Configuration->Usage->Inc(L"Synchronizations");
  4819. UpdateCopyParamCounters(CopyParam);
  4820. TSynchronizeOptions SynchronizeOptions;
  4821. GetSynchronizeOptions(Params, SynchronizeOptions);
  4822. if (SaveSettings)
  4823. {
  4824. GUIConfiguration->SynchronizeParams = Params;
  4825. }
  4826. else
  4827. {
  4828. SaveMode = false;
  4829. }
  4830. TDateTime StartTime = Now();
  4831. TSynchronizeChecklist * Checklist = NULL;
  4832. try
  4833. {
  4834. DebugAssert(!FAutoOperation);
  4835. FAutoOperation = true;
  4836. try
  4837. {
  4838. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  4839. FSynchronizeProgressForm->Start();
  4840. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  4841. static_cast<TTerminal::TSynchronizeMode>(Mode),
  4842. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  4843. &SynchronizeOptions);
  4844. }
  4845. __finally
  4846. {
  4847. FAutoOperation = false;
  4848. SAFE_DESTROY(FSynchronizeProgressForm);
  4849. }
  4850. if (Checklist->Count == 0)
  4851. {
  4852. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  4853. MessageDialog(Message, qtInformation, qaOK,
  4854. HELP_SYNCHRONIZE_NO_DIFFERENCES);
  4855. }
  4856. else if (FLAGCLEAR(Params, TTerminal::spPreviewChanges) ||
  4857. DoSynchronizeChecklistDialog(Checklist, Mode, Params,
  4858. LocalDirectory, RemoteDirectory, CustomCommandMenu))
  4859. {
  4860. DebugAssert(!FAutoOperation);
  4861. void * BatchStorage;
  4862. BatchStart(BatchStorage);
  4863. FAutoOperation = true;
  4864. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  4865. {
  4866. StartTime = Now();
  4867. }
  4868. try
  4869. {
  4870. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, false);
  4871. FSynchronizeProgressForm->Start();
  4872. Terminal->SynchronizeApply(Checklist, LocalDirectory, RemoteDirectory,
  4873. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory);
  4874. }
  4875. __finally
  4876. {
  4877. FAutoOperation = false;
  4878. SAFE_DESTROY(FSynchronizeProgressForm);
  4879. BatchEnd(BatchStorage);
  4880. ReloadLocalDirectory();
  4881. }
  4882. }
  4883. }
  4884. __finally
  4885. {
  4886. delete Checklist;
  4887. }
  4888. OperationComplete(StartTime);
  4889. }
  4890. return Result;
  4891. }
  4892. //---------------------------------------------------------------------------
  4893. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  4894. const UnicodeString LocalDirectory, const UnicodeString RemoteDirectory,
  4895. bool & Continue, bool /*Collect*/)
  4896. {
  4897. DebugAssert(FSynchronizeProgressForm != NULL);
  4898. if (!FSynchronizeProgressForm->Started)
  4899. {
  4900. FSynchronizeProgressForm->Start();
  4901. }
  4902. FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Continue);
  4903. }
  4904. //---------------------------------------------------------------------------
  4905. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  4906. {
  4907. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  4908. TRemoteFile * File = NULL;
  4909. Terminal->ReadFile(FullFileName, File);
  4910. if (File != NULL)
  4911. {
  4912. std::unique_ptr<TRemoteFile> FileOwner(File);
  4913. TAutoFlag Flag(FStandaloneEditing);
  4914. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  4915. Application->ShowMainForm = false;
  4916. Application->Run();
  4917. }
  4918. }
  4919. //---------------------------------------------------------------------------
  4920. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  4921. {
  4922. DebugFail();
  4923. }
  4924. //---------------------------------------------------------------------------
  4925. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  4926. {
  4927. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  4928. SessionData->Assign(Terminal->SessionData);
  4929. UpdateSessionData(SessionData.get());
  4930. TTerminalManager::Instance()->UpdateSessionCredentials(SessionData.get());
  4931. if (Terminal->SessionData->IsWorkspace)
  4932. {
  4933. // Have to reset the "Workspace/XXX" name which would become user-visible
  4934. // once IsWorkspace is cleared
  4935. SessionData->Name = UnicodeString();
  4936. SessionData->IsWorkspace = false;
  4937. }
  4938. return SessionData.release();
  4939. }
  4940. //---------------------------------------------------------------------------
  4941. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  4942. {
  4943. TSessionData * SessionData = CloneCurrentSessionData();
  4944. try
  4945. {
  4946. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  4947. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  4948. }
  4949. __finally
  4950. {
  4951. delete SessionData;
  4952. }
  4953. }
  4954. //---------------------------------------------------------------------------
  4955. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  4956. {
  4957. TTerminalManager * Manager = TTerminalManager::Instance();
  4958. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4959. if (DebugAlwaysTrue(Terminal != NULL))
  4960. {
  4961. // Update (Managed)Terminal->StateData
  4962. UpdateTerminal(Terminal);
  4963. }
  4964. Manager->SaveWorkspace(DataList.get());
  4965. return DataList.release();
  4966. }
  4967. //---------------------------------------------------------------------------
  4968. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  4969. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords)
  4970. {
  4971. WinConfiguration->LastWorkspace = Name;
  4972. if (!SavePasswords)
  4973. {
  4974. for (int Index = 0; Index < DataList->Count; Index++)
  4975. {
  4976. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  4977. if (SessionData->Link.IsEmpty())
  4978. {
  4979. SessionData->ClearSessionPasswords();
  4980. }
  4981. }
  4982. }
  4983. StoredSessions->NewWorkspace(Name, DataList);
  4984. // modified only, explicit
  4985. StoredSessions->Save(false, true);
  4986. }
  4987. //---------------------------------------------------------------------------
  4988. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  4989. {
  4990. return
  4991. DefaultStr(WinConfiguration->LastWorkspace,
  4992. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  4993. }
  4994. //---------------------------------------------------------------------------
  4995. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  4996. {
  4997. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  4998. bool AnyNonStoredSessionWithPassword = false;
  4999. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5000. bool AllNonStoredSessionsAnonymous = true;
  5001. TTerminalManager * Manager = TTerminalManager::Instance();
  5002. for (int Index = 0; Index < DataList->Count; Index++)
  5003. {
  5004. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5005. if (SessionData->Link.IsEmpty())
  5006. {
  5007. if (SessionData->HasAnySessionPassword())
  5008. {
  5009. AnyNonStoredSessionWithPassword = true;
  5010. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  5011. {
  5012. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5013. }
  5014. }
  5015. if (!SameText(SessionData->UserName, AnonymousUserName))
  5016. {
  5017. AllNonStoredSessionsAnonymous = false;
  5018. }
  5019. }
  5020. }
  5021. bool SavePasswords;
  5022. bool * PSavePasswords;
  5023. bool NotRecommendedSavingPasswords =
  5024. !CustomWinConfiguration->UseMasterPassword &&
  5025. !AllNonStoredSessionsAnonymous;
  5026. if (Configuration->DisablePasswordStoring ||
  5027. !AnyNonStoredSessionWithPassword)
  5028. {
  5029. PSavePasswords = NULL;
  5030. SavePasswords = false;
  5031. }
  5032. else
  5033. {
  5034. PSavePasswords = &SavePasswords;
  5035. SavePasswords =
  5036. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5037. !NotRecommendedSavingPasswords;
  5038. }
  5039. UnicodeString Name = WorkspaceName();
  5040. bool CreateShortcut = false;
  5041. bool Result =
  5042. DoSaveWorkspaceDialog(
  5043. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5044. EnableAutoSave);
  5045. if (Result)
  5046. {
  5047. DoSaveWorkspace(Name, DataList.get(), SavePasswords);
  5048. if (CreateShortcut)
  5049. {
  5050. TOperationVisualizer Visualizer;
  5051. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5052. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5053. }
  5054. if (EnableAutoSave)
  5055. {
  5056. WinConfiguration->AutoSaveWorkspace = true;
  5057. WinConfiguration->AutoWorkspace = Name;
  5058. if (PSavePasswords != NULL)
  5059. {
  5060. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5061. }
  5062. }
  5063. }
  5064. return Result;
  5065. }
  5066. //---------------------------------------------------------------------------
  5067. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TTerminal * Terminal)
  5068. {
  5069. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  5070. DebugAssert(ManagedTerminal != NULL);
  5071. SAFE_DESTROY(ManagedTerminal->RemoteExplorerState);
  5072. if (WinConfiguration->PreservePanelState)
  5073. {
  5074. ManagedTerminal->RemoteExplorerState = RemoteDirView->SaveState();
  5075. }
  5076. UpdateSessionData(ManagedTerminal->StateData);
  5077. }
  5078. //---------------------------------------------------------------------------
  5079. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5080. {
  5081. // Keep in sync with TSessionData::CopyStateData
  5082. DebugAssert(Data != NULL);
  5083. // cannot use RemoteDirView->Path, because it is empty if connection
  5084. // was already closed
  5085. // also only peek, we may not be connected at all atm
  5086. // so make sure we do not try retrieving current directory from the server
  5087. // (particularly with FTP)
  5088. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5089. if (!ACurrentDirectory.IsEmpty())
  5090. {
  5091. Data->RemoteDirectory = ACurrentDirectory;
  5092. }
  5093. Data->Color = SessionColor;
  5094. }
  5095. //---------------------------------------------------------------------------
  5096. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5097. {
  5098. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5099. DebugAssert(Toolbar != NULL);
  5100. for (int i = 0; i < Toolbar->Items->Count; i++)
  5101. {
  5102. TTBXCustomDropDownItem * DropDownItem;
  5103. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5104. if (DropDownItem != NULL)
  5105. {
  5106. ToolbarItemResize(DropDownItem,
  5107. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5108. Toolbar->NonClientWidth);
  5109. break;
  5110. }
  5111. }
  5112. }
  5113. //---------------------------------------------------------------------------
  5114. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5115. {
  5116. Item->EditWidth = Width;
  5117. }
  5118. //---------------------------------------------------------------------------
  5119. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5120. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5121. {
  5122. for (int i = 0; i < Toolbar->Items->Count; i++)
  5123. {
  5124. TTBXCustomDropDownItem * DropDownItem;
  5125. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5126. if (DropDownItem != NULL)
  5127. {
  5128. ASize.x -= DropDownItem->EditWidth;
  5129. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5130. }
  5131. }
  5132. }
  5133. //---------------------------------------------------------------------------
  5134. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5135. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5136. {
  5137. if (WinConfiguration->DDWarnLackOfTempSpace)
  5138. {
  5139. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5140. if (!ADrive.IsEmpty())
  5141. {
  5142. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5143. DebugAssert(RequiredSpace >= 0);
  5144. __int64 RequiredWithReserve;
  5145. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5146. if (FreeSpace < RequiredWithReserve)
  5147. {
  5148. unsigned int Result;
  5149. TMessageParams Params(mpNeverAskAgainCheck);
  5150. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5151. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5152. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5153. if (Result == qaNeverAskAgain)
  5154. {
  5155. WinConfiguration->DDWarnLackOfTempSpace = false;
  5156. }
  5157. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5158. }
  5159. }
  5160. }
  5161. }
  5162. //---------------------------------------------------------------------------
  5163. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5164. {
  5165. DoOpenDirectoryDialog(odAddBookmark, Side);
  5166. }
  5167. //---------------------------------------------------------------------------
  5168. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5169. {
  5170. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5171. TStringList * VisitedDirectories = CreateSortedStringList((Side == osRemote));
  5172. try
  5173. {
  5174. TCustomDirView * DView = DirView(Side);
  5175. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5176. {
  5177. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5178. }
  5179. }
  5180. catch (...)
  5181. {
  5182. delete VisitedDirectories;
  5183. throw;
  5184. }
  5185. return VisitedDirectories;
  5186. }
  5187. //---------------------------------------------------------------------------
  5188. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5189. TOpenDirectoryMode Mode, TOperationSide Side)
  5190. {
  5191. bool Continue = true;
  5192. if (Mode == odAddBookmark)
  5193. {
  5194. TMessageParams Params(mpNeverAskAgainCheck);
  5195. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5196. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5197. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  5198. unsigned int Answer =
  5199. MessageDialog(Message,
  5200. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  5201. if (Answer == qaNeverAskAgain)
  5202. {
  5203. Continue = true;
  5204. WinConfiguration->UseSharedBookmarks = true;
  5205. }
  5206. else if (Answer == qaOK)
  5207. {
  5208. Continue = true;
  5209. WinConfiguration->UseSharedBookmarks = false;
  5210. }
  5211. else
  5212. {
  5213. Continue = false;
  5214. }
  5215. }
  5216. if (Continue)
  5217. {
  5218. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  5219. try
  5220. {
  5221. UnicodeString Name = DirView(Side)->PathName;
  5222. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  5223. // do not allow switching to location profiles,
  5224. // if we are not connected
  5225. HasDirView[osLocal] && (Terminal != NULL)))
  5226. {
  5227. TWindowLock Lock(this);
  5228. DirView(Side)->Path = Name;
  5229. }
  5230. }
  5231. __finally
  5232. {
  5233. delete VisitedDirectories;
  5234. }
  5235. }
  5236. }
  5237. //---------------------------------------------------------------------------
  5238. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  5239. {
  5240. DebugAssert(!FTerminal->CommandSessionOpened);
  5241. return TTerminalManager::Instance()->ConnectTerminal(FTerminal->CommandSession);
  5242. }
  5243. //---------------------------------------------------------------------------
  5244. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  5245. {
  5246. bool Result = FTerminal->IsCapable[Capability] ||
  5247. FTerminal->CommandSessionOpened;
  5248. if (!Result)
  5249. {
  5250. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  5251. if (!GUIConfiguration->ConfirmCommandSession)
  5252. {
  5253. Result = true;
  5254. }
  5255. else
  5256. {
  5257. TMessageParams Params(mpNeverAskAgainCheck);
  5258. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  5259. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  5260. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  5261. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  5262. if (Answer == qaNeverAskAgain)
  5263. {
  5264. GUIConfiguration->ConfirmCommandSession = false;
  5265. Result = true;
  5266. }
  5267. else if (Answer == qaOK)
  5268. {
  5269. Result = true;
  5270. }
  5271. }
  5272. if (Result)
  5273. {
  5274. Result = CommandSessionFallback();
  5275. }
  5276. }
  5277. return Result;
  5278. }
  5279. //---------------------------------------------------------------------------
  5280. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  5281. {
  5282. if (EnsureCommandSessionFallback(fcAnyCommand))
  5283. {
  5284. DoConsoleDialog(Terminal, Command);
  5285. }
  5286. }
  5287. //---------------------------------------------------------------------------
  5288. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  5289. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  5290. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  5291. {
  5292. if (IsFileControl(DropSourceControl, osRemote) &&
  5293. (FDDExtMapFile != NULL))
  5294. {
  5295. Accept = true;
  5296. }
  5297. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  5298. DebugAssert(FDDTargetControl != NULL);
  5299. }
  5300. //---------------------------------------------------------------------------
  5301. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  5302. _di_IDataObject DataObj, int KeyState,
  5303. const TPoint & Point, int & Effect, bool & Accept)
  5304. {
  5305. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  5306. }
  5307. //---------------------------------------------------------------------------
  5308. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  5309. _di_IDataObject DataObj, int KeyState,
  5310. const TPoint & Point, int & Effect, bool & Accept)
  5311. {
  5312. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  5313. }
  5314. //---------------------------------------------------------------------------
  5315. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  5316. TObject * Sender)
  5317. {
  5318. DebugUsedParam(Sender);
  5319. DebugAssert(FDDTargetControl == Sender);
  5320. FDDTargetControl = NULL;
  5321. }
  5322. //---------------------------------------------------------------------------
  5323. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  5324. {
  5325. FileControlDDDragLeave(SessionsPageControl);
  5326. }
  5327. //---------------------------------------------------------------------------
  5328. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  5329. {
  5330. FileControlDDDragLeave(QueueView3);
  5331. }
  5332. //---------------------------------------------------------------------------
  5333. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  5334. {
  5335. DebugAssert(GetSide(Side) == osRemote);
  5336. DebugUsedParam(Side);
  5337. bool Edit = false;
  5338. TRemoteFile * File = NULL;
  5339. UnicodeString FileName;
  5340. UnicodeString PointTo;
  5341. bool SymbolicLink = true;
  5342. if (RemoteDirView->ItemFocused)
  5343. {
  5344. DebugAssert(RemoteDirView->ItemFocused->Data);
  5345. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  5346. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  5347. if (Edit)
  5348. {
  5349. FileName = File->FileName;
  5350. PointTo = File->LinkTo;
  5351. }
  5352. else
  5353. {
  5354. PointTo = File->FileName;
  5355. }
  5356. }
  5357. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  5358. Terminal->IsCapable[fcHardLink]))
  5359. {
  5360. Configuration->Usage->Inc(L"RemoteLinksCreated");
  5361. if (Edit)
  5362. {
  5363. DebugAssert(File->FileName == FileName);
  5364. int Params = dfNoRecursive;
  5365. Terminal->ExceptionOnFail = true;
  5366. try
  5367. {
  5368. Terminal->DeleteFile(L"", File, &Params);
  5369. }
  5370. __finally
  5371. {
  5372. Terminal->ExceptionOnFail = false;
  5373. }
  5374. }
  5375. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  5376. }
  5377. }
  5378. //---------------------------------------------------------------------------
  5379. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  5380. {
  5381. return
  5382. (Terminal != NULL) &&
  5383. ((GetSide(Side) != osRemote) ||
  5384. (Terminal->ResolvingSymlinks &&
  5385. Terminal->IsCapable[fcSymbolicLink]));
  5386. }
  5387. //---------------------------------------------------------------------------
  5388. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  5389. {
  5390. return
  5391. (FCurrentSide == osRemote) &&
  5392. (RemoteDirView->ItemFocused != NULL) &&
  5393. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  5394. Terminal->SessionData->ResolveSymlinks;
  5395. }
  5396. //---------------------------------------------------------------------------
  5397. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  5398. {
  5399. DebugAssert(!WinConfiguration->DisableOpenEdit);
  5400. FForceExecution = true;
  5401. try
  5402. {
  5403. DirView(osCurrent)->ExecuteCurrentFile();
  5404. }
  5405. __finally
  5406. {
  5407. FForceExecution = false;
  5408. }
  5409. }
  5410. //---------------------------------------------------------------------------
  5411. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  5412. {
  5413. TEditorData ExternalEditor;
  5414. ExternalEditor.Editor = edExternal;
  5415. bool Remember = false;
  5416. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc,
  5417. (GetSide(osCurrent) == osRemote)))
  5418. {
  5419. if (Remember)
  5420. {
  5421. TEditorList * EditorList = new TEditorList;
  5422. try
  5423. {
  5424. *EditorList = *WinConfiguration->EditorList;
  5425. bool Found = false;
  5426. int i = 0;
  5427. while (!Found && (i < EditorList->Count))
  5428. {
  5429. const TEditorPreferences * Editor = EditorList->Editors[i];
  5430. if ((Editor->Data->Editor == edExternal) &&
  5431. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  5432. {
  5433. Found = true;
  5434. }
  5435. i++;
  5436. }
  5437. if (!Found)
  5438. {
  5439. EditorList->Add(new TEditorPreferences(ExternalEditor));
  5440. WinConfiguration->EditorList = EditorList;
  5441. }
  5442. }
  5443. __finally
  5444. {
  5445. delete EditorList;
  5446. }
  5447. }
  5448. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  5449. }
  5450. }
  5451. //---------------------------------------------------------------------------
  5452. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  5453. {
  5454. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  5455. if (FFileFindTerminal == Sender)
  5456. {
  5457. FFileFindTerminal = NULL;
  5458. HideFileFindDialog();
  5459. }
  5460. }
  5461. //---------------------------------------------------------------------------
  5462. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  5463. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  5464. {
  5465. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  5466. DebugAssert(Terminal != NULL);
  5467. if (Data->Terminal == Terminal)
  5468. {
  5469. Data->Terminal = NULL;
  5470. }
  5471. }
  5472. //---------------------------------------------------------------------------
  5473. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  5474. {
  5475. UpdateControls();
  5476. SessionColor = TColor(0);
  5477. UpdateRemotePathComboBox(false);
  5478. try
  5479. {
  5480. NeedSession(true);
  5481. }
  5482. catch (EAbort &)
  5483. {
  5484. // swallow
  5485. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect
  5486. }
  5487. }
  5488. //---------------------------------------------------------------------------
  5489. void __fastcall TCustomScpExplorerForm::NeedSession(bool ReloadSessions)
  5490. {
  5491. try
  5492. {
  5493. TTerminalManager::Instance()->NewSession(false, L"", ReloadSessions, this);
  5494. }
  5495. __finally
  5496. {
  5497. if (!WinConfiguration->KeepOpenWhenNoSession &&
  5498. (!Terminal || !Terminal->Active))
  5499. {
  5500. TerminateApplication();
  5501. }
  5502. }
  5503. }
  5504. //---------------------------------------------------------------------------
  5505. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged(bool Force)
  5506. {
  5507. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  5508. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  5509. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  5510. bool ListChanged = Force || (TerminalList->Count + 1 != SessionsPageControl->PageCount);
  5511. if (!ListChanged)
  5512. {
  5513. int Index = 0;
  5514. while (!ListChanged && (Index < TerminalList->Count))
  5515. {
  5516. ListChanged =
  5517. (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != TerminalList->Objects[Index]) ||
  5518. (SessionsPageControl->Pages[Index]->Caption != TerminalList->Strings[Index]);
  5519. Index++;
  5520. }
  5521. }
  5522. if (ListChanged)
  5523. {
  5524. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  5525. try
  5526. {
  5527. FSessionColors->Clear();
  5528. AddFixedSessionImages();
  5529. while (SessionsPageControl->PageCount > 0)
  5530. {
  5531. delete SessionsPageControl->Pages[0];
  5532. }
  5533. for (int Index = 0; Index < TerminalList->Count; Index++)
  5534. {
  5535. TThemeTabSheet * TabSheet = new TThemeTabSheet(SessionsPageControl);
  5536. TabSheet->Caption = TerminalList->Strings[Index];
  5537. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  5538. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  5539. TabSheet->PageControl = SessionsPageControl;
  5540. UpdateSessionTab(TabSheet);
  5541. }
  5542. TTabSheet * TabSheet = new TTabSheet(SessionsPageControl);
  5543. TabSheet->PageControl = SessionsPageControl;
  5544. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  5545. UpdateNewSessionTab();
  5546. }
  5547. __finally
  5548. {
  5549. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  5550. }
  5551. }
  5552. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  5553. }
  5554. //---------------------------------------------------------------------------
  5555. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  5556. {
  5557. DoTerminalListChanged(false);
  5558. }
  5559. //---------------------------------------------------------------------------
  5560. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  5561. {
  5562. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  5563. DebugAssert(TabSheet->ImageIndex == 0);
  5564. TabSheet->Caption =
  5565. WinConfiguration->SelectiveToolbarText ?
  5566. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  5567. UnicodeString();
  5568. }
  5569. //---------------------------------------------------------------------------
  5570. TTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  5571. {
  5572. return reinterpret_cast<TTerminal *>(TabSheet->Tag);
  5573. }
  5574. //---------------------------------------------------------------------------
  5575. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  5576. {
  5577. if (DebugAlwaysTrue(TabSheet != NULL))
  5578. {
  5579. TManagedTerminal * ManagedTerminal =
  5580. dynamic_cast<TManagedTerminal *>(GetSessionTabTerminal(TabSheet));
  5581. if (DebugAlwaysTrue(ManagedTerminal != NULL))
  5582. {
  5583. TColor Color =
  5584. (ManagedTerminal == FTerminal) ? FSessionColor : ManagedTerminal->StateData->Color;
  5585. TabSheet->ImageIndex = AddSessionColor(Color);
  5586. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  5587. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  5588. {
  5589. ThemeTabSheet->Shadowed = !ManagedTerminal->Active;
  5590. }
  5591. }
  5592. }
  5593. }
  5594. //---------------------------------------------------------------------------
  5595. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  5596. {
  5597. DebugAssert(SessionsPageControl->ActivePage != NULL);
  5598. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  5599. bool Result = (Terminal != NULL);
  5600. if (Result)
  5601. {
  5602. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  5603. }
  5604. else
  5605. {
  5606. try
  5607. {
  5608. NewSession(false);
  5609. }
  5610. __finally
  5611. {
  5612. DoTerminalListChanged(false);
  5613. }
  5614. FSessionsPageControlNewSessionTime = Now();
  5615. }
  5616. return Result;
  5617. }
  5618. //---------------------------------------------------------------------------
  5619. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  5620. {
  5621. SessionTabSwitched();
  5622. }
  5623. //---------------------------------------------------------------------------
  5624. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  5625. {
  5626. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  5627. DebugAssert(TransferList != NULL);
  5628. UnicodeString Name;
  5629. if (TransferList->ItemIndex <= 0)
  5630. {
  5631. Name = L"";
  5632. }
  5633. else
  5634. {
  5635. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  5636. }
  5637. if (FCopyParamAutoSelected.IsEmpty())
  5638. {
  5639. // if previous preset was not autoselected, make new preset the "default"
  5640. FCopyParamDefault = Name;
  5641. }
  5642. GUIConfiguration->CopyParamCurrent = Name;
  5643. }
  5644. //---------------------------------------------------------------------------
  5645. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  5646. {
  5647. // sanity check
  5648. bool ExistingPreset =
  5649. (FTransferListHoverIndex >= 0) &&
  5650. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  5651. DebugAssert(ExistingPreset);
  5652. if (ExistingPreset)
  5653. {
  5654. HDC DC = GetDC(0);
  5655. TCanvas * Canvas = new TCanvas();
  5656. try
  5657. {
  5658. Canvas->Handle = DC;
  5659. Canvas->Font = GetToolbarFont(this);
  5660. UnicodeString Name;
  5661. if (FTransferListHoverIndex == 0)
  5662. {
  5663. Name = L"";
  5664. }
  5665. else
  5666. {
  5667. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  5668. }
  5669. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  5670. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  5671. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  5672. static_cast<TObject*>(GetComponent(fcTransferList)));
  5673. UnicodeString InfoStr =
  5674. GUIConfiguration->CopyParamPreset[Name].
  5675. GetInfoStr(L"; ",
  5676. FLAGMASK(Terminal != NULL, Terminal->UsableCopyParamAttrs(0).General));
  5677. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  5678. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  5679. {
  5680. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  5681. {
  5682. InfoStr.SetLength(InfoStr.Length() - 1);
  5683. }
  5684. InfoStr += Ellipsis;
  5685. }
  5686. // UpdateCaption does not cause invalidation of whole submenu, while
  5687. // setting Caption property does.
  5688. // also it probably does not resize the label, even if necessary
  5689. // (we do not want that anyway)
  5690. TransferLabel->UpdateCaption(InfoStr);
  5691. }
  5692. __finally
  5693. {
  5694. Canvas->Handle = NULL;
  5695. ReleaseDC(0, DC);
  5696. delete Canvas;
  5697. }
  5698. }
  5699. }
  5700. //---------------------------------------------------------------------------
  5701. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  5702. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  5703. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  5704. {
  5705. if (FTransferListHoverIndex != AHoverIndex)
  5706. {
  5707. FTransferListHoverIndex = AHoverIndex;
  5708. UpdateTransferLabel();
  5709. }
  5710. }
  5711. //---------------------------------------------------------------------------
  5712. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  5713. {
  5714. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  5715. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  5716. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  5717. {
  5718. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  5719. {
  5720. OpenDirectory(GetSide(osCurrent));
  5721. Message.Result = 1;
  5722. }
  5723. else
  5724. {
  5725. TForm::Dispatch(&Message);
  5726. }
  5727. }
  5728. else
  5729. {
  5730. TForm::Dispatch(&Message);
  5731. }
  5732. }
  5733. //---------------------------------------------------------------------------
  5734. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  5735. {
  5736. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  5737. if ((FIgnoreNextDialogChar != 0) &&
  5738. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  5739. {
  5740. Message.Result = 1;
  5741. }
  5742. else
  5743. {
  5744. TForm::Dispatch(&Message);
  5745. }
  5746. FIgnoreNextDialogChar = 0;
  5747. }
  5748. //---------------------------------------------------------------------------
  5749. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  5750. {
  5751. // The four low-order bits are used internally by Windows
  5752. unsigned int Cmd = (Message.WParam & 0xFFF0);
  5753. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  5754. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  5755. // 61730 - restore thru double click - undocumented
  5756. // 61490 - maximize thru double click - undocumented
  5757. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  5758. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  5759. {
  5760. SysResizing(Cmd);
  5761. }
  5762. TForm::Dispatch(&Message);
  5763. }
  5764. //---------------------------------------------------------------------------
  5765. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  5766. {
  5767. // We were actually never able to make ENDSESSION_CRITICAL happen.
  5768. // Also there no point returning TRUE as we are not able to
  5769. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  5770. // Hence the process termination might be safer :)
  5771. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  5772. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  5773. {
  5774. Message.Result = FALSE;
  5775. }
  5776. else
  5777. {
  5778. Message.Result = TRUE;
  5779. }
  5780. // Do not call default handling as that triggers OnCloseQuery,
  5781. // where our implementation will popup confirmation dialogs, what we do not want,
  5782. // as per Vista guidelines:
  5783. // https://msdn.microsoft.com/en-us/library/ms700677.aspx
  5784. }
  5785. //---------------------------------------------------------------------------
  5786. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  5787. {
  5788. if (Message.EndSession && IsApplicationMinimized())
  5789. {
  5790. // WORKAROUND
  5791. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  5792. // when it receives WM_ENDSESSION,
  5793. // but that sometimes (particularly when application is minimized) cause crashes.
  5794. // Crash popups message that blocks log off.
  5795. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  5796. // so we call ExitProcess() immediately, not even trying to cleanup.
  5797. // It still causes beep, so there's likely some popup, but it does not block
  5798. // log off.
  5799. ExitProcess(0);
  5800. }
  5801. TForm::Dispatch(&Message);
  5802. }
  5803. #ifdef _DEBUG
  5804. //---------------------------------------------------------------------------
  5805. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  5806. {
  5807. TForm::Dispatch(&Message);
  5808. }
  5809. #endif
  5810. //---------------------------------------------------------------------------
  5811. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  5812. {
  5813. }
  5814. //---------------------------------------------------------------------------
  5815. void __fastcall TCustomScpExplorerForm::DoShow()
  5816. {
  5817. // only now are the controls resized finally, so the size constraints
  5818. // will not conflict with possibly very small window size
  5819. RestoreFormParams();
  5820. FixControlsPlacement();
  5821. if (Position == poDefaultPosOnly)
  5822. {
  5823. CutFormToDesktop(this);
  5824. }
  5825. TForm::DoShow();
  5826. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  5827. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  5828. if (Terminal == NULL)
  5829. {
  5830. StartingDisconnected();
  5831. }
  5832. UpdatePixelsPerInchMainWindowCounter();
  5833. FShowing = true;
  5834. }
  5835. //---------------------------------------------------------------------------
  5836. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  5837. {
  5838. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  5839. }
  5840. //---------------------------------------------------------------------------
  5841. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  5842. {
  5843. DoTerminalListChanged(true);
  5844. }
  5845. //---------------------------------------------------------------------------
  5846. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  5847. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  5848. TNotifyEvent OnBalloonClick, TObject * UserData)
  5849. {
  5850. bool Do;
  5851. UnicodeString Message;
  5852. if (E == NULL)
  5853. {
  5854. Message = Str;
  5855. Do = true;
  5856. }
  5857. else
  5858. {
  5859. Do = ExceptionMessage(E, Message);
  5860. }
  5861. if (Do && WinConfiguration->BalloonNotifications)
  5862. {
  5863. UnicodeString Title;
  5864. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  5865. {
  5866. Message = TrimLeft(Message);
  5867. }
  5868. else
  5869. {
  5870. Message = UnformatMessage(Message);
  5871. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  5872. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  5873. Title = LoadResourceString(Captions[Type]);
  5874. if (Terminal != NULL)
  5875. {
  5876. Title = FORMAT(L"%s - %s",
  5877. (TTerminalManager::Instance()->TerminalTitle(Terminal), Title));
  5878. }
  5879. }
  5880. if (Seconds == 0)
  5881. {
  5882. Seconds = WinConfiguration->NotificationsTimeout;
  5883. }
  5884. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  5885. }
  5886. }
  5887. //---------------------------------------------------------------------------
  5888. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  5889. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  5890. UnicodeString HelpKeyword, const TMessageParams * Params,
  5891. TTerminal * Terminal)
  5892. {
  5893. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  5894. (FOnFeedSynchronizeError != NULL)) &&
  5895. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  5896. {
  5897. if (FOnFeedSynchronizeError != NULL)
  5898. {
  5899. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  5900. }
  5901. else
  5902. {
  5903. DebugAssert(FErrorList != NULL);
  5904. TStringList * MoreMessagesCopy = NULL;
  5905. if (MoreMessages)
  5906. {
  5907. MoreMessagesCopy = new TStringList();
  5908. MoreMessagesCopy->Assign(MoreMessages);
  5909. }
  5910. FErrorList->AddObject(Message, MoreMessagesCopy);
  5911. }
  5912. PopupTrayBalloon(Terminal, Message, Type);
  5913. return ContinueAnswer(Answers);
  5914. }
  5915. else
  5916. {
  5917. bool UseBalloon = FTrayIcon->Visible;
  5918. if (UseBalloon)
  5919. {
  5920. PopupTrayBalloon(Terminal, Message, Type);
  5921. }
  5922. unsigned int Result;
  5923. try
  5924. {
  5925. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  5926. }
  5927. __finally
  5928. {
  5929. // cancel only balloon we popped up, otherwise we may cancel notification
  5930. // balloon that was there before the message dialog
  5931. // (such as "dd confirmation opt in balloon)
  5932. if (UseBalloon)
  5933. {
  5934. FTrayIcon->CancelBalloon();
  5935. }
  5936. }
  5937. return Result;
  5938. }
  5939. }
  5940. //---------------------------------------------------------------------------
  5941. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  5942. TTerminal * Terminal, Exception * E)
  5943. {
  5944. if (FTrayIcon->Visible)
  5945. {
  5946. PopupTrayBalloon(Terminal, L"", qtError, E);
  5947. }
  5948. // particularly prevent opening new session from jump list,
  5949. // while exception is shown
  5950. NonVisualDataModule->StartBusy();
  5951. try
  5952. {
  5953. ShowExtendedExceptionEx(Terminal, E);
  5954. }
  5955. __finally
  5956. {
  5957. NonVisualDataModule->EndBusy();
  5958. FTrayIcon->CancelBalloon();
  5959. }
  5960. }
  5961. //---------------------------------------------------------------------------
  5962. void __fastcall TCustomScpExplorerForm::TerminalReady()
  5963. {
  5964. // cannot rely on active page being page for active terminal,
  5965. // as it can happen that active page is the "new session" page
  5966. // (e.g. when reconnecting active terminal, while login dialog
  5967. // invoked from "new session" page is modal)
  5968. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  5969. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  5970. }
  5971. //---------------------------------------------------------------------------
  5972. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  5973. TTerminal * Terminal, Exception * E)
  5974. {
  5975. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  5976. if (!Terminal->Active)
  5977. {
  5978. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  5979. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  5980. {
  5981. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  5982. }
  5983. }
  5984. }
  5985. //---------------------------------------------------------------------------
  5986. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  5987. UnicodeString Message, TQueryType Type,
  5988. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  5989. {
  5990. if ((E == NULL) ||
  5991. ExceptionMessage(E, Message))
  5992. {
  5993. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  5994. if (Important)
  5995. {
  5996. Seconds *= 5;
  5997. }
  5998. UnicodeString NoteMessage(UnformatMessage(Message));
  5999. if (Terminal != NULL)
  6000. {
  6001. NoteMessage = FORMAT(L"%s: %s",
  6002. (TTerminalManager::Instance()->TerminalTitle(Terminal), NoteMessage));
  6003. }
  6004. if (WinConfiguration->BalloonNotifications)
  6005. {
  6006. AddNote(NoteMessage);
  6007. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6008. }
  6009. else
  6010. {
  6011. FlashOnBackground();
  6012. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6013. }
  6014. }
  6015. }
  6016. //---------------------------------------------------------------------------
  6017. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6018. {
  6019. RestoreApp();
  6020. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6021. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6022. !NonVisualDataModule->Busy)
  6023. {
  6024. TTerminal * Terminal = TerminalNoteData->Terminal;
  6025. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6026. if (!ComponentVisible[fcQueueView])
  6027. {
  6028. ToggleQueueVisibility();
  6029. GoToQueue();
  6030. }
  6031. }
  6032. }
  6033. //---------------------------------------------------------------------------
  6034. void __fastcall TCustomScpExplorerForm::QueueEvent(TTerminal * ATerminal,
  6035. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6036. {
  6037. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  6038. UnicodeString Message;
  6039. TNotifyEvent OnClick = NULL;
  6040. TObject * UserData = NULL;
  6041. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6042. switch (Event)
  6043. {
  6044. case qeEmptyButMonitored:
  6045. if ((ATerminal != Terminal) || QueueInvisible)
  6046. {
  6047. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6048. OnClick = QueueEmptyNoteClicked;
  6049. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6050. TerminalNoteData->Terminal = ATerminal;
  6051. UserData = TerminalNoteData;
  6052. }
  6053. break;
  6054. case qeEmpty:
  6055. OperationComplete(ManagedTerminal->QueueOperationStart);
  6056. break;
  6057. case qePendingUserAction:
  6058. if ((ATerminal != Terminal) ||
  6059. (QueueInvisible && !IsQueueAutoPopup()))
  6060. {
  6061. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6062. }
  6063. break;
  6064. default:
  6065. DebugFail();
  6066. }
  6067. if (!Message.IsEmpty())
  6068. {
  6069. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6070. }
  6071. }
  6072. //---------------------------------------------------------------------------
  6073. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6074. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6075. {
  6076. if (FDDExtMapFile != NULL)
  6077. {
  6078. CloseHandle(FDDExtMapFile);
  6079. FDDExtMapFile = NULL;
  6080. }
  6081. if (WinConfiguration->DDExtEnabled)
  6082. {
  6083. if (!WinConfiguration->DDExtInstalled)
  6084. {
  6085. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtNotInstalled");
  6086. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_NOT_INSTALLED2), HELP_DRAGEXT_TARGET_NOT_INSTALLED);
  6087. }
  6088. DDExtInitDrag(FileList, Created);
  6089. }
  6090. }
  6091. //---------------------------------------------------------------------------
  6092. void __fastcall TCustomScpExplorerForm::DDExtInitDrag(TFileList * FileList,
  6093. bool & Created)
  6094. {
  6095. FDragExtFakeDirectory =
  6096. ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6097. if (!ForceDirectories(ApiPath(FDragExtFakeDirectory)))
  6098. {
  6099. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (FDragExtFakeDirectory)));
  6100. }
  6101. FileList->AddItem(NULL, FDragExtFakeDirectory);
  6102. Created = true;
  6103. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6104. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6105. {
  6106. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6107. TDragExtCommStruct* CommStruct;
  6108. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6109. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6110. DebugAssert(CommStruct != NULL);
  6111. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  6112. CommStruct->Dragging = true;
  6113. wcsncpy(CommStruct->DropDest, FDragExtFakeDirectory.c_str(),
  6114. LENOF(CommStruct->DropDest));
  6115. NULL_TERMINATE(CommStruct->DropDest);
  6116. UnmapViewOfFile(CommStruct);
  6117. }
  6118. FDDMoveSlipped = false;
  6119. }
  6120. //---------------------------------------------------------------------------
  6121. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  6122. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  6123. {
  6124. bool Result;
  6125. if (Sender == RemoteDirView)
  6126. {
  6127. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  6128. }
  6129. else
  6130. {
  6131. DebugAssert(Sender == RemoteDriveView);
  6132. TStrings * FileList = RemoteDriveView->DragFileList();
  6133. try
  6134. {
  6135. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  6136. }
  6137. __finally
  6138. {
  6139. delete FileList;
  6140. }
  6141. }
  6142. if (FDDTargetControl == RemoteDriveView)
  6143. {
  6144. RemoteDriveView->UpdateDropTarget();
  6145. }
  6146. // foRemoteMove happens when file/dir is dragged within the remote tree view
  6147. // or from tree view to dir view.
  6148. // foMove happens when file/dir is dragged from remote tree view outside of
  6149. // application via dragex
  6150. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  6151. (DropSourceControl == RemoteDriveView))
  6152. {
  6153. RemoteDriveView->UpdateDropSource();
  6154. }
  6155. return Result;
  6156. }
  6157. //---------------------------------------------------------------------------
  6158. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  6159. {
  6160. // This also handles drops of remote files to queue.
  6161. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  6162. SAFE_DESTROY(FDDFileList);
  6163. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  6164. {
  6165. try
  6166. {
  6167. TDragResult DDResult = (Sender == RemoteDirView) ?
  6168. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  6169. // Focus is moved to the target application,
  6170. // but as we are going to present the UI, we need to steal the focus back.
  6171. // This most likely won't work though (windows does not allow application
  6172. // to steal focus most of the time)
  6173. Application->BringToFront();
  6174. // On older version of Windows we never got drMove here, see also comment below.
  6175. // On Windows 10, we get the "move".
  6176. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  6177. {
  6178. UnicodeString TargetDirectory;
  6179. TFileOperation Operation;
  6180. // drInvalid may mean drMove, see comment below
  6181. switch (DDResult)
  6182. {
  6183. case drCopy:
  6184. Operation = foCopy;
  6185. break;
  6186. case drMove:
  6187. Operation = foMove;
  6188. break;
  6189. default:
  6190. DebugFail();
  6191. case drInvalid:
  6192. // prefer "copy" for safety
  6193. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  6194. break;
  6195. }
  6196. if (FDDMoveSlipped)
  6197. {
  6198. Operation = foMove;
  6199. }
  6200. TTransferOperationParam Param;
  6201. bool Internal;
  6202. bool ForceQueue;
  6203. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, Internal))
  6204. {
  6205. // we get drInvalid both if d&d was intercepted by ddext,
  6206. // and when users drops on no-drop location.
  6207. // we tell the difference by existence of response from ddext,
  6208. // so we ignore absence of response in this case
  6209. if (DDResult != drInvalid)
  6210. {
  6211. // here we know that the extension is installed,
  6212. // as it is checked as soon as drag&drop starts from
  6213. // RemoteFileControlDDCreateDragFileList
  6214. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  6215. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  6216. }
  6217. }
  6218. else
  6219. {
  6220. // download using ddext
  6221. Param.Temp = false;
  6222. Param.DragDrop = true;
  6223. if (ForceQueue)
  6224. {
  6225. Param.Queue = asOn;
  6226. }
  6227. if (Sender == RemoteDirView)
  6228. {
  6229. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  6230. }
  6231. if (RemoteFileControlFileOperation(Sender, Operation,
  6232. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  6233. {
  6234. Configuration->Usage->Inc(
  6235. Internal ? L"DownloadsDragDropInternal" : L"DownloadsDragDropExternalExt");
  6236. }
  6237. }
  6238. }
  6239. }
  6240. __finally
  6241. {
  6242. CloseHandle(FDDExtMapFile);
  6243. FDDExtMapFile = NULL;
  6244. RemoveDir(ApiPath(FDragExtFakeDirectory));
  6245. FDragExtFakeDirectory = L"";
  6246. }
  6247. }
  6248. if (FDragDropSshTerminate.get() != NULL)
  6249. {
  6250. FDragDropSshTerminate->Rethrow();
  6251. }
  6252. }
  6253. //---------------------------------------------------------------------------
  6254. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  6255. TObject * Sender, int dwEffect, HRESULT & /*Result*/)
  6256. {
  6257. HCURSOR SlippedCopyCursor;
  6258. FDDMoveSlipped =
  6259. (FDragMoveCursor != NULL) &&
  6260. (!WinConfiguration->DDAllowMoveInit) && (dwEffect == DROPEFFECT_Copy) &&
  6261. ((IsFileControl(FDDTargetControl, osRemote) && (GetKeyState(VK_CONTROL) >= 0) &&
  6262. FTerminal->IsCapable[fcRemoteMove]) ||
  6263. (IsFileControl(FDDTargetControl, osLocal) && (GetKeyState(VK_SHIFT) < 0)));
  6264. SlippedCopyCursor = FDDMoveSlipped ? FDragMoveCursor : Dragdrop::DefaultCursor;
  6265. DragDropFiles(Sender)->CHCopy = SlippedCopyCursor;
  6266. DragDropFiles(Sender)->CHScrollCopy = SlippedCopyCursor;
  6267. // Remember drop effect so we know (when user drops files), if we copy or move
  6268. FLastDropEffect = dwEffect;
  6269. }
  6270. //---------------------------------------------------------------------------
  6271. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  6272. UnicodeString & Directory, bool & ForceQueue, bool & Internal)
  6273. {
  6274. bool Result;
  6275. if (FDDTargetControl == QueueView3)
  6276. {
  6277. Directory = DefaultDownloadTargetDirectory();
  6278. Result = true;
  6279. Internal = true;
  6280. ForceQueue = true;
  6281. }
  6282. else
  6283. {
  6284. ForceQueue = false;
  6285. Enabled = false;
  6286. try
  6287. {
  6288. int Timer = 0;
  6289. Result = false;
  6290. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  6291. {
  6292. {
  6293. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6294. TDragExtCommStruct* CommStruct;
  6295. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6296. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6297. DebugAssert(CommStruct != NULL);
  6298. Result = !CommStruct->Dragging;
  6299. if (Result)
  6300. {
  6301. Directory = ExtractFilePath(CommStruct->DropDest);
  6302. Internal = false;
  6303. }
  6304. UnmapViewOfFile(CommStruct);
  6305. }
  6306. if (!Result)
  6307. {
  6308. Sleep(50);
  6309. Timer += 50;
  6310. Application->ProcessMessages();
  6311. }
  6312. }
  6313. }
  6314. __finally
  6315. {
  6316. Enabled = true;
  6317. }
  6318. }
  6319. return Result;
  6320. }
  6321. //---------------------------------------------------------------------------
  6322. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  6323. const UnicodeString TempDir, int SecDelay)
  6324. {
  6325. TDateTime Alarm = Now() + (double)((double)SecDelay*OneMillisecond);
  6326. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  6327. if (FDelayedDeletionTimer == NULL)
  6328. {
  6329. DebugAssert(HandleAllocated());
  6330. FDelayedDeletionTimer = new TTimer(this);
  6331. FDelayedDeletionTimer->Interval = 10000;
  6332. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  6333. }
  6334. else
  6335. {
  6336. FDelayedDeletionTimer->Enabled = true;
  6337. }
  6338. }
  6339. //---------------------------------------------------------------------------
  6340. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  6341. {
  6342. DebugAssert(FDelayedDeletionList != NULL);
  6343. TDateTime N = Now();
  6344. TDateTime Alert;
  6345. UnicodeString Directory;
  6346. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  6347. {
  6348. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  6349. if ((N >= Alert) || (Sender == NULL))
  6350. {
  6351. Directory = FDelayedDeletionList->Strings[Index];
  6352. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  6353. {
  6354. FDelayedDeletionList->Delete(Index);
  6355. }
  6356. }
  6357. }
  6358. if (FDelayedDeletionList->Count == 0)
  6359. {
  6360. FDelayedDeletionTimer->Enabled = false;
  6361. }
  6362. }
  6363. //---------------------------------------------------------------------------
  6364. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  6365. {
  6366. if (IsFileControl(FDDTargetControl, osRemote) ||
  6367. (FDDTargetControl == SessionsPageControl))
  6368. {
  6369. TTerminal * TargetTerminal = NULL;
  6370. TFileOperation Operation = foNone;
  6371. if (FDDTargetControl == SessionsPageControl)
  6372. {
  6373. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  6374. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  6375. // do not allow dropping on the "+" tab
  6376. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  6377. if (TargetTerminal != NULL)
  6378. {
  6379. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  6380. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  6381. {
  6382. Operation = foRemoteMove;
  6383. }
  6384. else
  6385. {
  6386. Operation = foRemoteCopy;
  6387. }
  6388. }
  6389. }
  6390. else
  6391. {
  6392. // when move from remote side is disabled, we allow copying inside the remote
  6393. // panel, but we interpret is as moving (we also slip in the move cursor)
  6394. if ((FLastDropEffect == DROPEFFECT_MOVE) ||
  6395. (!WinConfiguration->DDAllowMoveInit && (FLastDropEffect == DROPEFFECT_COPY) &&
  6396. FDDMoveSlipped))
  6397. {
  6398. Operation = foRemoteMove;
  6399. }
  6400. else if (FLastDropEffect == DROPEFFECT_COPY)
  6401. {
  6402. Operation = foRemoteCopy;
  6403. }
  6404. }
  6405. if (Operation != foNone)
  6406. {
  6407. RemoteFileControlFileOperation(DropSourceControl,
  6408. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  6409. }
  6410. // abort drag&drop
  6411. Abort();
  6412. }
  6413. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  6414. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  6415. (FDDTargetControl != QueueView3))
  6416. {
  6417. DebugAssert(!FDragTempDir.IsEmpty());
  6418. TTransferType Type;
  6419. UnicodeString TempDir = FDragTempDir;
  6420. // We clear FDragTempDir before calling
  6421. // just in case it fail (raises exception)
  6422. FDragTempDir = L"";
  6423. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  6424. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  6425. // empty directory parameter means temp directory -> don't display it!
  6426. UnicodeString TargetDir = L"";
  6427. int Options = 0;
  6428. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  6429. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  6430. {
  6431. Abort();
  6432. }
  6433. // TargetDir is set when dropped on local file control
  6434. // (this was workaround for legacy dirview event handling, now it should be
  6435. // made prettier)
  6436. if (TargetDir.IsEmpty())
  6437. {
  6438. TargetDir = TempDir;
  6439. if (ForceDirectories(ApiPath(TargetDir)))
  6440. {
  6441. DebugAssert(Terminal && !TargetDir.IsEmpty());
  6442. FPendingTempSpaceWarn = true;
  6443. try
  6444. {
  6445. FDragDropOperation = true;
  6446. int Params = cpTemporary |
  6447. (Type == ttMove ? cpDelete : 0);
  6448. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  6449. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  6450. }
  6451. __finally
  6452. {
  6453. FDragDropOperation = false;
  6454. FPendingTempSpaceWarn = false;
  6455. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  6456. }
  6457. }
  6458. else
  6459. {
  6460. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  6461. }
  6462. }
  6463. }
  6464. }
  6465. //---------------------------------------------------------------------------
  6466. void __fastcall TCustomScpExplorerForm::DDDownload(TStrings * FilesToCopy,
  6467. const UnicodeString TargetDir, const TCopyParamType * CopyParam, int Params)
  6468. {
  6469. void * BatchStorage;
  6470. BatchStart(BatchStorage);
  6471. try
  6472. {
  6473. UpdateCopyParamCounters(*CopyParam);
  6474. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  6475. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  6476. {
  6477. RemoteDriveView->UpdateDropSource();
  6478. }
  6479. }
  6480. __finally
  6481. {
  6482. BatchEnd(BatchStorage);
  6483. }
  6484. }
  6485. //---------------------------------------------------------------------------
  6486. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  6487. {
  6488. public:
  6489. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  6490. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename)
  6491. {
  6492. }
  6493. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  6494. {
  6495. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  6496. TDataObjectFilesEx::AllowData(FormatEtc);
  6497. }
  6498. };
  6499. //---------------------------------------------------------------------------
  6500. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  6501. TObject * Sender, TDataObject *& DataObject)
  6502. {
  6503. if (FDDExtMapFile != NULL)
  6504. {
  6505. TFileList * FileList = DragDropFiles(Sender)->FileList;
  6506. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  6507. {
  6508. Abort();
  6509. }
  6510. if (FileList->Count > 0)
  6511. {
  6512. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  6513. if (!FilesObject->IsValid(true, true))
  6514. {
  6515. FilesObject->_Release();
  6516. }
  6517. else
  6518. {
  6519. DataObject = FilesObject;
  6520. }
  6521. }
  6522. }
  6523. }
  6524. //---------------------------------------------------------------------------
  6525. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  6526. {
  6527. DebugFail();
  6528. }
  6529. //---------------------------------------------------------------------------
  6530. void __fastcall TCustomScpExplorerForm::GoToTree()
  6531. {
  6532. ComponentVisible[fcRemoteTree] = true;
  6533. RemoteDriveView->SetFocus();
  6534. }
  6535. //---------------------------------------------------------------------------
  6536. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  6537. TPanelExport Export)
  6538. {
  6539. TCustomDirView * DirView = this->DirView(Side);
  6540. std::unique_ptr<TStrings> ExportData(new TStringList());
  6541. switch (Export)
  6542. {
  6543. case pePath:
  6544. ExportData->Add(DirView->PathName);
  6545. break;
  6546. case peFileList:
  6547. case peFullFileList:
  6548. {
  6549. bool FullPath = (Export == peFullFileList);
  6550. DirView->CreateFileList(false, FullPath, ExportData.get());
  6551. for (int Index = 0; Index < ExportData->Count; Index++)
  6552. {
  6553. if (ExportData->Strings[Index].Pos(L" ") > 0)
  6554. {
  6555. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  6556. }
  6557. }
  6558. }
  6559. break;
  6560. default:
  6561. DebugFail();
  6562. }
  6563. return ExportData.release();
  6564. }
  6565. //---------------------------------------------------------------------------
  6566. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  6567. TPanelExport Export, TPanelExportDestination Destination)
  6568. {
  6569. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  6570. PanelExportStore(Side, Export, Destination, ExportData.get());
  6571. }
  6572. //---------------------------------------------------------------------------
  6573. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  6574. TPanelExport /*Export*/, TPanelExportDestination Destination,
  6575. TStrings * ExportData)
  6576. {
  6577. if (Destination == pedClipboard)
  6578. {
  6579. TInstantOperationVisualizer Visualizer;
  6580. CopyToClipboard(ExportData);
  6581. }
  6582. else
  6583. {
  6584. DebugFail();
  6585. }
  6586. }
  6587. //---------------------------------------------------------------------------
  6588. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  6589. {
  6590. TCustomDirView * DirView = this->DirView(Side);
  6591. TFileFilter Filter;
  6592. DefaultFileFilter(Filter);
  6593. Filter.Masks = DirView->Mask;
  6594. if (DoFilterMaskDialog(DirView, &Filter))
  6595. {
  6596. DirView->Mask = TFileMasks::NormalizeMask(Filter.Masks);
  6597. Configuration->Usage->Inc(L"Filters");
  6598. }
  6599. }
  6600. //---------------------------------------------------------------------------
  6601. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  6602. {
  6603. return FQueueController->DefaultOperation();
  6604. }
  6605. //---------------------------------------------------------------------------
  6606. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  6607. TQueueOperation Operation, void ** Param)
  6608. {
  6609. switch (Operation)
  6610. {
  6611. case qoPreferences:
  6612. return true;
  6613. case qoGoTo:
  6614. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  6615. case qoOnceEmpty:
  6616. return !FQueueController->Empty;
  6617. default:
  6618. return FQueueController->AllowOperation(Operation, Param);
  6619. }
  6620. }
  6621. //---------------------------------------------------------------------------
  6622. void __fastcall TCustomScpExplorerForm::GoToQueue()
  6623. {
  6624. if (DebugAlwaysTrue(QueueView3->Visible))
  6625. {
  6626. QueueView3->SetFocus();
  6627. }
  6628. }
  6629. //---------------------------------------------------------------------------
  6630. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  6631. TQueueOperation Operation, void * Param)
  6632. {
  6633. if (Operation == qoGoTo)
  6634. {
  6635. GoToQueue();
  6636. }
  6637. else if (Operation == qoPreferences)
  6638. {
  6639. PreferencesDialog(pmQueue);
  6640. }
  6641. else
  6642. {
  6643. FQueueController->ExecuteOperation(Operation, Param);
  6644. }
  6645. }
  6646. //---------------------------------------------------------------------------
  6647. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  6648. {
  6649. return (Queue != NULL) && Queue->Enabled;
  6650. }
  6651. //---------------------------------------------------------------------------
  6652. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  6653. {
  6654. DebugAssert(Queue != NULL);
  6655. if (Queue != NULL)
  6656. {
  6657. Queue->Enabled = !Queue->Enabled;
  6658. }
  6659. }
  6660. //---------------------------------------------------------------------------
  6661. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  6662. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  6663. {
  6664. FQueueActedItem = QueueView3->ItemFocused;
  6665. }
  6666. //---------------------------------------------------------------------------
  6667. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  6668. {
  6669. return TopDock->Height + QueueSplitter->Height;
  6670. }
  6671. //---------------------------------------------------------------------------
  6672. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  6673. TObject * /*Sender*/, int & NewSize, bool & Accept)
  6674. {
  6675. // when queue is hidden by double-clicking splitter, stray attempt to
  6676. // resize the panel with strange value arrives, make sure it is ignored
  6677. if (ComponentVisible[fcQueueView])
  6678. {
  6679. int HeightLimit = ClientHeight - GetStaticComponentsHeight() -
  6680. RemotePanel->Constraints->MinHeight;
  6681. if (NewSize > HeightLimit)
  6682. {
  6683. NewSize = HeightLimit;
  6684. }
  6685. }
  6686. else
  6687. {
  6688. Accept = false;
  6689. }
  6690. }
  6691. //---------------------------------------------------------------------------
  6692. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  6693. TDragObject *& /*DragObject*/)
  6694. {
  6695. FQueueActedItem = QueueView3->ItemFocused;
  6696. }
  6697. //---------------------------------------------------------------------------
  6698. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  6699. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  6700. {
  6701. Accept = true;
  6702. if (Source == QueueView3)
  6703. {
  6704. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  6705. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  6706. if (Accept)
  6707. {
  6708. TQueueItemProxy * QueueItem;
  6709. TQueueItemProxy * DestQueueItem;
  6710. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  6711. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  6712. Accept = (QueueItem != DestQueueItem) &&
  6713. (QueueItem->Status == TQueueItem::qsPending) &&
  6714. (DestQueueItem->Status == TQueueItem::qsPending);
  6715. }
  6716. }
  6717. }
  6718. //---------------------------------------------------------------------------
  6719. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  6720. TObject * /*Source*/, int /*X*/, int /*Y*/)
  6721. {
  6722. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  6723. {
  6724. TQueueItemProxy * QueueItem;
  6725. TQueueItemProxy * DestQueueItem;
  6726. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  6727. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  6728. QueueItem->Move(DestQueueItem);
  6729. }
  6730. }
  6731. //---------------------------------------------------------------------------
  6732. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  6733. {
  6734. if ((QueueView3->ItemFocused == NULL) &&
  6735. (QueueView3->Items->Count > 0))
  6736. {
  6737. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  6738. }
  6739. QueueLabelUpdateStatus();
  6740. }
  6741. //---------------------------------------------------------------------------
  6742. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  6743. {
  6744. QueueLabelUpdateStatus();
  6745. }
  6746. //---------------------------------------------------------------------------
  6747. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  6748. {
  6749. QueueLabel->UpdateStatus();
  6750. }
  6751. //---------------------------------------------------------------------------
  6752. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  6753. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  6754. {
  6755. if (Selected)
  6756. {
  6757. NonVisualDataModule->UpdateNonVisibleActions();
  6758. }
  6759. }
  6760. //---------------------------------------------------------------------------
  6761. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  6762. {
  6763. TDragDropFilesEx * Result = NULL;
  6764. if (Sender == SessionsPageControl)
  6765. {
  6766. Result = FSessionsDragDropFilesEx;
  6767. }
  6768. else if (Sender == QueueView3)
  6769. {
  6770. Result = FQueueDragDropFilesEx;
  6771. }
  6772. else
  6773. {
  6774. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  6775. if (DirView != NULL)
  6776. {
  6777. Result = DirView->DragDropFilesEx;
  6778. }
  6779. else
  6780. {
  6781. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  6782. if (DriveView != NULL)
  6783. {
  6784. Result = DriveView->DragDropFilesEx;
  6785. }
  6786. }
  6787. }
  6788. DebugAssert(Result != NULL);
  6789. return Result;
  6790. }
  6791. //---------------------------------------------------------------------------
  6792. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  6793. {
  6794. return (DView->SelCount == DView->FilesCount);
  6795. }
  6796. //---------------------------------------------------------------------------
  6797. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  6798. {
  6799. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  6800. }
  6801. //---------------------------------------------------------------------------
  6802. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  6803. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue)
  6804. {
  6805. TFileOperation Operation;
  6806. switch (Effect)
  6807. {
  6808. case DROPEFFECT_MOVE:
  6809. Operation = foMove;
  6810. break;
  6811. case DROPEFFECT_COPY:
  6812. // occures on WinXP (reported by user)
  6813. default:
  6814. Operation = foCopy;
  6815. break;
  6816. };
  6817. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  6818. // see a comment in TUnixDirView::PerformItemDragDropOperation
  6819. if (DragDropFilesEx->FileList->Count > 0)
  6820. {
  6821. TStrings * FileList = new TStringList();
  6822. try
  6823. {
  6824. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  6825. {
  6826. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  6827. }
  6828. FDragDropOperation = true;
  6829. TTransferOperationParam Param;
  6830. Param.TargetDirectory = TargetPath;
  6831. // upload, no temp dirs
  6832. Param.Temp = false;
  6833. Param.DragDrop = true;
  6834. Param.Options =
  6835. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  6836. if (ForceQueue)
  6837. {
  6838. Param.Queue = asOn;
  6839. }
  6840. if (ExecuteFileOperation(Operation, osLocal, FileList,
  6841. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  6842. {
  6843. if (IsFileControl(DropSourceControl, osLocal))
  6844. {
  6845. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  6846. }
  6847. else
  6848. {
  6849. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  6850. }
  6851. }
  6852. }
  6853. __finally
  6854. {
  6855. FDragDropOperation = false;
  6856. delete FileList;
  6857. }
  6858. }
  6859. }
  6860. //---------------------------------------------------------------------------
  6861. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  6862. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  6863. UnicodeString TargetPath, bool & /*DoOperation*/)
  6864. {
  6865. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false);
  6866. }
  6867. //---------------------------------------------------------------------------
  6868. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  6869. TObject * Sender, int grfKeyState, int & dwEffect)
  6870. {
  6871. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  6872. if ((dwEffect != DROPEFFECT_None) &&
  6873. IsFileControl(DropSourceControl, osRemote))
  6874. {
  6875. // do not allow drop on remote panel (on free space, still allow drop on directories)
  6876. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  6877. (RemoteDirView->DropTarget == NULL))
  6878. {
  6879. dwEffect = DROPEFFECT_None;
  6880. }
  6881. else
  6882. {
  6883. if (dwEffect == DROPEFFECT_Copy)
  6884. {
  6885. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  6886. // currently we support copying always (at least via temporary directory);
  6887. // remove associated checks once this all proves stable and working
  6888. bool CopyCapable = true;
  6889. // if we do not support neither of operations, there's no discussion
  6890. if (!MoveCapable && !CopyCapable)
  6891. {
  6892. dwEffect = DROPEFFECT_None;
  6893. }
  6894. // when moving is disabled, we need to keep effect to "copy",
  6895. // which will be later interpretted as move (with slipped-in cursor)
  6896. else if (!WinConfiguration->DDAllowMoveInit && FLAGCLEAR(grfKeyState, MK_CONTROL))
  6897. {
  6898. // no-op, keep copy
  6899. }
  6900. else
  6901. {
  6902. // The default effect inside remote panel is move,
  6903. // unless we do not support it, but support copy
  6904. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  6905. {
  6906. dwEffect = MoveCapable ? DROPEFFECT_Move : DROPEFFECT_Copy;
  6907. }
  6908. else
  6909. {
  6910. // with ctrl-down, we want copy unless it is not supported
  6911. dwEffect = CopyCapable ? DROPEFFECT_Copy : DROPEFFECT_None;
  6912. }
  6913. }
  6914. }
  6915. }
  6916. }
  6917. }
  6918. //---------------------------------------------------------------------------
  6919. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  6920. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  6921. {
  6922. if (FDDTotalSize >= 0)
  6923. {
  6924. if (File->IsDirectory)
  6925. {
  6926. FDDTotalSize = -1;
  6927. }
  6928. else
  6929. {
  6930. FDDTotalSize += File->Size;
  6931. }
  6932. }
  6933. DebugAssert(!FDragTempDir.IsEmpty());
  6934. // TODO: this is quite ineffective
  6935. // TODO: what if invalid character replacement is disabled?
  6936. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  6937. UnicodeString TransferFileName;
  6938. if (Sender == RemoteDriveView)
  6939. {
  6940. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  6941. }
  6942. else
  6943. {
  6944. TransferFileName = File->FileName;
  6945. }
  6946. FDDFileList->AddObject(TransferFileName, File);
  6947. }
  6948. //---------------------------------------------------------------------------
  6949. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  6950. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  6951. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  6952. {
  6953. // sometimes we do not get DDEnd so the list is not released
  6954. SAFE_DESTROY(FDDFileList);
  6955. FDDFileList = new TStringList();
  6956. FDragTempDir = WinConfiguration->TemporaryDir();
  6957. FDDTotalSize = 0;
  6958. FDragDropSshTerminate.reset(NULL);
  6959. }
  6960. //---------------------------------------------------------------------------
  6961. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  6962. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  6963. HRESULT & Result)
  6964. {
  6965. if (Result == DRAGDROP_S_DROP)
  6966. {
  6967. try
  6968. {
  6969. GlobalDragImageList->HideDragImage();
  6970. try
  6971. {
  6972. RemoteFileControlDDTargetDrop();
  6973. }
  6974. catch(ESshTerminate & E)
  6975. {
  6976. FDragDropSshTerminate.reset(E.Clone());
  6977. }
  6978. }
  6979. catch (Exception &E)
  6980. {
  6981. // If downloading fails we need to cancel drag&drop, otherwise
  6982. // Explorer shows error
  6983. // But by the way exception probably never reach this point as
  6984. // it's catched on way.
  6985. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  6986. Result = DRAGDROP_S_CANCEL;
  6987. ShowExtendedException(Terminal, &E);
  6988. }
  6989. }
  6990. }
  6991. //---------------------------------------------------------------------------
  6992. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  6993. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  6994. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  6995. {
  6996. TFileMasks::TParams MaskParams;
  6997. MaskParams.Size = Size;
  6998. MaskParams.Modification = Modification;
  6999. // this does not re-parse the mask if it is the same as the last time
  7000. FDirViewMatchMask = Masks;
  7001. bool ImplicitMatch;
  7002. Matches =
  7003. // RecurseInclude parameter has no effect as the Path is empty
  7004. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7005. (AllowImplicitMatches || !ImplicitMatch);
  7006. }
  7007. //---------------------------------------------------------------------------
  7008. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7009. TObject * Sender, TListItem * Item, WORD & Indexes)
  7010. {
  7011. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7012. UnicodeString Ext;
  7013. if (DirView == RemoteDirView)
  7014. {
  7015. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7016. }
  7017. else
  7018. {
  7019. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7020. }
  7021. if (SameText(Ext, Configuration->PartialExt))
  7022. {
  7023. Indexes |= oiPartial;
  7024. }
  7025. }
  7026. //---------------------------------------------------------------------------
  7027. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7028. {
  7029. return
  7030. DirViewEnabled(osCurrent) &&
  7031. DirView(osCurrent)->CanPasteFromClipBoard();
  7032. }
  7033. //---------------------------------------------------------------------------
  7034. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7035. {
  7036. bool Result = false;
  7037. if (CanPasteToDirViewFromClipBoard())
  7038. {
  7039. Result = true;
  7040. }
  7041. else
  7042. {
  7043. UnicodeString ClipboardText;
  7044. if (NonEmptyTextFromClipboard(ClipboardText))
  7045. {
  7046. if (StoredSessions->IsUrl(ClipboardText))
  7047. {
  7048. Result = true;
  7049. }
  7050. else
  7051. {
  7052. Result = DirViewEnabled(osCurrent);
  7053. }
  7054. }
  7055. }
  7056. return Result;
  7057. }
  7058. //---------------------------------------------------------------------------
  7059. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7060. {
  7061. if (CanPasteToDirViewFromClipBoard())
  7062. {
  7063. DirView(osCurrent)->PasteFromClipBoard();
  7064. }
  7065. else
  7066. {
  7067. UnicodeString ClipboardText;
  7068. if (NonEmptyTextFromClipboard(ClipboardText))
  7069. {
  7070. if (StoredSessions->IsUrl(ClipboardText))
  7071. {
  7072. NewSession(false, ClipboardText);
  7073. }
  7074. else
  7075. {
  7076. DirView(osCurrent)->Path = ClipboardText;
  7077. }
  7078. }
  7079. }
  7080. }
  7081. //---------------------------------------------------------------------------
  7082. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  7083. {
  7084. // TBD
  7085. }
  7086. //---------------------------------------------------------------------------
  7087. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  7088. {
  7089. TCustomDirView * ADirView = DirView(Side);
  7090. ADirView->SelectAll(Mode);
  7091. ADirView->SetFocus();
  7092. }
  7093. //---------------------------------------------------------------------------
  7094. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  7095. {
  7096. TCustomDirView * ADirView = DirView(Side);
  7097. TFileFilter Filter;
  7098. DefaultFileFilter(Filter);
  7099. if (DoSelectMaskDialog(ADirView, Select, &Filter, Configuration))
  7100. {
  7101. Configuration->Usage->Inc(L"MaskSelections");
  7102. ADirView->SelectFiles(Filter, Select);
  7103. ADirView->SetFocus();
  7104. }
  7105. }
  7106. //---------------------------------------------------------------------------
  7107. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  7108. {
  7109. TCustomDirView * ADirView = DirView(Side);
  7110. ADirView->RestoreSelectedNames();
  7111. ADirView->SetFocus();
  7112. }
  7113. //---------------------------------------------------------------------------
  7114. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  7115. {
  7116. TCustomDirView * CurrentDirView = DirView(osCurrent);
  7117. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  7118. {
  7119. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  7120. UnicodeString Ext;
  7121. if (GetSide(osCurrent) == osRemote)
  7122. {
  7123. Ext = UnixExtractFileExt(FileName);
  7124. }
  7125. else
  7126. {
  7127. Ext = ExtractFileExt(FileName);
  7128. }
  7129. if (Ext.IsEmpty())
  7130. {
  7131. Ext = L".";
  7132. }
  7133. TFileFilter Filter;
  7134. Filter.Masks = FORMAT(L"*%s", (Ext));
  7135. Filter.Directories = false;
  7136. CurrentDirView->SelectFiles(Filter, Select);
  7137. }
  7138. }
  7139. //---------------------------------------------------------------------------
  7140. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  7141. const TStatusFileInfo & FileInfo, TOperationSide Side)
  7142. {
  7143. UnicodeString Result;
  7144. if ((Side == osRemote) && (Terminal == NULL))
  7145. {
  7146. // noop
  7147. }
  7148. else
  7149. {
  7150. Result =
  7151. FMTLOAD(FILE_INFO_FORMAT,
  7152. (FormatBytes(FileInfo.SelectedSize),
  7153. FormatBytes(FileInfo.FilesSize),
  7154. FormatNumber(FileInfo.SelectedCount),
  7155. FormatNumber(FileInfo.FilesCount)));
  7156. }
  7157. return Result;
  7158. }
  7159. //---------------------------------------------------------------------------
  7160. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  7161. TTBXStatusPanel * Panel, TOperationSide Side)
  7162. {
  7163. if (Panel->Index == 1)
  7164. {
  7165. ToggleShowHiddenFiles();
  7166. }
  7167. else if (Panel->Index == 2)
  7168. {
  7169. Filter(Side);
  7170. }
  7171. }
  7172. //---------------------------------------------------------------------------
  7173. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  7174. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  7175. {
  7176. DebugAssert(!StatusBar->SimplePanel);
  7177. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  7178. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  7179. }
  7180. //---------------------------------------------------------------------------
  7181. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  7182. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  7183. {
  7184. DebugAssert(StatusBar->Panels->Count >= 3);
  7185. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  7186. if (FileInfo.HiddenCount > 0)
  7187. {
  7188. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  7189. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7190. }
  7191. else
  7192. {
  7193. HiddenFilesPanel->ViewPriority = 0;
  7194. // not really necessary, just to cleanup no-longer-valid data
  7195. HiddenFilesPanel->Caption = L"";
  7196. }
  7197. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  7198. if (FileInfo.FilteredCount > 0)
  7199. {
  7200. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  7201. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7202. }
  7203. else
  7204. {
  7205. FilteredFilesPanel->ViewPriority = 0;
  7206. // not really necessary, just to cleanup no-longer-valid data
  7207. FilteredFilesPanel->Caption = L"";
  7208. }
  7209. }
  7210. //---------------------------------------------------------------------------
  7211. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  7212. TObject * /*Sender*/)
  7213. {
  7214. if (RemoteDirView->Enabled)
  7215. {
  7216. RemoteDirView->SetFocus();
  7217. }
  7218. }
  7219. //---------------------------------------------------------------------------
  7220. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  7221. {
  7222. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  7223. switch (Config.Show)
  7224. {
  7225. case qvShow:
  7226. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  7227. {
  7228. Config.Show = qvHide;
  7229. }
  7230. else
  7231. {
  7232. Config.Show = Config.LastHideShow;
  7233. }
  7234. break;
  7235. case qvHideWhenEmpty:
  7236. if (ComponentVisible[fcQueueView])
  7237. {
  7238. Config.Show = qvHide;
  7239. }
  7240. else
  7241. {
  7242. Config.LastHideShow = Config.Show;
  7243. Config.Show = qvShow;
  7244. }
  7245. break;
  7246. case qvHide:
  7247. Config.LastHideShow = Config.Show;
  7248. Config.Show = qvShow;
  7249. break;
  7250. }
  7251. WinConfiguration->QueueView = Config;
  7252. }
  7253. //---------------------------------------------------------------------------
  7254. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  7255. {
  7256. UnicodeString Result;
  7257. if (FTerminal != NULL)
  7258. {
  7259. switch (WinConfiguration->PathInCaption)
  7260. {
  7261. case picShort:
  7262. {
  7263. Result = UnixExtractFileName(FTerminal->CurrentDirectory);
  7264. if (Result.IsEmpty())
  7265. {
  7266. Result = FTerminal->CurrentDirectory;
  7267. }
  7268. }
  7269. break;
  7270. case picFull:
  7271. Result = FTerminal->CurrentDirectory;
  7272. break;
  7273. }
  7274. }
  7275. return Result;
  7276. }
  7277. //---------------------------------------------------------------------------
  7278. void __fastcall TCustomScpExplorerForm::UpdateControls()
  7279. {
  7280. TTerminalManager::Instance()->UpdateAppTitle();
  7281. // WORAKRDOUND: Disabling list view when it is not showing yet does not set its
  7282. // background to gray on Windows 7 (works on Windows 10).
  7283. // See also EnableControl
  7284. if (Showing)
  7285. {
  7286. bool HasTerminal = (Terminal != NULL);
  7287. if (HasTerminal)
  7288. {
  7289. if (!RemoteDirView->Enabled)
  7290. {
  7291. RemoteDirView->Enabled = true;
  7292. if (FRemoteDirViewWasFocused)
  7293. {
  7294. ActiveControl = RemoteDirView;
  7295. }
  7296. }
  7297. RemoteDriveView->Enabled = true;
  7298. RemoteDirView->Color = (FSessionColor != 0 ? FSessionColor : clWindow);
  7299. RemoteDriveView->Color = RemoteDirView->Color;
  7300. }
  7301. else
  7302. {
  7303. if (RemoteDirView->Enabled)
  7304. {
  7305. // This is first called when the form is being constructed
  7306. // (not anymore due to Showing test above)
  7307. // but the false is overriden in the constructor later.
  7308. // An even later in TScpCommanderForm::DoShow()
  7309. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  7310. EnableControl(RemoteDirView, false);
  7311. }
  7312. EnableControl(RemoteDriveView, false);
  7313. }
  7314. EnableControl(QueueView3, HasTerminal);
  7315. QueueLabelUpdateStatus();
  7316. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  7317. }
  7318. }
  7319. //---------------------------------------------------------------------------
  7320. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  7321. {
  7322. UpdateControls();
  7323. }
  7324. //---------------------------------------------------------------------------
  7325. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  7326. TObject * Sender)
  7327. {
  7328. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7329. DebugAssert(DirView != NULL);
  7330. DoDirViewLoaded(DirView);
  7331. TransferPresetAutoSelect();
  7332. }
  7333. //---------------------------------------------------------------------------
  7334. void __fastcall TCustomScpExplorerForm::StartUpdates()
  7335. {
  7336. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  7337. // first run after installation
  7338. if (double(Updates.LastCheck) == 0)
  7339. {
  7340. // make sure next time there will be an update (if enabled)
  7341. Updates.LastCheck = TDateTime(1);
  7342. WinConfiguration->Updates = Updates;
  7343. }
  7344. else if ((double(Updates.Period) > 0) &&
  7345. (Now() - Updates.LastCheck >= Updates.Period))
  7346. {
  7347. StartUpdateThread(UpdatesChecked);
  7348. }
  7349. }
  7350. //---------------------------------------------------------------------------
  7351. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  7352. {
  7353. UnicodeString Message;
  7354. bool New;
  7355. TQueryType Type;
  7356. GetUpdatesMessage(Message, New, Type, false);
  7357. if (!Message.IsEmpty())
  7358. {
  7359. if (New)
  7360. {
  7361. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  7362. }
  7363. if (!New && (Type != qtWarning))
  7364. {
  7365. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  7366. }
  7367. else
  7368. {
  7369. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  7370. }
  7371. if (New)
  7372. {
  7373. Configuration->Usage->Inc(L"UpdateNotifications");
  7374. }
  7375. }
  7376. }
  7377. //---------------------------------------------------------------------------
  7378. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  7379. {
  7380. RestoreApp();
  7381. bool CanDisplay = !NonVisualDataModule->Busy;
  7382. if (!CanDisplay && (Screen->ActiveForm != NULL))
  7383. {
  7384. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  7385. }
  7386. if (CanDisplay)
  7387. {
  7388. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  7389. CheckForUpdates(true);
  7390. }
  7391. else
  7392. {
  7393. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  7394. }
  7395. }
  7396. //---------------------------------------------------------------------------
  7397. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  7398. TCopyParamRuleData & Data)
  7399. {
  7400. DebugAssert(Terminal != NULL);
  7401. Data.HostName = Terminal->SessionData->HostNameExpanded;
  7402. Data.UserName = Terminal->SessionData->UserNameExpanded;
  7403. Data.RemoteDirectory = RemoteDirView->PathName;
  7404. }
  7405. //---------------------------------------------------------------------------
  7406. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  7407. {
  7408. // Terminal can be null when we are changing local directory implicitly,
  7409. // because it has been deleted, while no session is connected
  7410. // (Login dialog is open)
  7411. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  7412. {
  7413. TCopyParamRuleData Data;
  7414. GetTransferPresetAutoSelectData(Data);
  7415. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  7416. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  7417. if (CopyParamIndex < 0)
  7418. {
  7419. // there is no preset that matches autoselection
  7420. // set preset that we consider "default"
  7421. FCopyParamAutoSelected = L""; // forget last autoselected preset
  7422. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  7423. }
  7424. else
  7425. {
  7426. // there is preset matching autoselection
  7427. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  7428. if (CopyParamName == FCopyParamAutoSelected)
  7429. {
  7430. // autoselected the same preset as the last time
  7431. // make no change (i.e. preserve custom user preset, if any)
  7432. }
  7433. else
  7434. {
  7435. // autoselected the different preset then the last time (or there
  7436. // was default preset set)
  7437. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  7438. GUIConfiguration->CopyParamCurrent = CopyParamName;
  7439. }
  7440. }
  7441. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  7442. {
  7443. if (CopyParamIndex >= 0)
  7444. {
  7445. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  7446. }
  7447. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  7448. try
  7449. {
  7450. int Fmt =
  7451. (CopyParamIndex < 0) ?
  7452. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  7453. COPY_PARAM_AUTOSELECTED;
  7454. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  7455. Data->Message = MainInstructions(Message);
  7456. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  7457. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  7458. CopyParamAttrs);
  7459. if (CopyParamIndex >= 0)
  7460. {
  7461. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  7462. Info = FORMAT(L"%s\n \n%s", (Info,
  7463. FMTLOAD(COPY_PARAM_RULE,
  7464. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  7465. }
  7466. Data->Message += L"\n\n" + Info;
  7467. if (WinConfiguration->CopyParamAutoSelectNotice)
  7468. {
  7469. TransferPresetNoteMessage(Data, true);
  7470. }
  7471. else
  7472. {
  7473. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  7474. Data = NULL; // ownership passed
  7475. }
  7476. }
  7477. __finally
  7478. {
  7479. delete Data;
  7480. }
  7481. }
  7482. }
  7483. }
  7484. //---------------------------------------------------------------------------
  7485. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  7486. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  7487. {
  7488. DebugAssert(NoteData != NULL);
  7489. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  7490. TQueryButtonAlias Aliases[1];
  7491. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  7492. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  7493. Params.Aliases = Aliases;
  7494. Params.AliasesCount = LENOF(Aliases);
  7495. unsigned int Result =
  7496. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  7497. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  7498. switch (Result)
  7499. {
  7500. case qaNeverAskAgain:
  7501. DebugAssert(AllowNeverAskAgain);
  7502. WinConfiguration->CopyParamAutoSelectNotice = false;
  7503. break;
  7504. case qaIgnore:
  7505. PreferencesDialog(pmPresets);
  7506. break;
  7507. }
  7508. }
  7509. //---------------------------------------------------------------------------
  7510. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  7511. {
  7512. // as of now useless, as this is used for notes only, never for balloons, ...
  7513. RestoreApp();
  7514. // .. and we should never be busy here
  7515. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  7516. {
  7517. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  7518. }
  7519. }
  7520. //---------------------------------------------------------------------------
  7521. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  7522. TPreferencesMode APreferencesMode)
  7523. {
  7524. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  7525. TCopyParamRuleData Data;
  7526. if (Terminal != NULL)
  7527. {
  7528. PreferencesData.reset(new TPreferencesDialogData());
  7529. GetTransferPresetAutoSelectData(Data);
  7530. PreferencesData->CopyParamRuleData = &Data;
  7531. }
  7532. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  7533. }
  7534. //---------------------------------------------------------------------------
  7535. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  7536. const TCustomCommandType & Command)
  7537. {
  7538. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  7539. {
  7540. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (Command.Command)));
  7541. }
  7542. }
  7543. //---------------------------------------------------------------------------
  7544. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  7545. {
  7546. bool RemoteAllowed = CustomCommandRemoteAllowed();
  7547. TCustomCommandType Command;
  7548. // make sure we use local custom command when remote are not supported
  7549. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  7550. {
  7551. Command = FLastCustomCommand;
  7552. }
  7553. else
  7554. {
  7555. Command.Params = Command.Params | ccLocal;
  7556. }
  7557. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  7558. FEditingFocusedAdHocCommand = OnFocused;
  7559. bool LocalSide = (FCurrentSide == osLocal);
  7560. int Options =
  7561. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  7562. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  7563. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  7564. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  7565. {
  7566. FLastCustomCommand = Command;
  7567. UpdateCustomCommandsToolbar();
  7568. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  7569. }
  7570. }
  7571. //---------------------------------------------------------------------------
  7572. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  7573. {
  7574. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  7575. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  7576. DebugAssert(State > 0);
  7577. if (State <= 0)
  7578. {
  7579. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (FLastCustomCommand.Command)));
  7580. }
  7581. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  7582. }
  7583. //---------------------------------------------------------------------------
  7584. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  7585. TCustomCommandType & Command, int & State)
  7586. {
  7587. bool Result = !FLastCustomCommand.Command.IsEmpty();
  7588. if (Result)
  7589. {
  7590. Command = FLastCustomCommand;
  7591. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  7592. }
  7593. return Result;
  7594. }
  7595. //---------------------------------------------------------------------------
  7596. void __fastcall TCustomScpExplorerForm::BeforeAction()
  7597. {
  7598. if (RemoteDirView->ItemFocused != NULL)
  7599. {
  7600. RemoteDirView->ItemFocused->CancelEdit();
  7601. }
  7602. }
  7603. //---------------------------------------------------------------------------
  7604. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  7605. {
  7606. DebugAssert(ComponentVisible[Component]);
  7607. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  7608. }
  7609. //---------------------------------------------------------------------------
  7610. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  7611. {
  7612. // when queue panel is resized here directly, the status bar is stretched
  7613. // over whole space the panel occupied
  7614. PostComponentHide(fcQueueView);
  7615. }
  7616. //---------------------------------------------------------------------------
  7617. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  7618. {
  7619. TMessage * M = static_cast<TMessage*>(Message);
  7620. switch (M->Msg)
  7621. {
  7622. case CM_DIALOGCHAR:
  7623. CMDialogChar(*M);
  7624. break;
  7625. case WM_APPCOMMAND:
  7626. WMAppCommand(*M);
  7627. break;
  7628. case WM_SYSCOMMAND:
  7629. WMSysCommand(*M);
  7630. break;
  7631. case WM_QUERYENDSESSION:
  7632. WMQueryEndSession(*M);
  7633. break;
  7634. case WM_ENDSESSION:
  7635. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  7636. break;
  7637. #ifdef _DEBUG
  7638. case WM_WINDOWPOSCHANGED:
  7639. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  7640. break;
  7641. case CM_CUSTOMSTYLECHANGED:
  7642. TForm::Dispatch(Message);
  7643. break;
  7644. #endif
  7645. case WM_COMPONENT_HIDE:
  7646. {
  7647. Byte Component = static_cast<Byte>(M->WParam);
  7648. // sanity check
  7649. if (ComponentVisible[Component])
  7650. {
  7651. // special treatment
  7652. if (Component == fcQueueView)
  7653. {
  7654. ToggleQueueVisibility();
  7655. DebugAssert(!ComponentVisible[fcQueueView]);
  7656. }
  7657. else
  7658. {
  7659. ComponentVisible[Component] = false;
  7660. }
  7661. }
  7662. }
  7663. break;
  7664. case WM_COPYDATA:
  7665. WMCopyData(*M);
  7666. break;
  7667. case WM_MANAGES_CAPTION:
  7668. // caption managed in TTerminalManager::UpdateAppTitle()
  7669. M->Result = 1;
  7670. break;
  7671. case WM_WANTS_MOUSEWHEEL:
  7672. M->Result = 1;
  7673. break;
  7674. case CM_SHOWINGCHANGED:
  7675. CMShowingChanged(*M);
  7676. break;
  7677. case WM_CLOSE:
  7678. WMClose(*M);
  7679. break;
  7680. case WM_DPICHANGED:
  7681. WMDpiChanged(*M);
  7682. break;
  7683. case CM_DPICHANGED:
  7684. CMDpiChanged(*M);
  7685. break;
  7686. default:
  7687. TForm::Dispatch(Message);
  7688. break;
  7689. }
  7690. }
  7691. //---------------------------------------------------------------------------
  7692. void __fastcall TCustomScpExplorerForm::UpdateImages()
  7693. {
  7694. // noop
  7695. }
  7696. //---------------------------------------------------------------------------
  7697. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  7698. {
  7699. TForm::Dispatch(&Message);
  7700. // regenerate session images
  7701. DoTerminalListChanged(true);
  7702. UpdateImages();
  7703. }
  7704. //---------------------------------------------------------------------------
  7705. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  7706. {
  7707. TForm::Dispatch(&Message);
  7708. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  7709. UpdatePixelsPerInchMainWindowCounter();
  7710. }
  7711. //---------------------------------------------------------------------------
  7712. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  7713. {
  7714. // Cannot close window while we are busy.
  7715. // We cannot test this in FormCloseQuery as that is called also from
  7716. // Close(), which is called by CloseApplicationAction. So we can be busy
  7717. // there already even, when it is legitimate to close the application.
  7718. // Possibly a better place to handle this would be WMSysCommand.
  7719. if (NonVisualDataModule->Busy)
  7720. {
  7721. Message.Result = 1;
  7722. }
  7723. else
  7724. {
  7725. TForm::Dispatch(&Message);
  7726. }
  7727. }
  7728. //---------------------------------------------------------------------------
  7729. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  7730. {
  7731. TForm::Dispatch(&Message);
  7732. if (Showing && (Terminal == NULL))
  7733. {
  7734. // When we are starting minimized (i.e. from an installer),
  7735. // postpone showing Login dialog until we get restored.
  7736. // Otherwise the Login dialog (and Authentication window) show restored
  7737. // over invidible (minimized) main window.
  7738. if (WindowState == wsMinimized)
  7739. {
  7740. FNeedSession = true;
  7741. }
  7742. else
  7743. {
  7744. // This happens before application ever goes idle, so the toolbars would
  7745. // stay enabled (initial state) until the Login dialog is dismissed.
  7746. UpdateActions();
  7747. NonVisualDataModule->StartBusy();
  7748. try
  7749. {
  7750. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  7751. // otherwise the dialog does not receive focus.
  7752. // With Commander interface the ProcessMessages is called already
  7753. // by TDriveView, but with Explorer interface, we need to call it explicily
  7754. Application->ProcessMessages();
  7755. // do not reload sessions, they have been loaded just now (optimization)
  7756. NeedSession(false);
  7757. }
  7758. __finally
  7759. {
  7760. NonVisualDataModule->EndBusy();
  7761. }
  7762. }
  7763. }
  7764. }
  7765. //---------------------------------------------------------------------------
  7766. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  7767. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  7768. int & MaxHeight)
  7769. {
  7770. // workaround for bug in TWinControl.CalcConstraints
  7771. // Check for empty rect (restore from iconic state) is done there only after
  7772. // call to AdjustClientRect, which enlarges the rect (for forms).
  7773. TRect R = GetClientRect();
  7774. // when restoring from iconic state, place no restrictions
  7775. if (IsRectEmpty(R))
  7776. {
  7777. MinWidth = 0;
  7778. MinHeight = 0;
  7779. MaxWidth = 32000;
  7780. MaxHeight = 32000;
  7781. }
  7782. }
  7783. //---------------------------------------------------------------------------
  7784. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  7785. TSpaceAvailable & ASpaceAvailable, bool & Close)
  7786. {
  7787. // terminal can be already closed (e.g. dropped connection)
  7788. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  7789. {
  7790. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  7791. try
  7792. {
  7793. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  7794. }
  7795. catch(...)
  7796. {
  7797. if (!Terminal->Active)
  7798. {
  7799. Close = true;
  7800. }
  7801. throw;
  7802. }
  7803. }
  7804. }
  7805. //---------------------------------------------------------------------------
  7806. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  7807. {
  7808. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  7809. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  7810. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  7811. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  7812. {
  7813. OnGetSpaceAvailable = GetSpaceAvailable;
  7814. }
  7815. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  7816. OnGetSpaceAvailable);
  7817. }
  7818. //---------------------------------------------------------------------------
  7819. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  7820. {
  7821. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  7822. Terminal->FillSessionDataForCode(Data.get());
  7823. return Data.release();
  7824. }
  7825. //---------------------------------------------------------------------------
  7826. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  7827. {
  7828. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  7829. DoGenerateUrlDialog(Data.get(), Paths);
  7830. }
  7831. //---------------------------------------------------------------------------
  7832. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  7833. {
  7834. GenerateUrl(NULL);
  7835. }
  7836. //---------------------------------------------------------------------------
  7837. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  7838. {
  7839. std::unique_ptr<TStrings> Paths(new TStringList());
  7840. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  7841. GenerateUrl(Paths.get());
  7842. }
  7843. //---------------------------------------------------------------------------
  7844. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  7845. {
  7846. if (value != FSessionColor)
  7847. {
  7848. FSessionColor = value;
  7849. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  7850. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  7851. static_cast<TObject *>(GetComponent(fcColorMenu)));
  7852. DebugAssert(ColorItem != NULL);
  7853. ColorItem->Color = C;
  7854. NonVisualDataModule->ColorMenuItem->Color = C;
  7855. // Is null when called from LastTerminalClosed
  7856. if (Terminal != NULL)
  7857. {
  7858. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  7859. }
  7860. UpdateControls();
  7861. }
  7862. }
  7863. //---------------------------------------------------------------------------
  7864. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  7865. {
  7866. bool Result = FNoteTimer->Enabled;
  7867. if (Result)
  7868. {
  7869. // cannot cancel note too early
  7870. bool NotEarly =
  7871. (Now() - FNoteShown >
  7872. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  7873. if (Force || NotEarly)
  7874. {
  7875. FNoteTimer->Enabled = false;
  7876. FNote = L"";
  7877. SAFE_DESTROY(FNoteData);
  7878. FOnNoteClick = NULL;
  7879. FNoteHints = FNotes->Text;
  7880. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  7881. UpdateStatusBar();
  7882. }
  7883. }
  7884. return Result;
  7885. }
  7886. //---------------------------------------------------------------------------
  7887. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  7888. {
  7889. DebugAssert(FNoteTimer->Enabled);
  7890. CancelNote(true);
  7891. }
  7892. //---------------------------------------------------------------------------
  7893. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  7894. {
  7895. int P = Note.Pos(L"\n");
  7896. if (P > 0)
  7897. {
  7898. Note.SetLength(P - 1);
  7899. }
  7900. FNotes->Add(FORMAT(L"[%s] %s",
  7901. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  7902. while (FNotes->Count > 10)
  7903. {
  7904. FNotes->Delete(0);
  7905. }
  7906. if (UpdateNow)
  7907. {
  7908. FNoteHints = FNotes->Text;
  7909. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  7910. UpdateStatusBar();
  7911. }
  7912. }
  7913. //---------------------------------------------------------------------------
  7914. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  7915. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  7916. {
  7917. int P = Note.Pos(L"\n");
  7918. if (P > 0)
  7919. {
  7920. Note.SetLength(P - 1);
  7921. }
  7922. FNoteHints = FNotes->Text;
  7923. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  7924. FNote = Note;
  7925. SAFE_DESTROY(FNoteData);
  7926. FNoteData = NoteData;
  7927. FOnNoteClick = OnNoteClick;
  7928. AddNote(Note, false);
  7929. UpdateStatusBar();
  7930. FNoteShown = Now();
  7931. FNoteTimer->Enabled = false;
  7932. if (Seconds == 0)
  7933. {
  7934. Seconds = WinConfiguration->NotificationsTimeout;
  7935. }
  7936. FNoteTimer->Interval = Seconds * MSecsPerSec;
  7937. FNoteTimer->Enabled = true;
  7938. }
  7939. //---------------------------------------------------------------------------
  7940. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  7941. {
  7942. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  7943. }
  7944. //---------------------------------------------------------------------------
  7945. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  7946. {
  7947. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  7948. {
  7949. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  7950. }
  7951. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  7952. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  7953. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  7954. 0, NULL, NULL);
  7955. }
  7956. //---------------------------------------------------------------------------
  7957. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  7958. {
  7959. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  7960. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  7961. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  7962. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  7963. }
  7964. //---------------------------------------------------------------------------
  7965. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  7966. {
  7967. WinConfiguration->FormatSizeBytes = Style;
  7968. }
  7969. //---------------------------------------------------------------------------
  7970. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  7971. {
  7972. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  7973. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  7974. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  7975. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  7976. }
  7977. //---------------------------------------------------------------------------
  7978. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  7979. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  7980. {
  7981. if (Panel->Index == 0)
  7982. {
  7983. if (FOnNoteClick != NULL)
  7984. {
  7985. // prevent the user data from being freed by possible call
  7986. // to CancelNote or PostNote during call to OnNoteClick
  7987. std::unique_ptr<TObject> NoteData(FNoteData);
  7988. TNotifyEvent OnNoteClick = FOnNoteClick;
  7989. FNoteData = NULL;
  7990. // need to cancel the note as we are going to delete its user data
  7991. CancelNote(true);
  7992. OnNoteClick(NoteData.get());
  7993. }
  7994. }
  7995. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  7996. {
  7997. FileSystemInfo();
  7998. }
  7999. }
  8000. //---------------------------------------------------------------------------
  8001. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  8002. {
  8003. // workaround:
  8004. // 1) for unknown reason, disabling window, while minimized,
  8005. // prevents it from restoring, even if it was enabled again meanwhile
  8006. // 2) when disabling the main window, while another has focus
  8007. // minimize is no longer possible
  8008. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  8009. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  8010. // Shouldn't we use IsApplicationMinimized() here?
  8011. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  8012. {
  8013. Enabled = false;
  8014. }
  8015. FLockLevel++;
  8016. }
  8017. //---------------------------------------------------------------------------
  8018. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  8019. {
  8020. DebugAssert(FLockLevel > 0);
  8021. FLockLevel--;
  8022. if (FLockLevel == 0)
  8023. {
  8024. DebugAssert(FLockSuspendLevel == 0);
  8025. Enabled = true;
  8026. }
  8027. }
  8028. //---------------------------------------------------------------------------
  8029. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  8030. {
  8031. // We need to make sure that window is enabled when the last modal window closes
  8032. // otherwise focus is not restored correctly.
  8033. // So we re-enable the window when modal window opens and
  8034. // disable it back after is closes
  8035. if (FLockLevel > 0)
  8036. {
  8037. // while we have nesting counter, we know that we never be called
  8038. // recursivelly as Application->OnModalBegin is called only
  8039. // for the top-level modal window
  8040. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8041. {
  8042. // won't be disabled when conditions in LockWindow() were not satisfied
  8043. FDisabledOnLockSuspend = !Enabled;
  8044. // When minimized to tray (or actually when set to SW_HIDE),
  8045. // setting Enabled makes the window focusable even when there's
  8046. // modal window over it
  8047. if (!FTrayIcon->Visible)
  8048. {
  8049. Enabled = true;
  8050. }
  8051. }
  8052. FLockSuspendLevel++;
  8053. }
  8054. }
  8055. //---------------------------------------------------------------------------
  8056. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  8057. {
  8058. if (FLockSuspendLevel > 0)
  8059. {
  8060. DebugAssert(FLockLevel > 0);
  8061. FLockSuspendLevel--;
  8062. // see comment in SuspendWindowLock
  8063. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8064. {
  8065. // Note that window can be enabled here, when we were minized to tray when
  8066. // was SuspendWindowLock() called.
  8067. // We should possibly do the same check as in LockWindow(),
  8068. // if it is ever possible that the consitions change between
  8069. // SuspendWindowLock() and ResumeWindowLock()
  8070. Enabled = !FDisabledOnLockSuspend;
  8071. }
  8072. }
  8073. }
  8074. //---------------------------------------------------------------------------
  8075. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  8076. {
  8077. if (!TextOnly)
  8078. {
  8079. TTBXComboBoxItem * RemotePathComboBox =
  8080. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  8081. TStrings * Items = RemotePathComboBox->Strings;
  8082. Items->BeginUpdate();
  8083. try
  8084. {
  8085. Items->Clear();
  8086. if (Terminal != NULL)
  8087. {
  8088. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8089. while (!IsUnixRootPath(APath))
  8090. {
  8091. int P = APath.LastDelimiter(L'/');
  8092. DebugAssert(P >= 0);
  8093. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  8094. APath.SetLength(P - 1);
  8095. }
  8096. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  8097. }
  8098. }
  8099. __finally
  8100. {
  8101. RemotePathComboBox->ItemIndex = Items->Count - 1;
  8102. // Setting ItemIndex to -1 does not reset its text
  8103. if (Items->Count == 0)
  8104. {
  8105. RemotePathComboBox->Text = L"";
  8106. }
  8107. Items->EndUpdate();
  8108. }
  8109. }
  8110. }
  8111. //---------------------------------------------------------------------------
  8112. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  8113. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  8114. int & ImageIndex)
  8115. {
  8116. if (AIndex < 0)
  8117. {
  8118. AIndex = Sender->ItemIndex;
  8119. }
  8120. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  8121. }
  8122. //---------------------------------------------------------------------------
  8123. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  8124. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  8125. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  8126. {
  8127. ARect.Left += (10 * AIndex);
  8128. }
  8129. //---------------------------------------------------------------------------
  8130. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  8131. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  8132. {
  8133. AWidth += (10 * AIndex);
  8134. }
  8135. //---------------------------------------------------------------------------
  8136. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  8137. TObject * Sender)
  8138. {
  8139. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  8140. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8141. int Index = RemotePathComboBox->ItemIndex;
  8142. while (Index < RemotePathComboBox->Strings->Count - 1)
  8143. {
  8144. APath = UnixExtractFileDir(APath);
  8145. Index++;
  8146. }
  8147. // VanDyke style paths
  8148. if (APath.IsEmpty())
  8149. {
  8150. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  8151. APath = ROOTDIRECTORY;
  8152. }
  8153. if (RemoteDirView->Path != APath)
  8154. {
  8155. RemoteDirView->Path = APath;
  8156. }
  8157. }
  8158. //---------------------------------------------------------------------------
  8159. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  8160. {
  8161. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  8162. DebugUsedParam(Sender);
  8163. UpdateRemotePathComboBox(true);
  8164. }
  8165. //---------------------------------------------------------------------------
  8166. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  8167. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  8168. {
  8169. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8170. DebugAssert(DirView != NULL);
  8171. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  8172. {
  8173. HWND Edit = ListView_GetEditControl(DirView->Handle);
  8174. // OnEditing is called also from TCustomListView::CanEdit
  8175. if (Edit != NULL)
  8176. {
  8177. EditSelectBaseName(Edit);
  8178. }
  8179. }
  8180. }
  8181. //---------------------------------------------------------------------------
  8182. //---------------------------------------------------------------------------
  8183. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  8184. {
  8185. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  8186. Result->AutoDetectDnD = false;
  8187. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  8188. Result->RenderDataOn = rdoEnterAndDropSync;
  8189. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  8190. return Result;
  8191. }
  8192. //---------------------------------------------------------------------------
  8193. void __fastcall TCustomScpExplorerForm::CreateWnd()
  8194. {
  8195. TForm::CreateWnd();
  8196. if (FSessionsDragDropFilesEx == NULL)
  8197. {
  8198. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  8199. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  8200. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  8201. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  8202. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  8203. }
  8204. if (FQueueDragDropFilesEx == NULL)
  8205. {
  8206. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  8207. // No need to set OnDragOver as we do not have any restrictions
  8208. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  8209. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  8210. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  8211. }
  8212. }
  8213. //---------------------------------------------------------------------------
  8214. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  8215. {
  8216. TForm::DestroyWnd();
  8217. FSessionsDragDropFilesEx->DragDropControl = NULL;
  8218. FQueueDragDropFilesEx->DragDropControl = NULL;
  8219. }
  8220. //---------------------------------------------------------------------------
  8221. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  8222. {
  8223. SideEnter(FCurrentSide);
  8224. FEverShown = true;
  8225. }
  8226. //---------------------------------------------------------------------------
  8227. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  8228. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  8229. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  8230. {
  8231. if (!NonVisualDataModule->Busy)
  8232. {
  8233. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8234. Configuration->Usage->Inc(L"FileFinds");
  8235. LockWindow(true);
  8236. NonVisualDataModule->StartBusy();
  8237. WinConfiguration->LockedInterface = true;
  8238. try
  8239. {
  8240. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  8241. }
  8242. __finally
  8243. {
  8244. WinConfiguration->LockedInterface = false;
  8245. NonVisualDataModule->EndBusy();
  8246. UnlockWindow();
  8247. }
  8248. }
  8249. }
  8250. //---------------------------------------------------------------------------
  8251. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  8252. {
  8253. if (!NonVisualDataModule->Busy)
  8254. {
  8255. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8256. SetFocus();
  8257. RemoteDirView->Path = UnixExtractFilePath(Path);
  8258. TListItem * Item = RemoteDirView->FindFileItem(UnixExtractFileName(Path));
  8259. if (Item != NULL)
  8260. {
  8261. RemoteDirView->ItemFocused = Item;
  8262. RemoteDirView->ItemFocused->MakeVisible(false);
  8263. RemoteDirView->SetFocus();
  8264. }
  8265. }
  8266. }
  8267. //---------------------------------------------------------------------------
  8268. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  8269. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8270. {
  8271. if (!NonVisualDataModule->Busy)
  8272. {
  8273. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8274. DebugAssert(FOnFileOperationFinished == NULL);
  8275. FOnFileOperationFinished = OnFileOperationFinished;
  8276. try
  8277. {
  8278. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  8279. }
  8280. __finally
  8281. {
  8282. FOnFileOperationFinished = NULL;
  8283. }
  8284. }
  8285. }
  8286. //---------------------------------------------------------------------------
  8287. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  8288. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8289. {
  8290. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  8291. }
  8292. //---------------------------------------------------------------------------
  8293. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  8294. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8295. {
  8296. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  8297. }
  8298. //---------------------------------------------------------------------------
  8299. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  8300. {
  8301. FFileFindTerminal = Terminal;
  8302. ShowFileFindDialog(Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles, DoDownloadFoundFiles);
  8303. }
  8304. //---------------------------------------------------------------------------
  8305. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  8306. {
  8307. FTaskbarList = TaskbarList;
  8308. }
  8309. //---------------------------------------------------------------------------
  8310. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  8311. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  8312. {
  8313. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8314. if (Index >= 0)
  8315. {
  8316. if (Button == mbLeft)
  8317. {
  8318. // "Mouse down" is raised only after tab is switched.
  8319. // If switching tab (switching session) takes long enough for user
  8320. // to actually release the button, "mouse down" is still raised,
  8321. // but we do not get "mouse up" event, so dragging is not cancelled,
  8322. // prevent that by not beginning dragging in the first place.
  8323. if (IsKeyPressed(VK_LBUTTON))
  8324. {
  8325. // when user clicks the "+", we get mouse down only after the session
  8326. // is closed, when new session tab is already on X:Y, so dragging
  8327. // starts, prevent that
  8328. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  8329. {
  8330. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8331. if (Terminal != NULL)
  8332. {
  8333. SessionsPageControl->BeginDrag(false);
  8334. }
  8335. }
  8336. }
  8337. }
  8338. else if (Button == mbMiddle)
  8339. {
  8340. // ignore middle-click for "New session tab"
  8341. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8342. if (Terminal != NULL)
  8343. {
  8344. SessionsPageControl->ActivePageIndex = Index;
  8345. // Switch to session tab (has to be session tab, due to previous check)
  8346. if (DebugAlwaysTrue(SessionTabSwitched()))
  8347. {
  8348. CloseSession();
  8349. }
  8350. }
  8351. }
  8352. }
  8353. }
  8354. //---------------------------------------------------------------------------
  8355. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  8356. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  8357. {
  8358. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8359. // do not allow dropping on the "+" tab
  8360. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8361. if ((TargetTerminal != NULL) &&
  8362. (SessionsPageControl->ActivePage->PageIndex != Index))
  8363. {
  8364. Configuration->Usage->Inc(L"SessionTabMoves");
  8365. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  8366. // but we want to actually prevent that to avoid flicker
  8367. SessionsPageControl->ActivePage->PageIndex = Index;
  8368. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  8369. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  8370. }
  8371. }
  8372. //---------------------------------------------------------------------------
  8373. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  8374. TObject * Sender, TObject * Source, int X, int Y,
  8375. TDragState /*State*/, bool & Accept)
  8376. {
  8377. Accept = (Sender == Source);
  8378. if (Accept)
  8379. {
  8380. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8381. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8382. // do not allow dragging to the "+" tab
  8383. Accept = (Terminal != NULL);
  8384. }
  8385. }
  8386. //---------------------------------------------------------------------------
  8387. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(int /*KeyState*/,
  8388. const TPoint & Point, int & Effect)
  8389. {
  8390. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  8391. if (Index < 0)
  8392. {
  8393. Effect = DROPEFFECT_None;
  8394. }
  8395. else
  8396. {
  8397. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8398. // do not allow dropping on the "+" tab
  8399. if (TargetTerminal == NULL)
  8400. {
  8401. Effect = DROPEFFECT_None;
  8402. }
  8403. }
  8404. }
  8405. //---------------------------------------------------------------------------
  8406. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  8407. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  8408. {
  8409. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  8410. // do not allow dropping on the "+" tab
  8411. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8412. if (TargetTerminal != NULL)
  8413. {
  8414. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  8415. if (!IsFileControl(DropSourceControl, osRemote))
  8416. {
  8417. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  8418. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  8419. // Why don't we use Terminal->CurrentDirectory directly?
  8420. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory, false);
  8421. }
  8422. }
  8423. }
  8424. //---------------------------------------------------------------------------
  8425. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  8426. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  8427. {
  8428. // Downloads are handled in RemoteFileControlDDEnd
  8429. if (!IsFileControl(DropSourceControl, osRemote))
  8430. {
  8431. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  8432. Terminal->CurrentDirectory,
  8433. // force queue
  8434. true);
  8435. }
  8436. }
  8437. //---------------------------------------------------------------------------
  8438. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  8439. {
  8440. FShowing = false;
  8441. // Do not save empty workspace
  8442. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  8443. {
  8444. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  8445. UnicodeString Name = WorkspaceName();
  8446. DoSaveWorkspace(
  8447. Name, DataList.get(),
  8448. !Configuration->DisablePasswordStoring &&
  8449. WinConfiguration->AutoSaveWorkspacePasswords);
  8450. WinConfiguration->LastStoredSession = Name;
  8451. }
  8452. }
  8453. //---------------------------------------------------------------------------
  8454. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  8455. {
  8456. TManagedTerminal * ManagedTerminal =
  8457. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  8458. if (ManagedTerminal != NULL)
  8459. {
  8460. ManagedTerminal->DirectoryLoaded = Now();
  8461. }
  8462. }
  8463. //---------------------------------------------------------------------------
  8464. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  8465. TListItem * /*Item*/, bool /*Selected*/)
  8466. {
  8467. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8468. switch (DirView->LastSelectMethod)
  8469. {
  8470. case smKeyboard:
  8471. Configuration->Usage->Inc(L"KeyboardSelections");
  8472. break;
  8473. case smMouse:
  8474. Configuration->Usage->Inc(L"MouseSelections");
  8475. break;
  8476. }
  8477. }
  8478. //---------------------------------------------------------------------------
  8479. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  8480. {
  8481. if (Color != 0)
  8482. {
  8483. AddSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  8484. return FSessionColors->Count - 1;
  8485. }
  8486. else
  8487. {
  8488. return FSessionTabImageIndex;
  8489. }
  8490. }
  8491. //---------------------------------------------------------------------------
  8492. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  8493. {
  8494. TPngImageCollectionItem * Item =
  8495. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  8496. FSessionColors->AddPng(Item->PngImage);
  8497. return FSessionColors->Count - 1;
  8498. }
  8499. //---------------------------------------------------------------------------
  8500. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  8501. {
  8502. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  8503. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  8504. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  8505. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  8506. }
  8507. //---------------------------------------------------------------------------
  8508. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  8509. {
  8510. for (int Index = 0; Index < Control->ControlCount; Index++)
  8511. {
  8512. TControl * AControl = Control->Controls[Index];
  8513. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  8514. if (WinControl != NULL)
  8515. {
  8516. CollectItemsWithTextDisplayMode(WinControl);
  8517. }
  8518. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  8519. if (Toolbar != NULL)
  8520. {
  8521. // we care for top-level items only
  8522. TTBCustomItem * Items = Toolbar->Items;
  8523. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  8524. {
  8525. TTBCustomItem * Item = Items->Items[ItemIndex];
  8526. if (((Item->DisplayMode == nbdmImageAndText) ||
  8527. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  8528. EligibleForImageDisplayMode(Item))
  8529. {
  8530. FItemsWithTextDisplayMode.insert(Item);
  8531. }
  8532. }
  8533. }
  8534. }
  8535. }
  8536. //---------------------------------------------------------------------------
  8537. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  8538. {
  8539. return true;
  8540. }
  8541. //---------------------------------------------------------------------------
  8542. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  8543. {
  8544. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  8545. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  8546. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  8547. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  8548. bool Result = true;
  8549. while (Result && (i != FItemsWithTextDisplayMode.end()))
  8550. {
  8551. TTBCustomItem * Item = *i;
  8552. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  8553. if (Label != NULL)
  8554. {
  8555. // optimization
  8556. if (Label->Visible == SelectiveToolbarText)
  8557. {
  8558. Result = false;
  8559. }
  8560. else
  8561. {
  8562. Label->Visible = SelectiveToolbarText;
  8563. }
  8564. }
  8565. else
  8566. {
  8567. // optimization
  8568. if (Item->DisplayMode == DisplayMode)
  8569. {
  8570. Result = false;
  8571. }
  8572. else
  8573. {
  8574. Item->DisplayMode = DisplayMode;
  8575. }
  8576. }
  8577. ++i;
  8578. }
  8579. if (Result)
  8580. {
  8581. UpdateNewSessionTab();
  8582. }
  8583. return Result;
  8584. }
  8585. //---------------------------------------------------------------------------
  8586. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  8587. {
  8588. DebugFail();
  8589. }
  8590. //---------------------------------------------------------------------------
  8591. bool __fastcall TCustomScpExplorerForm::IsBusy()
  8592. {
  8593. // Among other, a lock level is non zero, while directory is being loaded,
  8594. // even when it happens as a result of dirview navigation,
  8595. // i.e. when TNonVisualDataModule is NOT FBusy.
  8596. // That's why the TNonVisualDataModule::GetBusy calls this method.
  8597. // Among other this prevents a panel auto update to occur while
  8598. // directory is changing.
  8599. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  8600. }
  8601. //---------------------------------------------------------------------------
  8602. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  8603. {
  8604. // While the window is disabled, we still seem to process menu shortcuts at least,
  8605. // so stop it at least here.
  8606. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  8607. return
  8608. (Allowed == aaUpdate) ||
  8609. !NonVisualDataModule->Busy;
  8610. }
  8611. //---------------------------------------------------------------------------
  8612. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  8613. {
  8614. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  8615. }
  8616. //---------------------------------------------------------------------------
  8617. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  8618. {
  8619. // This is somewhat redundant to LockWindow() call from
  8620. // TTerminalManager::TerminalReadDirectoryProgress.
  8621. // But disabling window is known not to block keyboard shorcuts
  8622. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  8623. if (Busy > 0)
  8624. {
  8625. if (NonVisualDataModule->Busy)
  8626. {
  8627. State = false;
  8628. }
  8629. else
  8630. {
  8631. NonVisualDataModule->StartBusy();
  8632. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  8633. LockWindow();
  8634. }
  8635. }
  8636. else if (Busy < 0)
  8637. {
  8638. UnlockWindow();
  8639. NonVisualDataModule->EndBusy();
  8640. }
  8641. else
  8642. {
  8643. State = NonVisualDataModule->Busy;
  8644. }
  8645. }
  8646. //---------------------------------------------------------------------------
  8647. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  8648. {
  8649. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  8650. // no context menu for "New session tab"
  8651. if ((Index >= 0) && (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != NULL))
  8652. {
  8653. SessionsPageControl->ActivePageIndex = Index;
  8654. if (DebugAlwaysTrue(SessionTabSwitched()))
  8655. {
  8656. // copied from TControl.WMContextMenu
  8657. SendCancelMode(SessionsPageControl);
  8658. // explicit popup instead of using PopupMenu property
  8659. // to avoid menu to popup somewhere within SessionTabSwitched above,
  8660. // while connecting yet not-connected session and hence
  8661. // allowing an access to commands over not-completelly connected session
  8662. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  8663. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  8664. PopupMenu->PopupComponent = SessionsPageControl;
  8665. PopupMenu->Popup(Point.x, Point.y);
  8666. }
  8667. }
  8668. Handled = true;
  8669. }
  8670. //---------------------------------------------------------------------------
  8671. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  8672. {
  8673. return (Terminal != NULL) && Terminal->Active && Terminal->IsCapable[fcChangePassword];
  8674. }
  8675. //---------------------------------------------------------------------------
  8676. void __fastcall TCustomScpExplorerForm::ChangePassword()
  8677. {
  8678. Configuration->Usage->Inc(L"PasswordChanges");
  8679. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  8680. ChangePasswordData->DisableAuthentationsExceptPassword();
  8681. ChangePasswordData->ChangePassword = true;
  8682. std::unique_ptr<TTerminal> ChangePasswordSession(
  8683. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  8684. ChangePasswordSession->AutoReadDirectory = false;
  8685. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  8686. {
  8687. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  8688. }
  8689. else
  8690. {
  8691. Configuration->Usage->Inc(L"PasswordChangesFailed");
  8692. }
  8693. }
  8694. //---------------------------------------------------------------------------
  8695. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  8696. {
  8697. TForm::ChangeScale(M, D);
  8698. int APixelsPerInch = GetControlPixelsPerInch(this);
  8699. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  8700. }