CustomScpExplorer.cpp 330 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130
  1. //---------------------------------------------------------------------------
  2. #define NO_WIN32_LEAN_AND_MEAN
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include <Common.h>
  6. #include "CustomScpExplorer.h"
  7. #include <Bookmarks.h>
  8. #include <Interface.h>
  9. #include <Exceptions.h>
  10. #include <CoreMain.h>
  11. #include <FileSystems.h>
  12. #include <TextsCore.h>
  13. #include <TextsWin.h>
  14. #include <HelpWin.h>
  15. #include <VCLCommon.h>
  16. #include <Progress.h>
  17. #include <SynchronizeProgress.h>
  18. #include <DragExt.h>
  19. #include <WinApi.h>
  20. #include "GUITools.h"
  21. #include "NonVisual.h"
  22. #include "Glyphs.h"
  23. #include "Tools.h"
  24. #include "WinConfiguration.h"
  25. #include "TerminalManager.h"
  26. #include "EditorManager.h"
  27. #include "ProgParams.h"
  28. #include "Setup.h"
  29. #include <Consts.hpp>
  30. #include <DateUtils.hpp>
  31. #include <TB2Common.hpp>
  32. #include <DirectoryMonitor.hpp>
  33. //---------------------------------------------------------------------------
  34. #pragma package(smart_init)
  35. #pragma link "CustomDirView"
  36. #pragma link "CustomUnixDirView"
  37. #pragma link "IEListView"
  38. #pragma link "NortonLikeListView"
  39. #pragma link "UnixDirView"
  40. #pragma link "CustomDriveView"
  41. #pragma link "UnixDriveView"
  42. #pragma link "CustomDriveView"
  43. #pragma link "TB2Dock"
  44. #pragma link "TBXStatusBars"
  45. #pragma link "TB2Item"
  46. #pragma link "TB2Toolbar"
  47. #pragma link "ThemePageControl"
  48. #pragma link "PathLabel"
  49. #ifndef NO_RESOURCES
  50. #pragma resource "*.dfm"
  51. #endif
  52. //---------------------------------------------------------------------------
  53. #define SAVE_SELECTION(DIRVIEW) \
  54. UnicodeString FocusFile = L""; \
  55. UnicodeString LastFocusedFile = L""; \
  56. if (DIRVIEW->ItemFocused) LastFocusedFile = DIRVIEW->ItemFocused->Caption; \
  57. { TListItem * ClosestUnselected = DIRVIEW->ClosestUnselected(DIRVIEW->ItemFocused); \
  58. if (ClosestUnselected) FocusFile = ClosestUnselected->Caption; }
  59. #define RESTORE_SELECTION(DIRVIEW) \
  60. if (!LastFocusedFile.IsEmpty() && \
  61. (!DIRVIEW->ItemFocused || (DIRVIEW->ItemFocused->Caption != LastFocusedFile))) \
  62. { \
  63. TListItem *ItemToSelect = DIRVIEW->FindFileItem(FocusFile); \
  64. if (ItemToSelect) \
  65. { \
  66. DIRVIEW->ItemFocused = ItemToSelect; \
  67. DIRVIEW->ItemFocused->MakeVisible(False); \
  68. } \
  69. }
  70. //---------------------------------------------------------------------------
  71. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  72. static const int SessionPanelCount = 4;
  73. //---------------------------------------------------------------------------
  74. class TMutexGuard
  75. {
  76. public:
  77. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  78. int Timeout = 5000)
  79. {
  80. FMutex = NULL;
  81. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  82. if (WaitResult == WAIT_TIMEOUT)
  83. {
  84. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  85. }
  86. else
  87. {
  88. FMutex = AMutex;
  89. }
  90. }
  91. ~TMutexGuard()
  92. {
  93. if (FMutex != NULL)
  94. {
  95. ReleaseMutex(FMutex);
  96. }
  97. }
  98. private:
  99. HANDLE FMutex;
  100. };
  101. //---------------------------------------------------------------------------
  102. class TWindowLock
  103. {
  104. public:
  105. TWindowLock(TCustomScpExplorerForm * Form) :
  106. FForm(Form)
  107. {
  108. FForm->LockWindow();
  109. }
  110. ~TWindowLock()
  111. {
  112. FForm->UnlockWindow();
  113. }
  114. private:
  115. TCustomScpExplorerForm * FForm;
  116. };
  117. //---------------------------------------------------------------------------
  118. class TAutoBatch
  119. {
  120. public:
  121. TAutoBatch(TCustomScpExplorerForm * Form) :
  122. FForm(Form)
  123. {
  124. FForm->BatchStart(FBatchStorage);
  125. }
  126. ~TAutoBatch()
  127. {
  128. FForm->BatchEnd(FBatchStorage);
  129. }
  130. private:
  131. TCustomScpExplorerForm * FForm;
  132. void * FBatchStorage;
  133. };
  134. //---------------------------------------------------------------------------
  135. struct TTransferOperationParam
  136. {
  137. TTransferOperationParam();
  138. UnicodeString TargetDirectory;
  139. bool Temp;
  140. bool DragDrop;
  141. int Options;
  142. TAutoSwitch Queue;
  143. };
  144. //---------------------------------------------------------------------------
  145. TTransferOperationParam::TTransferOperationParam()
  146. {
  147. Temp = false;
  148. DragDrop = false;
  149. Options = 0;
  150. Queue = asAuto;
  151. }
  152. //---------------------------------------------------------------------------
  153. class TTransferPresetNoteData : public TObject
  154. {
  155. public:
  156. UnicodeString Message;
  157. };
  158. //---------------------------------------------------------------------------
  159. class TTerminalNoteData : public TObject
  160. {
  161. public:
  162. TTerminal * Terminal;
  163. };
  164. //---------------------------------------------------------------------------
  165. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  166. FFormRestored(false),
  167. TForm(Owner)
  168. {
  169. FCurrentSide = osRemote;
  170. FEverShown = false;
  171. FDocks = new TList();
  172. RestoreParams();
  173. ConfigurationChanged();
  174. RemoteDirView->Invalidate();
  175. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  176. NonVisualDataModule->ScpExplorer = this;
  177. FAutoOperation = false;
  178. FOnFileOperationFinished = NULL;
  179. FForceExecution = false;
  180. FIgnoreNextDialogChar = 0;
  181. FErrorList = NULL;
  182. FSynchronizeProgressForm = NULL;
  183. FProgressForm = NULL;
  184. FRefreshLocalDirectory = false;
  185. FRefreshRemoteDirectory = false;
  186. FDDMoveSlipped = false;
  187. FDDExtMapFile = NULL;
  188. // CreateMutexW keeps failing with ERROR_NOACCESS
  189. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  190. DebugAssert(FDDExtMutex != NULL);
  191. FDDTargetControl = NULL;
  192. FDelayedDeletionTimer = NULL;
  193. FDelayedDeletionList = new TStringList();
  194. FDDFileList = NULL;
  195. FPendingTempSpaceWarn = false;
  196. FCapturedLog = NULL;
  197. FDragDropOperation = false;
  198. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  199. FAllowTransferPresetAutoSelect = true;
  200. FCopyParamDefault = L"";
  201. FSynchronizeController = NULL;
  202. FPendingQueueActionItem = NULL;
  203. FLockLevel = 0;
  204. FLockSuspendLevel = 0;
  205. FDisabledOnLockSuspend = false;
  206. FAlternativeDelete = false;
  207. FTrayIcon = new ::TTrayIcon(0);
  208. FTrayIcon->OnClick = TrayIconClick;
  209. FMaxQueueLength = 0;
  210. FLastContextPopupScreenPoint = TPoint(-1, -1);
  211. FTransferResumeList = NULL;
  212. FMoveToQueue = false;
  213. FStandaloneEditing = false;
  214. FOnFeedSynchronizeError = NULL;
  215. FNeedSession = false;
  216. FDoNotIdleCurrentTerminal = 0;
  217. FEditorManager = new TEditorManager();
  218. FEditorManager->OnFileChange = ExecutedFileChanged;
  219. FEditorManager->OnFileReload = ExecutedFileReload;
  220. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  221. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  222. FLocalEditors = new TList();
  223. FQueueStatus = NULL;
  224. FQueueStatusSection = new TCriticalSection();
  225. FQueueStatusInvalidated = false;
  226. FQueueItemInvalidated = false;
  227. FQueueActedItem = NULL;
  228. FQueueController = new TQueueController(QueueView3);
  229. FUserActionTimer = new TTimer(this);
  230. FUserActionTimer->Enabled = false;
  231. FUserActionTimer->Interval = 10;
  232. FUserActionTimer->OnTimer = UserActionTimer;
  233. FNotes = new TStringList();
  234. FNoteTimer = new TTimer(this);
  235. FNoteTimer->Enabled = false;
  236. FNoteTimer->OnTimer = NoteTimer;
  237. FOnNoteClick = NULL;
  238. FOle32Library = LoadLibrary(L"Ole32.dll");
  239. FDragMoveCursor = FOle32Library != NULL ?
  240. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  241. UseSystemSettings(this);
  242. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  243. static_cast<TObject*>(GetComponent(fcTransferList)));
  244. DebugAssert(TransferList != NULL);
  245. FTransferListHoverIndex = -1;
  246. TransferList->OnChange = TransferListChange;
  247. TransferList->OnDrawItem = TransferListDrawItem;
  248. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))));
  249. SetSubmenu(NonVisualDataModule->ColorMenuItem);
  250. UseDesktopFont(SessionsPageControl);
  251. UpdateSessionsPageControlHeight();
  252. UseDesktopFont(RemoteDirView);
  253. UseDesktopFont(RemoteDriveView);
  254. UseDesktopFont(QueueView3);
  255. UseDesktopFont(QueueLabel);
  256. UseDesktopFont(RemoteStatusBar);
  257. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  258. QueueSplitter->ShowHint = true;
  259. RemotePanelSplitter->ShowHint = true;
  260. UpdateImages();
  261. FCustomCommandMenu = CreateTBXPopupMenu(this);
  262. FCustomCommandLocalFileList = NULL;
  263. FCustomCommandRemoteFileList = NULL;
  264. FSessionColors = new TPngImageList(this);
  265. FSessionColors->ColorDepth = cd32Bit;
  266. AddFixedSessionImages();
  267. SessionsPageControl->Images = FSessionColors;
  268. QueueLabel->FocusControl = QueueView3;
  269. UpdateQueueLabel();
  270. FRemoteDirViewWasFocused = true;
  271. CreateHiddenWindow();
  272. StartUpdates();
  273. }
  274. //---------------------------------------------------------------------------
  275. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  276. {
  277. // this has to be one of the very first things to do
  278. StopUpdateThread();
  279. SessionsPageControl->Images = NULL;
  280. SAFE_DESTROY(FSessionColors);
  281. SAFE_DESTROY(FSessionsDragDropFilesEx);
  282. delete FCustomCommandLocalFileList;
  283. delete FCustomCommandRemoteFileList;
  284. delete FCustomCommandMenu;
  285. delete FTrayIcon;
  286. FTrayIcon = NULL;
  287. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  288. delete FEditorManager;
  289. ForceCloseLocalEditors();
  290. delete FLocalEditors;
  291. HideFileFindDialog();
  292. if (FDelayedDeletionTimer)
  293. {
  294. DoDelayedDeletion(NULL);
  295. SAFE_DESTROY(FDelayedDeletionTimer);
  296. }
  297. SAFE_DESTROY(FDelayedDeletionList);
  298. // sometimes we do not get DDEnd so the list is not released
  299. SAFE_DESTROY(FDDFileList);
  300. DebugAssert(FSynchronizeController == NULL);
  301. CloseHandle(FDDExtMutex);
  302. FDDExtMutex = NULL;
  303. FreeLibrary(FOle32Library);
  304. FOle32Library = NULL;
  305. FDragMoveCursor = NULL;
  306. DebugAssert(!FErrorList);
  307. if (FEverShown)
  308. {
  309. // when window is never shown (like when running command-line operation),
  310. // particularly window site is not restored correctly (BoundsRect value set
  311. // in RestoreForm gets lost during handle allocation), so we do not want
  312. // it to be stored
  313. StoreParams();
  314. }
  315. Terminal = NULL;
  316. Queue = NULL;
  317. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  318. NonVisualDataModule->ScpExplorer = NULL;
  319. delete FQueueController;
  320. FQueueController = NULL;
  321. delete FQueueStatusSection;
  322. FQueueStatusSection = NULL;
  323. delete FQueueStatus;
  324. FQueueStatus = NULL;
  325. delete FUserActionTimer;
  326. FUserActionTimer = NULL;
  327. delete FNoteTimer;
  328. delete FNotes;
  329. delete FNoteData;
  330. SAFE_DESTROY(FDocks);
  331. SAFE_DESTROY(FHistoryMenu[0][0]);
  332. SAFE_DESTROY(FHistoryMenu[0][1]);
  333. SAFE_DESTROY(FHistoryMenu[1][0]);
  334. SAFE_DESTROY(FHistoryMenu[1][1]);
  335. if (FHiddenWindow != NULL)
  336. {
  337. DestroyWindow(FHiddenWindow);
  338. FHiddenWindow = NULL;
  339. }
  340. }
  341. //---------------------------------------------------------------------------
  342. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  343. {
  344. std::unique_ptr<TSessionData> Data;
  345. if (!Session.IsEmpty())
  346. {
  347. bool DefaultsOnly;
  348. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  349. }
  350. TTerminalManager * Manager = TTerminalManager::Instance();
  351. for (int Index = 0; Index < Manager->Count; Index++)
  352. {
  353. TTerminal * Terminal = Manager->Terminals[Index];
  354. if (Session.IsEmpty() ||
  355. Terminal->SessionData->IsSameSite(Data.get()))
  356. {
  357. if (Path.IsEmpty())
  358. {
  359. Terminal->ClearCaches();
  360. }
  361. else
  362. {
  363. Terminal->DirectoryModified(Path, true);
  364. }
  365. }
  366. }
  367. // We should flag a pending refresh for the background terminals or busy foreground terminals
  368. if ((Terminal != NULL) && Terminal->Active &&
  369. CanCommandLineFromAnotherInstance() &&
  370. (Session.IsEmpty() || Terminal->SessionData->IsSameSite(Data.get())) &&
  371. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  372. {
  373. Terminal->ReloadDirectory();
  374. }
  375. }
  376. //---------------------------------------------------------------------------
  377. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  378. {
  379. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  380. size_t MessageSize = sizeof(TCopyDataMessage);
  381. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  382. if (Result)
  383. {
  384. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  385. Result = (Message.Version == TCopyDataMessage::Version1);
  386. if (Result)
  387. {
  388. switch (Message.Command)
  389. {
  390. case TCopyDataMessage::CommandCanCommandLine:
  391. Result = CanCommandLineFromAnotherInstance();
  392. break;
  393. case TCopyDataMessage::CommandCommandLine:
  394. {
  395. UnicodeString CommandLine(Message.CommandLine);
  396. Result = CommandLineFromAnotherInstance(CommandLine);
  397. }
  398. break;
  399. case TCopyDataMessage::RefreshPanel:
  400. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  401. break;
  402. case TCopyDataMessage::MainWindowCheck:
  403. Result = true;
  404. break;
  405. default:
  406. Result = false;
  407. break;
  408. }
  409. }
  410. }
  411. Message.Result = Result ? 1 : 0;
  412. }
  413. //---------------------------------------------------------------------------
  414. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  415. {
  416. WNDCLASS WindowClass = {0};
  417. WindowClass.lpfnWndProc = DefWindowProc;
  418. WindowClass.hInstance = HInstance;
  419. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  420. FHiddenWindow = NULL;
  421. if (RegisterClass(&WindowClass))
  422. {
  423. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  424. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  425. }
  426. }
  427. //---------------------------------------------------------------------------
  428. bool __fastcall TCustomScpExplorerForm::CanConsole()
  429. {
  430. return (Terminal != NULL) && (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  431. }
  432. //---------------------------------------------------------------------------
  433. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  434. {
  435. bool Result = Visible && !NonVisualDataModule->Busy;
  436. return Result;
  437. }
  438. //---------------------------------------------------------------------------
  439. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  440. const UnicodeString & CommandLine)
  441. {
  442. TProgramParams Params(CommandLine);
  443. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  444. if (Result)
  445. {
  446. NonVisualDataModule->StartBusy();
  447. try
  448. {
  449. // this action is initiated from another process,
  450. // so it's likely that our window is not visible,
  451. // and user won't see what is going on
  452. Application->BringToFront();
  453. // reload sessions as we may be asked to open a session
  454. // just stored by another instance
  455. StoredSessions->Reload();
  456. UnicodeString SessionName = Params.Param[1];
  457. std::unique_ptr<TObjectList> DataList(new TObjectList());
  458. UnicodeString DownloadFile; // unused
  459. try
  460. {
  461. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this);
  462. }
  463. catch (EAbort &)
  464. {
  465. // Happens when opening session in PuTTY and in other situations,
  466. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  467. // to process the command-line on its own (or to pass it to yet another instance)
  468. }
  469. if (DataList->Count > 0)
  470. {
  471. TTerminalManager * Manager = TTerminalManager::Instance();
  472. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  473. }
  474. }
  475. __finally
  476. {
  477. NonVisualDataModule->EndBusy();
  478. }
  479. }
  480. return Result;
  481. }
  482. //---------------------------------------------------------------------------
  483. void __fastcall TCustomScpExplorerForm::SetTerminal(TTerminal * value)
  484. {
  485. if (FTerminal != value)
  486. {
  487. TerminalChanging();
  488. FTerminal = value;
  489. bool PrevAllowTransferPresetAutoSelect = FAllowTransferPresetAutoSelect;
  490. FAllowTransferPresetAutoSelect = false;
  491. try
  492. {
  493. TerminalChanged();
  494. }
  495. __finally
  496. {
  497. FAllowTransferPresetAutoSelect = PrevAllowTransferPresetAutoSelect;
  498. }
  499. if (Terminal != NULL)
  500. {
  501. TransferPresetAutoSelect();
  502. }
  503. }
  504. }
  505. //---------------------------------------------------------------------------
  506. void __fastcall TCustomScpExplorerForm::TerminalChanging()
  507. {
  508. if (FTerminal != NULL)
  509. {
  510. UpdateTerminal(Terminal);
  511. }
  512. }
  513. //---------------------------------------------------------------------------
  514. void __fastcall TCustomScpExplorerForm::TerminalChanged()
  515. {
  516. RemoteDirView->Terminal = Terminal;
  517. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  518. TManagedTerminal * ManagedTerminal = NULL;
  519. if (Terminal != NULL)
  520. {
  521. if (Terminal->Active)
  522. {
  523. Terminal->RefreshDirectory();
  524. }
  525. ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  526. DebugAssert(ManagedTerminal != NULL);
  527. if (WinConfiguration->PreservePanelState)
  528. {
  529. if (ManagedTerminal->RemoteExplorerState != NULL)
  530. {
  531. RemoteDirView->RestoreState(ManagedTerminal->RemoteExplorerState);
  532. }
  533. else
  534. {
  535. RemoteDirView->ClearState();
  536. }
  537. }
  538. InitStatusBar();
  539. }
  540. DoTerminalListChanged();
  541. if (ManagedTerminal != NULL)
  542. {
  543. // this has to be set only after the tab is switched from DoTerminalListChanged,
  544. // otherwise we are changing color of wrong tab
  545. SessionColor = (TColor)ManagedTerminal->StateData->Color;
  546. }
  547. UpdateTransferList();
  548. // Update panels Enable state before refreshing the labels
  549. UpdateControls();
  550. }
  551. //---------------------------------------------------------------------------
  552. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  553. {
  554. if (Queue != value)
  555. {
  556. if (FQueue != NULL)
  557. {
  558. FQueue->OnListUpdate = NULL;
  559. FQueue->OnQueueItemUpdate = NULL;
  560. }
  561. FQueue = value;
  562. if (FQueue != NULL)
  563. {
  564. DebugAssert(FQueue->OnListUpdate == NULL);
  565. FQueue->OnListUpdate = QueueListUpdate;
  566. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  567. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  568. }
  569. QueueChanged();
  570. }
  571. }
  572. //---------------------------------------------------------------------------
  573. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  574. TListItem * Item)
  575. {
  576. if (FQueueActedItem == Item)
  577. {
  578. FQueueActedItem = NULL;
  579. if ((QueueView3->PopupMenu != NULL) &&
  580. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  581. {
  582. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  583. ReleaseCapture();
  584. }
  585. }
  586. if (Item->Data == FPendingQueueActionItem)
  587. {
  588. FPendingQueueActionItem = NULL;
  589. }
  590. }
  591. //---------------------------------------------------------------------------
  592. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  593. {
  594. {
  595. TGuard Guard(FQueueStatusSection);
  596. FQueueStatusInvalidated = false;
  597. if (FQueue != NULL)
  598. {
  599. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  600. }
  601. }
  602. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  603. {
  604. FMaxQueueLength = FQueueStatus->Count;
  605. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  606. }
  607. FQueueController->UpdateQueueStatus(FQueueStatus);
  608. SetQueueProgress();
  609. UpdateQueueView();
  610. bool IsEmpty = (FQueueStatus == NULL) || (FQueueStatus->Count == 0);
  611. if (IsEmpty && (Terminal != NULL))
  612. {
  613. TOnceDoneOperation OnceDoneOperation =
  614. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  615. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  616. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  617. {
  618. FQueue->Enabled = false;
  619. }
  620. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  621. {
  622. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  623. }
  624. }
  625. }
  626. //---------------------------------------------------------------------------
  627. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  628. {
  629. UnicodeString Result;
  630. if (FQueueStatus != NULL)
  631. {
  632. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  633. {
  634. TFileOperationProgressType * ProgressData =
  635. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  636. if ((ProgressData != NULL) && ProgressData->InProgress)
  637. {
  638. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  639. }
  640. }
  641. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  642. {
  643. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  644. }
  645. }
  646. return Result;
  647. }
  648. //---------------------------------------------------------------------------
  649. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  650. {
  651. ComponentVisible[fcQueueView] =
  652. (WinConfiguration->QueueView.Show == qvShow) ||
  653. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  654. (FQueueStatus != NULL) && (FQueueStatus->Count > 0));
  655. }
  656. //---------------------------------------------------------------------------
  657. void __fastcall TCustomScpExplorerForm::QueueChanged()
  658. {
  659. if (FQueueStatus != NULL)
  660. {
  661. delete FQueueStatus;
  662. FQueueStatus = NULL;
  663. }
  664. UpdateQueueStatus(true);
  665. }
  666. //---------------------------------------------------------------------------
  667. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  668. {
  669. if (FQueue == Queue)
  670. {
  671. FQueueStatusInvalidated = true;
  672. }
  673. }
  674. //---------------------------------------------------------------------------
  675. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  676. TQueueItem * Item)
  677. {
  678. if (FQueue == Queue)
  679. {
  680. TGuard Guard(FQueueStatusSection);
  681. // this may be running in parallel with QueueChanged
  682. if (FQueueStatus != NULL)
  683. {
  684. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  685. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  686. {
  687. FRefreshLocalDirectory = (QueueItem == NULL) ||
  688. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  689. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  690. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  691. }
  692. if (QueueItem != NULL)
  693. {
  694. QueueItem->UserData = (void*)true;
  695. FQueueItemInvalidated = true;
  696. }
  697. }
  698. }
  699. }
  700. //---------------------------------------------------------------------------
  701. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  702. {
  703. // during standalone editing, we have no way to see/control queue,
  704. // so we have to always popup prompts automatically
  705. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  706. }
  707. //---------------------------------------------------------------------------
  708. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  709. {
  710. if (FQueueStatus != NULL)
  711. {
  712. bool QueueAutoPopup = IsQueueAutoPopup();
  713. bool NeedRefresh = FQueueController->NeedRefresh();
  714. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  715. FQueueItemInvalidated = false;
  716. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  717. bool Updated = false;
  718. TQueueItemProxy * QueueItem;
  719. bool WasUserAction;
  720. for (int Index = 0; Index < Limit; Index++)
  721. {
  722. bool Update = false;
  723. QueueItem = FQueueStatus->Items[Index];
  724. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  725. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  726. (FPendingQueueActionItem == NULL))
  727. {
  728. FPendingQueueActionItem = QueueItem;
  729. FUserActionTimer->Enabled = true;
  730. }
  731. if ((bool)QueueItem->UserData)
  732. {
  733. QueueItem->UserData = (void*)false;
  734. QueueItem->Update();
  735. Updated = true;
  736. Update = true;
  737. }
  738. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  739. {
  740. Update = true;
  741. }
  742. if (Update)
  743. {
  744. FQueueController->RefreshQueueItem(QueueItem);
  745. }
  746. }
  747. if (Updated)
  748. {
  749. NonVisualDataModule->UpdateNonVisibleActions();
  750. SetQueueProgress();
  751. }
  752. }
  753. }
  754. //---------------------------------------------------------------------------
  755. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  756. {
  757. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  758. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  759. }
  760. //---------------------------------------------------------------------------
  761. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  762. {
  763. if (ProgressData->Operation != foCalculateSize)
  764. {
  765. int OverallProgress;
  766. // FProgressForm is null when this is called from SetQueueProgress
  767. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  768. {
  769. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  770. }
  771. else
  772. {
  773. OverallProgress = ProgressData->OverallProgress();
  774. }
  775. // implies TBPF_NORMAL
  776. FTaskbarList->SetProgressValue(GetMainForm()->Handle, OverallProgress, 100);
  777. }
  778. else
  779. {
  780. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  781. }
  782. }
  783. //---------------------------------------------------------------------------
  784. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  785. {
  786. TTerminalManager::Instance()->QueueStatusUpdated();
  787. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  788. {
  789. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  790. {
  791. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  792. {
  793. TFileOperationProgressType * ProgressData = NULL;
  794. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  795. {
  796. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  797. }
  798. if ((ProgressData != NULL) &&
  799. ProgressData->InProgress)
  800. {
  801. SetTaskbarListProgressValue(ProgressData);
  802. }
  803. else
  804. {
  805. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  806. }
  807. }
  808. else
  809. {
  810. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  811. }
  812. }
  813. else
  814. {
  815. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  816. }
  817. }
  818. UpdateQueueLabel();
  819. }
  820. //---------------------------------------------------------------------------
  821. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  822. {
  823. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  824. if (FQueueStatus != NULL)
  825. {
  826. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  827. if (ActiveAndPendingPrimaryCount > 0)
  828. {
  829. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  830. }
  831. }
  832. QueueLabel->Caption = Caption;
  833. }
  834. //---------------------------------------------------------------------------
  835. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  836. {
  837. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  838. static_cast<TComponent*>(GetComponent(fcTransferList)));
  839. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  840. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  841. TransferList->Strings->BeginUpdate();
  842. try
  843. {
  844. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  845. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  846. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  847. if (FTransferDropDownHint.IsEmpty())
  848. {
  849. FTransferDropDownHint = TransferDropDown->Hint;
  850. }
  851. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  852. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  853. TransferDropDown->Text = StripHotkey(Name);
  854. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  855. (FTransferDropDownHint, StripHotkey(Name),
  856. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  857. FLAGMASK(Terminal != NULL, Terminal->UsableCopyParamAttrs(0).General))));
  858. // update the label, otherwise when it is updated only on the first draw
  859. // of the list, it is drawn "bold" for some reason
  860. FTransferListHoverIndex = TransferList->ItemIndex;
  861. UpdateTransferLabel();
  862. }
  863. __finally
  864. {
  865. TransferList->Strings->EndUpdate();
  866. }
  867. }
  868. //---------------------------------------------------------------------------
  869. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  870. {
  871. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  872. DebugAssert(Toolbar != NULL);
  873. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  874. }
  875. //---------------------------------------------------------------------------
  876. void __fastcall TCustomScpExplorerForm::UpdateActions()
  877. {
  878. TForm::UpdateActions();
  879. if (ComponentVisible[fcCustomCommandsBand])
  880. {
  881. UpdateCustomCommandsToolbar();
  882. }
  883. }
  884. //---------------------------------------------------------------------------
  885. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  886. {
  887. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  888. }
  889. //---------------------------------------------------------------------------
  890. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  891. {
  892. DebugAssert(Configuration && RemoteDirView);
  893. RemoteDirView->DDAllowMove = WinConfiguration->DDAllowMoveInit;
  894. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  895. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  896. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  897. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  898. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  899. if (RemoteDirView->RowSelect != WinConfiguration->FullRowSelect)
  900. {
  901. RemoteDirView->RowSelect = WinConfiguration->FullRowSelect;
  902. // selection is not redrawn automatically when RowSelect changes
  903. RemoteDirView->Invalidate();
  904. }
  905. RemoteDriveView->DDAllowMove = WinConfiguration->DDAllowMoveInit;
  906. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  907. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  908. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  909. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  910. UpdateSessionsPageControlHeight();
  911. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  912. UpdateToolbarDisplayMode();
  913. UpdateTransferList();
  914. if (Terminal != NULL)
  915. {
  916. TransferPresetAutoSelect();
  917. }
  918. UpdateQueueView();
  919. UpdateControls();
  920. // this can be called even before constuctor finishes.
  921. if (FEditorManager != NULL)
  922. {
  923. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  924. }
  925. // this can be called even before constuctor finishes.
  926. if (FLocalEditors != NULL)
  927. {
  928. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  929. {
  930. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  931. }
  932. }
  933. if (ComponentVisible[fcCustomCommandsBand])
  934. {
  935. UpdateCustomCommandsToolbar();
  936. }
  937. // show only when keeping queue items forever,
  938. // otherwise, its enough to have in in the context menu
  939. QueueDeleteAllDoneQueueToolbarItem->Visible =
  940. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  941. }
  942. //---------------------------------------------------------------------------
  943. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  944. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  945. void * /*Arg*/)
  946. {
  947. if (Token != NULL)
  948. {
  949. TForm * Editor = dynamic_cast<TForm*>(Token);
  950. DebugAssert(Editor != NULL);
  951. ReconfigureEditorForm(Editor);
  952. }
  953. }
  954. //---------------------------------------------------------------------------
  955. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  956. {
  957. WinConfiguration->DDTransferConfirmation = asOn;
  958. }
  959. //---------------------------------------------------------------------------
  960. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  961. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  962. {
  963. // noop
  964. }
  965. //---------------------------------------------------------------------------
  966. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  967. TTransferDirection Direction, TTransferType Type, bool Temp,
  968. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  969. bool Confirm, bool DragDrop, int Options)
  970. {
  971. bool Result = true;
  972. DebugAssert(Terminal && Terminal->Active);
  973. // Temp means d&d here so far, may change in future!
  974. if (Temp && (Direction == tdToLocal) && (Type == ttMove) &&
  975. !WinConfiguration->DDAllowMove)
  976. {
  977. TMessageParams Params(mpNeverAskAgainCheck);
  978. unsigned int Answer = MessageDialog(LoadStr(DND_DOWNLOAD_MOVE_WARNING), qtWarning,
  979. qaOK | qaCancel, HELP_DND_DOWNLOAD_MOVE_WARNING, &Params);
  980. if (Answer == qaNeverAskAgain)
  981. {
  982. WinConfiguration->DDAllowMove = true;
  983. }
  984. else if (Answer == qaCancel)
  985. {
  986. Result = false;
  987. }
  988. }
  989. // these parameters are known in advance
  990. int Params =
  991. FLAGMASK(Type == ttMove, cpDelete);
  992. bool ToTemp = (Temp && (Direction == tdToLocal));
  993. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  994. {
  995. bool DisableNewerOnly =
  996. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  997. ToTemp;
  998. Options |=
  999. FLAGMASK(ToTemp, coTemp) |
  1000. FLAGMASK(!Terminal->IsCapable[fsBackgroundTransfers], coDisableQueue) |
  1001. coDoNotShowAgain;
  1002. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1003. int CopyParamAttrs = (Direction == tdToRemote ?
  1004. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1005. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1006. int OutputOptions =
  1007. FLAGMASK(DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto),
  1008. cooDoNotShowAgain);
  1009. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1010. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1011. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1012. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions);
  1013. if (Result)
  1014. {
  1015. if (FLAGSET(OutputOptions, cooDoNotShowAgain))
  1016. {
  1017. if (DragDrop)
  1018. {
  1019. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1020. {
  1021. PopupTrayBalloon(NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation,
  1022. NULL, 0, EnableDDTransferConfirmation, NULL);
  1023. }
  1024. WinConfiguration->DDTransferConfirmation = asOff;
  1025. }
  1026. else
  1027. {
  1028. WinConfiguration->ConfirmTransferring = false;
  1029. }
  1030. }
  1031. else
  1032. {
  1033. // User exclicitly unchecked "do not show again",
  1034. // so show him the dialog the next time
  1035. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1036. {
  1037. WinConfiguration->DDTransferConfirmation = asOn;
  1038. }
  1039. }
  1040. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1041. }
  1042. }
  1043. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fsBackgroundTransfers])
  1044. {
  1045. Configuration->Usage->Inc(L"TransfersOnBackground");
  1046. // these parameter are known only after transfer dialog
  1047. Params |=
  1048. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1049. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1050. Result = false;
  1051. ClearTransferSourceSelection(Direction);
  1052. }
  1053. return Result;
  1054. }
  1055. //---------------------------------------------------------------------------
  1056. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1057. {
  1058. if (FOnFileOperationFinished != NULL)
  1059. {
  1060. FOnFileOperationFinished(UnicodeString(), true);
  1061. }
  1062. else
  1063. {
  1064. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1065. if (HasDirView[Side])
  1066. {
  1067. DirView(Side)->SelectAll(smNone);
  1068. }
  1069. }
  1070. }
  1071. //---------------------------------------------------------------------------
  1072. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1073. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1074. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1075. int Params)
  1076. {
  1077. DebugAssert(Queue != NULL);
  1078. bool SingleFile = false;
  1079. if (FileList->Count == 1)
  1080. {
  1081. if (Direction == tdToRemote)
  1082. {
  1083. UnicodeString FileName = FileList->Strings[0];
  1084. SingleFile = FileExists(ApiPath(FileName));
  1085. }
  1086. else
  1087. {
  1088. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1089. SingleFile = !File->IsDirectory;
  1090. }
  1091. }
  1092. TQueueItem * QueueItem;
  1093. if (Direction == tdToRemote)
  1094. {
  1095. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1096. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1097. }
  1098. else
  1099. {
  1100. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1101. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1102. }
  1103. AddQueueItem(Queue, QueueItem, Terminal);
  1104. }
  1105. //---------------------------------------------------------------------------
  1106. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TTerminal * Terminal)
  1107. {
  1108. if (Queue->IsEmpty)
  1109. {
  1110. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  1111. ManagedTerminal->QueueOperationStart = Now();
  1112. }
  1113. Queue->AddItem(QueueItem);
  1114. }
  1115. //---------------------------------------------------------------------------
  1116. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1117. {
  1118. }
  1119. //---------------------------------------------------------------------------
  1120. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1121. {
  1122. DebugAssert(FDocks != NULL);
  1123. for (int Index = 0; Index < ComponentCount; Index++)
  1124. {
  1125. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1126. if ((Dock != NULL) && (Dock->Tag == 0))
  1127. {
  1128. FDocks->Add(Dock);
  1129. }
  1130. }
  1131. CollectItemsWithTextDisplayMode(this);
  1132. ConfigurationChanged();
  1133. QueuePanel->Height =
  1134. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1135. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1136. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1137. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1138. }
  1139. //---------------------------------------------------------------------------
  1140. void __fastcall TCustomScpExplorerForm::StoreParams()
  1141. {
  1142. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1143. WinConfiguration->QueueView.HeightPixelsPerInch = GetControlPixelsPerInch(this);
  1144. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1145. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1146. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1147. }
  1148. //---------------------------------------------------------------------------
  1149. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1150. {
  1151. if (!FFormRestored)
  1152. {
  1153. FFormRestored = true;
  1154. RestoreFormParams();
  1155. }
  1156. TForm::CreateParams(Params);
  1157. }
  1158. //---------------------------------------------------------------------------
  1159. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1160. {
  1161. DebugAssert(FDocks != NULL);
  1162. for (int Index = 0; Index < FDocks->Count; Index++)
  1163. {
  1164. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1165. }
  1166. }
  1167. //---------------------------------------------------------------------------
  1168. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1169. {
  1170. SetDockAllowDrag(true);
  1171. while (!ButtonsStr.IsEmpty())
  1172. {
  1173. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1174. UnicodeString Name = CutToChar(S, L'=', true);
  1175. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1176. {
  1177. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1178. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1179. {
  1180. while (!S.IsEmpty())
  1181. {
  1182. UnicodeString S2 = CutToChar(S, L',', true);
  1183. UnicodeString Name = CutToChar(S2, L':', true);
  1184. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1185. {
  1186. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1187. if (SameText(GetToolbarItemName(Item), Name))
  1188. {
  1189. // forward compatibility
  1190. UnicodeString S3 = CutToChar(S2, L':', true);
  1191. bool Visible = StrToIntDef(S3, 0);
  1192. Item->Visible = Visible;
  1193. }
  1194. }
  1195. }
  1196. }
  1197. }
  1198. }
  1199. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1200. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1201. }
  1202. //---------------------------------------------------------------------------
  1203. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1204. {
  1205. UnicodeString Result;
  1206. SetDockAllowDrag(true);
  1207. Result = ::GetToolbarsLayoutStr(this);
  1208. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1209. return Result;
  1210. }
  1211. //---------------------------------------------------------------------------
  1212. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1213. {
  1214. UnicodeString Result;
  1215. if (Item->Action != NULL)
  1216. {
  1217. Result = Item->Action->Name;
  1218. Result = RemoveSuffix(Result, L"Action");
  1219. }
  1220. else
  1221. {
  1222. Result = Item->Name;
  1223. Result = RemoveSuffix(Result, L"SubmenuItem");
  1224. Result = RemoveSuffix(Result, L"Item");
  1225. }
  1226. return Result;
  1227. }
  1228. //---------------------------------------------------------------------------
  1229. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1230. {
  1231. UnicodeString Result;
  1232. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1233. {
  1234. UnicodeString ToolbarStr;
  1235. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1236. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1237. {
  1238. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1239. {
  1240. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1241. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1242. // Once we introduce any buttons that aer hidden by default, we would have to remember their initial state
  1243. // and save the changes here only.
  1244. if (!Item->Visible)
  1245. {
  1246. UnicodeString Name = GetToolbarItemName(Item);
  1247. DebugAssert(Name.Pos(L"TBX") == 0);
  1248. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1249. }
  1250. }
  1251. }
  1252. if (!ToolbarStr.IsEmpty())
  1253. {
  1254. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1255. }
  1256. }
  1257. return Result;
  1258. }
  1259. //---------------------------------------------------------------------------
  1260. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1261. {
  1262. DebugAssert(FProgressForm == NULL);
  1263. FProgressForm =
  1264. new TProgressForm(Application, (FTransferResumeList != NULL), Terminal->IsCapable[fsSkipTransfer], SynchronizeProgress);
  1265. FProgressForm->DeleteLocalToRecycleBin =
  1266. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1267. FProgressForm->DeleteRemoteToRecycleBin =
  1268. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1269. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1270. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1271. LockWindow();
  1272. if (FTaskbarList != NULL)
  1273. {
  1274. // Actually, do not know what hides the progress once the operation finishes
  1275. SetTaskbarListProgressState(TBPF_NORMAL);
  1276. }
  1277. }
  1278. //---------------------------------------------------------------------------
  1279. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1280. {
  1281. UnlockWindow();
  1282. SAFE_DESTROY(FProgressForm);
  1283. SetQueueProgress();
  1284. }
  1285. //---------------------------------------------------------------------------
  1286. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1287. TFileOperationProgressType & ProgressData)
  1288. {
  1289. // Download to temporary local directory
  1290. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1291. {
  1292. bool Continue = true;
  1293. FPendingTempSpaceWarn = false;
  1294. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1295. if (!Continue)
  1296. {
  1297. Abort();
  1298. }
  1299. }
  1300. // operation is being executed and we still didn't show up progress form
  1301. if (ProgressData.InProgress && !FProgressForm)
  1302. {
  1303. CreateProgressForm(NULL);
  1304. // When main window is hidden, synchronisation form does not exist (we do not get here during synchronization - only comparing),
  1305. // we suppose "/upload" or URL download mode
  1306. if (!Visible && (ProgressData.Operation != foCalculateSize) &&
  1307. (ProgressData.Operation != foCalculateChecksum) &&
  1308. (FSynchronizeProgressForm == NULL) &&
  1309. !FStandaloneEditing)
  1310. {
  1311. FProgressForm->OnceDoneOperation = odoDisconnect;
  1312. }
  1313. }
  1314. // operation is finished (or terminated), so we hide progress form
  1315. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1316. {
  1317. DestroyProgressForm();
  1318. if (ProgressData.Operation == foCalculateSize)
  1319. {
  1320. // When calculating size before transfer, the abort caused by
  1321. // cancel flag set due to "MoveToQueue" below,
  1322. // is not propagated back to us in ExecuteFileOperation,
  1323. // as it is silently swallowed.
  1324. // So we explicitly (re)throw it here.
  1325. if (FMoveToQueue)
  1326. {
  1327. Abort();
  1328. }
  1329. }
  1330. else
  1331. {
  1332. if ((ProgressData.Cancel == csContinue) &&
  1333. !FAutoOperation)
  1334. {
  1335. OperationComplete(ProgressData.StartTime);
  1336. }
  1337. }
  1338. }
  1339. if (FProgressForm &&
  1340. // Can happen only during synchronization, when ending one of the operations
  1341. ProgressData.InProgress)
  1342. {
  1343. FProgressForm->SetProgressData(ProgressData);
  1344. if (FTaskbarList != NULL)
  1345. {
  1346. DebugAssert(ProgressData.InProgress);
  1347. SetTaskbarListProgressValue(&ProgressData);
  1348. }
  1349. if (FProgressForm->Cancel > csContinue)
  1350. {
  1351. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1352. if (FProgressForm->Cancel == csCancelFile)
  1353. {
  1354. FProgressForm->ClearCancel();
  1355. }
  1356. // cancel cancels even the move
  1357. FMoveToQueue = false;
  1358. }
  1359. else if (FProgressForm->MoveToQueue)
  1360. {
  1361. FMoveToQueue = true;
  1362. ProgressData.SetCancelAtLeast(csCancel);
  1363. }
  1364. if ((FTransferResumeList != NULL) &&
  1365. ProgressData.InProgress &&
  1366. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1367. !ProgressData.FullFileName.IsEmpty())
  1368. {
  1369. if ((FTransferResumeList->Count == 0) ||
  1370. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1371. {
  1372. // note that we do not recognize directories from files here
  1373. FTransferResumeList->Add(ProgressData.FullFileName);
  1374. }
  1375. }
  1376. }
  1377. }
  1378. //---------------------------------------------------------------------------
  1379. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1380. const TDateTime & StartTime)
  1381. {
  1382. if (GUIConfiguration->BeepOnFinish &&
  1383. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1384. {
  1385. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1386. DWORD Sound;
  1387. if (!ExtractFileExt(BeepSound).IsEmpty())
  1388. {
  1389. Sound = SND_FILENAME;
  1390. }
  1391. else
  1392. {
  1393. Sound = SND_ALIAS;
  1394. }
  1395. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1396. }
  1397. }
  1398. //---------------------------------------------------------------------------
  1399. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1400. TFileOperationProgressType & ProgressData)
  1401. {
  1402. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1403. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1404. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1405. FileOperationProgress(ProgressData);
  1406. }
  1407. //---------------------------------------------------------------------------
  1408. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1409. {
  1410. UnicodeString Result;
  1411. if (FProgressForm != NULL)
  1412. {
  1413. Result = FProgressForm->ProgressStr();
  1414. }
  1415. return Result;
  1416. }
  1417. //---------------------------------------------------------------------------
  1418. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1419. bool DragDrop)
  1420. {
  1421. return (!DragDrop && (DropSourceControl == NULL)) ||
  1422. (DropSourceControl == RemoteDirView);
  1423. }
  1424. //---------------------------------------------------------------------------
  1425. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1426. TFileOperation Operation, TOperationSide Side,
  1427. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1428. TOnceDoneOperation & OnceDoneOperation)
  1429. {
  1430. if (!FAutoOperation)
  1431. {
  1432. // no selection on "/upload", form servers only as event handler
  1433. // (it is not displayed)
  1434. if (PanelOperation(Side, FDragDropOperation) &&
  1435. Visible && (Operation != foCalculateSize) &&
  1436. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1437. {
  1438. if (FOnFileOperationFinished != NULL)
  1439. {
  1440. FOnFileOperationFinished(FileName, Success);
  1441. }
  1442. else
  1443. {
  1444. TCustomDirView * DView = DirView(Side);
  1445. UnicodeString FileNameOnly = ExtractFileName(FileName, (Side == osRemote));
  1446. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1447. // this can happen when local drive is unplugged in the middle of the operation
  1448. if (Item != NULL)
  1449. {
  1450. if (Success) Item->Selected = false;
  1451. DView->MakeProgressVisible(Item);
  1452. }
  1453. }
  1454. }
  1455. if ((Operation == foCopy) || (Operation == foMove))
  1456. {
  1457. if (Side == osLocal)
  1458. {
  1459. Configuration->Usage->Inc(L"UploadedFiles");
  1460. }
  1461. else
  1462. {
  1463. Configuration->Usage->Inc(L"DownloadedFiles");
  1464. }
  1465. }
  1466. }
  1467. if (Success && (FSynchronizeController != NULL))
  1468. {
  1469. if (Operation == foCopy)
  1470. {
  1471. DebugAssert(Side == osLocal);
  1472. FSynchronizeController->LogOperation(soUpload, FileName);
  1473. }
  1474. else if (Operation == foDelete)
  1475. {
  1476. DebugAssert(Side == osRemote);
  1477. FSynchronizeController->LogOperation(soDelete, FileName);
  1478. }
  1479. }
  1480. if (FProgressForm)
  1481. {
  1482. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1483. }
  1484. }
  1485. //---------------------------------------------------------------------------
  1486. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1487. TFileOperation Operation, TOperationSide Side,
  1488. bool Temp, const UnicodeString & FileName, Boolean Success,
  1489. TOnceDoneOperation & OnceDoneOperation)
  1490. {
  1491. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1492. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1493. OnceDoneOperation);
  1494. }
  1495. //---------------------------------------------------------------------------
  1496. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1497. {
  1498. DebugAssert(GetSide(Side) == osRemote);
  1499. DebugUsedParam(Side);
  1500. return RemoteDirView;
  1501. }
  1502. //---------------------------------------------------------------------------
  1503. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1504. {
  1505. DebugAssert(GetSide(Side) == osRemote);
  1506. DebugUsedParam(Side);
  1507. return (Terminal != NULL);
  1508. }
  1509. //---------------------------------------------------------------------------
  1510. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1511. TOperationSide Side, int FilesOnly)
  1512. {
  1513. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1514. }
  1515. //---------------------------------------------------------------------------
  1516. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1517. TOperationSide Side, int FilesOnly)
  1518. {
  1519. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1520. }
  1521. //---------------------------------------------------------------------------
  1522. struct THistoryItemData
  1523. {
  1524. short int Side;
  1525. short int Index;
  1526. };
  1527. //---------------------------------------------------------------------------
  1528. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1529. {
  1530. DirView(Side)->HistoryGo(Index);
  1531. }
  1532. //---------------------------------------------------------------------------
  1533. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1534. {
  1535. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1536. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1537. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1538. }
  1539. //---------------------------------------------------------------------------
  1540. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1541. bool Back)
  1542. {
  1543. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1544. {
  1545. TCustomDirView * DView = DirView(Side);
  1546. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1547. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1548. if (ICount > 10)
  1549. {
  1550. ICount = 10;
  1551. }
  1552. Menu->Items->Clear();
  1553. THistoryItemData Data;
  1554. Data.Side = Side;
  1555. for (short int i = 1; i <= ICount; i++)
  1556. {
  1557. TTBCustomItem * Item = new TTBXItem(Menu);
  1558. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1559. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, (Side == osRemote));
  1560. Item->Hint = DView->HistoryPath[Data.Index];
  1561. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1562. Item->Tag = *reinterpret_cast<int*>(&Data);
  1563. Item->OnClick = HistoryItemClick;
  1564. Menu->Items->Add(Item);
  1565. }
  1566. }
  1567. }
  1568. //---------------------------------------------------------------------------
  1569. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1570. TOperationSide Side, bool Back)
  1571. {
  1572. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1573. {
  1574. // workaround
  1575. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1576. // operator new allocates memory in Pascal code, but calls inline
  1577. // contructor in C++, leading in problems, the function does
  1578. // both in Pascal code
  1579. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1580. UpdateHistoryMenu(Side, Back);
  1581. }
  1582. return FHistoryMenu[Side == osLocal][Back];
  1583. }
  1584. //---------------------------------------------------------------------------
  1585. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1586. TCustomDirView *Sender)
  1587. {
  1588. TOperationSide Side = (Sender == DirView(osRemote) ? osRemote : osLocal);
  1589. UpdateHistoryMenu(Side, true);
  1590. UpdateHistoryMenu(Side, false);
  1591. }
  1592. //---------------------------------------------------------------------------
  1593. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1594. {
  1595. // remote custom commands can be executed only if the server supports shell commands
  1596. // or have secondary shell
  1597. return (FTerminal != NULL) && (FTerminal->IsCapable[fcSecondaryShell] || FTerminal->IsCapable[fcShellAnyCommand]);
  1598. }
  1599. //---------------------------------------------------------------------------
  1600. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1601. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1602. {
  1603. int Result;
  1604. TFileCustomCommand RemoteCustomCommand;
  1605. TLocalCustomCommand LocalCustomCommand;
  1606. TFileCustomCommand * NonInteractiveCustomCommand =
  1607. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1608. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1609. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1610. if (FLAGCLEAR(Command.Params, ccLocal))
  1611. {
  1612. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1613. // custom command that does not operate with files can be executed anytime ...
  1614. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1615. {
  1616. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1617. {
  1618. // ... but do not show such command in remote file menu (TODO)
  1619. Result = AllowedState;
  1620. }
  1621. else
  1622. {
  1623. Result = -1;
  1624. }
  1625. }
  1626. else
  1627. {
  1628. if ((ListType == ccltAll) || (ListType == ccltFile))
  1629. {
  1630. Result = ((FCurrentSide == osRemote) && DirView(osRemote)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1631. }
  1632. else
  1633. {
  1634. Result = -1;
  1635. }
  1636. }
  1637. }
  1638. else
  1639. {
  1640. // custom command that does not operate with files can be executed anytime
  1641. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1642. {
  1643. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1644. }
  1645. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1646. {
  1647. if ((ListType == ccltAll) || (ListType == ccltFile))
  1648. {
  1649. // special case is "diff"-style command that can be executed over any side,
  1650. // if we have both sides
  1651. Result =
  1652. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1653. // directly (instead of EnableSelectedOperation) to pass
  1654. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1655. ((HasDirView[osLocal] && DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1656. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1657. }
  1658. else if (ListType == ccltBoth)
  1659. {
  1660. Result = 1;
  1661. }
  1662. else
  1663. {
  1664. Result = -1;
  1665. }
  1666. }
  1667. else
  1668. {
  1669. if ((ListType == ccltAll) || (ListType == ccltFile))
  1670. {
  1671. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1672. }
  1673. else
  1674. {
  1675. Result = -1;
  1676. }
  1677. }
  1678. }
  1679. return Result;
  1680. }
  1681. //---------------------------------------------------------------------------
  1682. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1683. TStrings * FileList, const TCustomCommandType & ACommand,
  1684. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1685. {
  1686. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1687. {
  1688. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1689. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1690. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1691. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1692. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1693. bool Capture =
  1694. FLAGSET(ACommand.Params, ccShowResults) ||
  1695. FLAGSET(ACommand.Params, ccCopyResults) ||
  1696. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1697. TCaptureOutputEvent OutputEvent = NULL;
  1698. DebugAssert(FCapturedLog == NULL);
  1699. if (Capture)
  1700. {
  1701. FCapturedLog = new TStringList();
  1702. OutputEvent = TerminalCaptureLog;
  1703. }
  1704. try
  1705. {
  1706. if (!RemoteCustomCommand.IsFileCommand(Command))
  1707. {
  1708. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1709. OutputEvent);
  1710. }
  1711. else
  1712. {
  1713. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1714. }
  1715. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1716. {
  1717. if (FLAGSET(ACommand.Params, ccCopyResults))
  1718. {
  1719. CopyToClipboard(FCapturedLog);
  1720. }
  1721. if (FLAGSET(ACommand.Params, ccShowResults))
  1722. {
  1723. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1724. }
  1725. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1726. {
  1727. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1728. }
  1729. }
  1730. }
  1731. __finally
  1732. {
  1733. SAFE_DESTROY(FCapturedLog);
  1734. }
  1735. }
  1736. }
  1737. //---------------------------------------------------------------------------
  1738. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1739. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1740. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1741. {
  1742. TStrings * LocalFileList = NULL;
  1743. TStrings * RemoteFileList = NULL;
  1744. TStrings * RemoteFileListFull = NULL;
  1745. try
  1746. {
  1747. if (LocalFileCommand)
  1748. {
  1749. if (ALocalFileList == NULL)
  1750. {
  1751. DebugAssert(HasDirView[osLocal]);
  1752. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1753. // directly (instead of EnableSelectedOperation) to pass
  1754. // false to FocusedFileOnlyWhenFocused
  1755. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1756. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1757. }
  1758. else
  1759. {
  1760. LocalFileList = ALocalFileList;
  1761. }
  1762. if (FileListCommand)
  1763. {
  1764. if (LocalFileList->Count != 1)
  1765. {
  1766. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1767. }
  1768. }
  1769. else
  1770. {
  1771. if ((LocalFileList->Count != 1) &&
  1772. (FileList->Count != 1) &&
  1773. (LocalFileList->Count != FileList->Count))
  1774. {
  1775. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1776. }
  1777. }
  1778. }
  1779. UnicodeString RootTempDir;
  1780. UnicodeString TempDir;
  1781. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1782. if (!RemoteFiles)
  1783. {
  1784. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1785. }
  1786. try
  1787. {
  1788. TDateTimes RemoteFileTimes;
  1789. if (RemoteFiles)
  1790. {
  1791. RemoteFileList = FileList;
  1792. }
  1793. else
  1794. {
  1795. RemoteFileList = new TStringList();
  1796. TMakeLocalFileListParams MakeFileListParam;
  1797. MakeFileListParam.FileList = RemoteFileList;
  1798. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1799. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1800. MakeFileListParam.Recursive =
  1801. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1802. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1803. if (!MakeFileListParam.Recursive)
  1804. {
  1805. RemoteFileTimes.clear();
  1806. RemoteFileListFull = new TStringList();
  1807. MakeFileListParam.FileList = RemoteFileListFull;
  1808. MakeFileListParam.Recursive = true;
  1809. MakeFileListParam.IncludeDirs = false;
  1810. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1811. }
  1812. else
  1813. {
  1814. RemoteFileListFull = RemoteFileList;
  1815. }
  1816. }
  1817. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1818. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1819. if (!NonBlocking)
  1820. {
  1821. Progress.Start(foCustomCommand, osRemote, FileListCommand ? 1 : FileList->Count);
  1822. DebugAssert(FProgressForm != NULL);
  1823. FProgressForm->ReadOnly = true;
  1824. }
  1825. try
  1826. {
  1827. if (FileListCommand)
  1828. {
  1829. UnicodeString LocalFile;
  1830. // MakeFileList does not delimit filenames
  1831. UnicodeString FileList = MakeFileList(RemoteFileList);
  1832. if (LocalFileCommand)
  1833. {
  1834. DebugAssert(LocalFileList->Count == 1);
  1835. LocalFile = LocalFileList->Strings[0];
  1836. }
  1837. TLocalCustomCommand CustomCommand(Data,
  1838. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1839. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1840. if (NonBlocking)
  1841. {
  1842. DebugAssert(!POutput);
  1843. ExecuteShellChecked(ShellCommand);
  1844. }
  1845. else
  1846. {
  1847. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1848. }
  1849. }
  1850. else if (LocalFileCommand)
  1851. {
  1852. if (LocalFileList->Count == 1)
  1853. {
  1854. UnicodeString LocalFile = LocalFileList->Strings[0];
  1855. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1856. {
  1857. UnicodeString FileName = RemoteFileList->Strings[Index];
  1858. TLocalCustomCommand CustomCommand(Data,
  1859. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  1860. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1861. }
  1862. }
  1863. else if (RemoteFileList->Count == 1)
  1864. {
  1865. UnicodeString FileName = RemoteFileList->Strings[0];
  1866. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1867. {
  1868. TLocalCustomCommand CustomCommand(
  1869. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1870. FileName, LocalFileList->Strings[Index], L"");
  1871. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1872. }
  1873. }
  1874. else
  1875. {
  1876. if (LocalFileList->Count != RemoteFileList->Count)
  1877. {
  1878. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  1879. }
  1880. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1881. {
  1882. UnicodeString FileName = RemoteFileList->Strings[Index];
  1883. TLocalCustomCommand CustomCommand(
  1884. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1885. FileName, LocalFileList->Strings[Index], L"");
  1886. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1887. }
  1888. }
  1889. }
  1890. else
  1891. {
  1892. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1893. {
  1894. TLocalCustomCommand CustomCommand(Data,
  1895. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1896. RemoteFileList->Strings[Index], L"", L"");
  1897. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1898. }
  1899. }
  1900. }
  1901. __finally
  1902. {
  1903. if (!NonBlocking)
  1904. {
  1905. Progress.Stop();
  1906. }
  1907. }
  1908. DebugAssert(!FAutoOperation);
  1909. if (!RemoteFiles)
  1910. {
  1911. TempDir = IncludeTrailingBackslash(TempDir);
  1912. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  1913. {
  1914. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  1915. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  1916. // Skip directories, process only nested files.
  1917. // The check is redundant as FileAge fails for directories anyway.
  1918. !DirectoryExists(FileName))
  1919. {
  1920. UnicodeString RemoteDir =
  1921. UnixExtractFileDir(
  1922. UnixIncludeTrailingBackslash(FTerminal->CurrentDirectory) +
  1923. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  1924. TDateTime NewTime;
  1925. if (FileAge(FileName, NewTime) &&
  1926. (NewTime != RemoteFileTimes[Index]))
  1927. {
  1928. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  1929. TemporaryFileCopyParam(CopyParam);
  1930. CopyParam.FileMask = L"";
  1931. FAutoOperation = true;
  1932. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  1933. TemporaryFilesList->Add(FileName);
  1934. FTerminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  1935. }
  1936. }
  1937. }
  1938. }
  1939. }
  1940. __finally
  1941. {
  1942. FAutoOperation = false;
  1943. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  1944. {
  1945. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  1946. }
  1947. }
  1948. }
  1949. __finally
  1950. {
  1951. if (RemoteFileList != FileList)
  1952. {
  1953. delete RemoteFileList;
  1954. }
  1955. if (LocalFileList != ALocalFileList)
  1956. {
  1957. delete LocalFileList;
  1958. }
  1959. if (RemoteFileListFull != RemoteFileList)
  1960. {
  1961. delete RemoteFileListFull;
  1962. }
  1963. }
  1964. }
  1965. //---------------------------------------------------------------------------
  1966. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithRemoteFiles(
  1967. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  1968. bool FileListCommand, UnicodeString * POutput)
  1969. {
  1970. std::unique_ptr<TStrings> SelectedFileList(DirView(osLocal)->CreateFileList(false, true, NULL));
  1971. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  1972. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  1973. {
  1974. UnicodeString FileName = SelectedFileList->Strings[Index];
  1975. if (DirectoryExists(FileName))
  1976. {
  1977. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  1978. {
  1979. LocalFileList->Add(FileName);
  1980. }
  1981. if (FLAGSET(ACommand.Params, ccRecursive))
  1982. {
  1983. TMakeLocalFileListParams MakeFileListParam;
  1984. MakeFileListParam.FileList = LocalFileList.get();
  1985. MakeFileListParam.FileTimes = NULL;
  1986. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1987. MakeFileListParam.Recursive = true;
  1988. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  1989. }
  1990. }
  1991. else
  1992. {
  1993. LocalFileList->Add(FileName);
  1994. }
  1995. }
  1996. if (FileListCommand)
  1997. {
  1998. UnicodeString FileList = MakeFileList(LocalFileList.get());
  1999. TLocalCustomCommand CustomCommand(
  2000. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2001. L"", L"", FileList);
  2002. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2003. }
  2004. else
  2005. {
  2006. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2007. Progress.Start(foCustomCommand, osRemote, FileListCommand ? 1 : LocalFileList->Count);
  2008. DebugAssert(FProgressForm != NULL);
  2009. FProgressForm->ReadOnly = true;
  2010. try
  2011. {
  2012. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2013. {
  2014. UnicodeString FileName = LocalFileList->Strings[Index];
  2015. TLocalCustomCommand CustomCommand(
  2016. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2017. FileName, L"", L"");
  2018. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2019. }
  2020. }
  2021. __finally
  2022. {
  2023. Progress.Stop();
  2024. }
  2025. }
  2026. }
  2027. //---------------------------------------------------------------------------
  2028. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2029. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2030. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2031. {
  2032. TLocalCustomCommand LocalCustomCommand(Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory());
  2033. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2034. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2035. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2036. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2037. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2038. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2039. std::unique_ptr<UnicodeString> POutput;
  2040. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2041. FLAGSET(ACommand.Params, ccCopyResults))
  2042. {
  2043. POutput.reset(new UnicodeString());
  2044. }
  2045. if (!LocalCustomCommand.IsFileCommand(Command))
  2046. {
  2047. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2048. }
  2049. // remote files?
  2050. else if ((FCurrentSide == osRemote) || LocalFileCommand)
  2051. {
  2052. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2053. }
  2054. // local files
  2055. else
  2056. {
  2057. LocalCustomCommandWithRemoteFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2058. }
  2059. if (POutput.get() != NULL)
  2060. {
  2061. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2062. int P = POutput->Pos(sLineBreak);
  2063. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2064. {
  2065. POutput->SetLength(P - 1);
  2066. }
  2067. // Copy even empty output to clipboard
  2068. if (FLAGSET(ACommand.Params, ccCopyResults))
  2069. {
  2070. CopyToClipboard(*POutput);
  2071. }
  2072. // But do not show an empty message box.
  2073. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2074. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2075. !POutput->IsEmpty())
  2076. {
  2077. TClipboardHandler ClipboardHandler;
  2078. ClipboardHandler.Text = *POutput;
  2079. TMessageParams Params;
  2080. TQueryButtonAlias Aliases[1];
  2081. Aliases[0].Button = qaRetry;
  2082. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2083. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2084. Params.Aliases = Aliases;
  2085. Params.AliasesCount = LENOF(Aliases);
  2086. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2087. }
  2088. }
  2089. }
  2090. //---------------------------------------------------------------------------
  2091. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2092. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2093. {
  2094. TCustomCommandData Data(Terminal);
  2095. UnicodeString Site = Terminal->SessionData->SessionKey;
  2096. UnicodeString HelpKeyword = ACommand.HomePage;
  2097. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2098. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2099. {
  2100. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2101. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2102. {
  2103. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, Terminal->CurrentDirectory));
  2104. }
  2105. else
  2106. {
  2107. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory()));
  2108. }
  2109. if (!DoCustomCommandOptionsDialog(
  2110. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2111. {
  2112. Abort();
  2113. }
  2114. }
  2115. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2116. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2117. {
  2118. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2119. }
  2120. else
  2121. {
  2122. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2123. }
  2124. }
  2125. //---------------------------------------------------------------------------
  2126. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2127. const TCustomCommandType & Command)
  2128. {
  2129. DebugAssert(FCustomCommandLocalFileList != NULL);
  2130. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2131. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2132. TStrings * LocalFileList = new TStringList();
  2133. TStrings * RemoteFileList = new TStringList();
  2134. try
  2135. {
  2136. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2137. {
  2138. LocalFileList->Clear();
  2139. LocalFileList->AddObject(
  2140. FCustomCommandLocalFileList->Strings[Index],
  2141. FCustomCommandLocalFileList->Objects[Index]);
  2142. RemoteFileList->Clear();
  2143. RemoteFileList->AddObject(
  2144. FCustomCommandRemoteFileList->Strings[Index],
  2145. FCustomCommandRemoteFileList->Objects[Index]);
  2146. CustomCommand(RemoteFileList, Command, LocalFileList);
  2147. }
  2148. }
  2149. __finally
  2150. {
  2151. delete LocalFileList;
  2152. delete RemoteFileList;
  2153. }
  2154. }
  2155. //---------------------------------------------------------------------------
  2156. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2157. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2158. {
  2159. delete FCustomCommandLocalFileList;
  2160. delete FCustomCommandRemoteFileList;
  2161. // takeover ownership,
  2162. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2163. FCustomCommandLocalFileList = LocalFileList;
  2164. FCustomCommandRemoteFileList = RemoteFileList;
  2165. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2166. if (Button != NULL)
  2167. {
  2168. FCustomCommandMenu->Items->Clear();
  2169. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth);
  2170. MenuPopup(FCustomCommandMenu, Button);
  2171. }
  2172. else
  2173. {
  2174. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2175. }
  2176. }
  2177. //---------------------------------------------------------------------------
  2178. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2179. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2180. {
  2181. DebugAssert(FCapturedLog != NULL);
  2182. if ((OutputType == cotOutput) || (OutputType == cotError))
  2183. {
  2184. FCapturedLog->Add(AddedLine);
  2185. }
  2186. }
  2187. //---------------------------------------------------------------------------
  2188. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2189. TOperationSide Side)
  2190. {
  2191. return (Side == osRemote) &&
  2192. ((Control == RemoteDirView) || (Control == RemoteDriveView));
  2193. }
  2194. //---------------------------------------------------------------------------
  2195. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2196. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2197. {
  2198. TTBItemOptions O;
  2199. O = Item->Options;
  2200. TCustomDirView * DView = DirView(Side);
  2201. if ((DView->ItemFocused != NULL) &&
  2202. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2203. // when resolving links is disabled, default action is to enter the directory,
  2204. // no matter what DoubleClickAction is configured to
  2205. ((Side != osRemote) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2206. // Can only Edit files, but can Open/Copy even directories
  2207. ((DoubleClickAction != dcaEdit) ||
  2208. !DView->ItemIsDirectory(DView->ItemFocused)))
  2209. {
  2210. Item->Options = O << tboDefault;
  2211. }
  2212. else
  2213. {
  2214. Item->Options = O >> tboDefault;
  2215. }
  2216. }
  2217. //---------------------------------------------------------------------------
  2218. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2219. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2220. {
  2221. TCustomDirView * DView = DirView(Side);
  2222. TListItem * Item = DView->ItemFocused;
  2223. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2224. EnableFocusedOperation[Side])
  2225. {
  2226. TPoint ClientPoint;
  2227. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2228. TPoint(0, 0) : MousePos;
  2229. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2230. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2231. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2232. }
  2233. }
  2234. //---------------------------------------------------------------------------
  2235. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2236. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2237. {
  2238. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2239. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2240. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2241. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2242. Handled = true;
  2243. }
  2244. //---------------------------------------------------------------------------
  2245. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2246. {
  2247. }
  2248. //---------------------------------------------------------------------------
  2249. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2250. {
  2251. DebugAssert(FErrorList == NULL);
  2252. if (WinConfiguration->ContinueOnError)
  2253. {
  2254. FErrorList = new TStringList();
  2255. Configuration->Usage->Inc(L"ContinuationsOnError");
  2256. }
  2257. NonVisualDataModule->StartBusy();
  2258. }
  2259. //---------------------------------------------------------------------------
  2260. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2261. {
  2262. NonVisualDataModule->EndBusy();
  2263. if (FErrorList)
  2264. {
  2265. HandleErrorList(FErrorList);
  2266. }
  2267. }
  2268. //---------------------------------------------------------------------------
  2269. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2270. const TCopyParamType & CopyParam)
  2271. {
  2272. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2273. {
  2274. Configuration->Usage->Inc(L"FileMaskUses");
  2275. }
  2276. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2277. {
  2278. Configuration->Usage->Inc(L"OperationMaskUses");
  2279. }
  2280. }
  2281. //---------------------------------------------------------------------------
  2282. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2283. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2284. {
  2285. void * BatchStorage;
  2286. BatchStart(BatchStorage);
  2287. bool Result;
  2288. try
  2289. {
  2290. if ((Operation == foCopy) || (Operation == foMove))
  2291. {
  2292. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2293. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2294. UnicodeString TargetDirectory;
  2295. bool Temp = false;
  2296. bool DragDrop = false;
  2297. int Options = 0;
  2298. TAutoSwitch UseQueue = asAuto;
  2299. if (Param != NULL)
  2300. {
  2301. TTransferOperationParam& TParam =
  2302. *static_cast<TTransferOperationParam*>(Param);
  2303. TargetDirectory = TParam.TargetDirectory;
  2304. Temp = TParam.Temp;
  2305. DragDrop = TParam.DragDrop;
  2306. Options = TParam.Options;
  2307. UseQueue = TParam.Queue;
  2308. }
  2309. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2310. switch (UseQueue)
  2311. {
  2312. case asOn:
  2313. CopyParam.Queue = true;
  2314. break;
  2315. case asOff:
  2316. CopyParam.Queue = false;
  2317. break;
  2318. case asAuto:
  2319. default:
  2320. // keep default
  2321. break;
  2322. }
  2323. Result =
  2324. CopyParamDialog(Direction, Type, Temp, FileList, TargetDirectory,
  2325. CopyParam, !NoConfirmation, DragDrop, Options);
  2326. if (Result)
  2327. {
  2328. DebugAssert(Terminal);
  2329. bool SelectionRestored = false;
  2330. TCustomDirView * DView = NULL;
  2331. if (HasDirView[Side])
  2332. {
  2333. DView = DirView(Side);
  2334. DView->SaveSelection();
  2335. DView->SaveSelectedNames();
  2336. }
  2337. UpdateCopyParamCounters(CopyParam);
  2338. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2339. DebugAssert(FTransferResumeList == NULL);
  2340. FTransferResumeList =
  2341. Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2342. FMoveToQueue = false;
  2343. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2344. try
  2345. {
  2346. TStrings * PermanentFileList;
  2347. std::unique_ptr<TStrings> PermanentFileListOwner;
  2348. try
  2349. {
  2350. if (Side == osLocal)
  2351. {
  2352. PermanentFileList = FileList;
  2353. Params |= FLAGMASK(Temp, cpTemporary);
  2354. Terminal->CopyToRemote(FileList, TargetDirectory, &CopyParam, Params, NULL);
  2355. if (Operation == foMove)
  2356. {
  2357. ReloadLocalDirectory();
  2358. if (DView != NULL)
  2359. {
  2360. DView->RestoreSelection();
  2361. }
  2362. SelectionRestored = true;
  2363. }
  2364. }
  2365. else
  2366. {
  2367. // Clone the file list as it may refer to current directory files,
  2368. // which get destroyed, when the source directory is reloaded after foMove operation.
  2369. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2370. // But for a hotfix, we are not going to do such a big change.
  2371. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2372. PermanentFileList = PermanentFileListOwner.get();
  2373. try
  2374. {
  2375. Terminal->CopyToLocal(
  2376. FileList, TargetDirectory, &CopyParam, Params, NULL);
  2377. }
  2378. __finally
  2379. {
  2380. if (Operation == foMove)
  2381. {
  2382. if (DView != NULL)
  2383. {
  2384. DView->RestoreSelection();
  2385. }
  2386. SelectionRestored = true;
  2387. }
  2388. ReloadLocalDirectory(TargetDirectory);
  2389. }
  2390. }
  2391. }
  2392. catch (EAbort &)
  2393. {
  2394. if (FMoveToQueue)
  2395. {
  2396. Params |=
  2397. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2398. DebugAssert(CopyParam.TransferSkipList == NULL);
  2399. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2400. if (TransferResumeList->Count > 0)
  2401. {
  2402. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2403. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2404. }
  2405. CopyParam.TransferSkipList = TransferResumeList.release();
  2406. // not really needed, just to keep it consistent with TransferResumeList
  2407. FTransferResumeList = NULL;
  2408. FMoveToQueue = false;
  2409. Configuration->Usage->Inc("MovesToBackground");
  2410. AddQueueItem(Queue, Direction, PermanentFileList, TargetDirectory, CopyParam, Params);
  2411. ClearTransferSourceSelection(Direction);
  2412. }
  2413. throw;
  2414. }
  2415. }
  2416. __finally
  2417. {
  2418. if (!SelectionRestored && (DView != NULL))
  2419. {
  2420. DView->DiscardSavedSelection();
  2421. }
  2422. FTransferResumeList = NULL;
  2423. }
  2424. }
  2425. }
  2426. else if (Operation == foRename)
  2427. {
  2428. DebugAssert(DirView(Side)->ItemFocused);
  2429. DirView(Side)->ItemFocused->EditCaption();
  2430. Result = true;
  2431. }
  2432. else if (Operation == foDelete)
  2433. {
  2434. DebugAssert(FileList->Count);
  2435. // We deliberately do not toggle alternative flag (Param), but use OR,
  2436. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2437. // shortcut of CurrentDeleteAlternativeAction
  2438. bool Alternative =
  2439. bool(Param) || UseAlternativeFunction();
  2440. bool Recycle;
  2441. if (Side == osLocal)
  2442. {
  2443. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2444. }
  2445. else
  2446. {
  2447. Recycle = (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2448. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2449. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2450. }
  2451. Result =
  2452. !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2453. if (!Result)
  2454. {
  2455. UnicodeString Query;
  2456. if (FileList->Count == 1)
  2457. {
  2458. if (Side == osLocal)
  2459. {
  2460. Query = ExtractFileName(FileList->Strings[0]);
  2461. }
  2462. else
  2463. {
  2464. Query = UnixExtractFileName(FileList->Strings[0]);
  2465. }
  2466. Query = FMTLOAD(
  2467. (Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2468. }
  2469. else
  2470. {
  2471. Query = FMTLOAD(
  2472. (Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2473. }
  2474. TMessageParams Params(mpNeverAskAgainCheck);
  2475. Params.ImageName = L"Delete file";
  2476. unsigned int Answer = MessageDialog(MainInstructions(Query), qtConfirmation,
  2477. qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2478. if (Answer == qaNeverAskAgain)
  2479. {
  2480. Result = true;
  2481. if (Recycle)
  2482. {
  2483. WinConfiguration->ConfirmRecycling = false;
  2484. }
  2485. else
  2486. {
  2487. WinConfiguration->ConfirmDeleting = false;
  2488. }
  2489. }
  2490. else
  2491. {
  2492. Result = (Answer == qaOK);
  2493. }
  2494. }
  2495. if (Result)
  2496. {
  2497. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2498. }
  2499. }
  2500. else if (Operation == foSetProperties)
  2501. {
  2502. RemoteDirView->SaveSelectedNames();
  2503. Result = SetProperties(Side, FileList);
  2504. }
  2505. else if (Operation == foCustomCommand)
  2506. {
  2507. DebugAssert(Param);
  2508. DebugAssert(Side == osRemote);
  2509. RemoteDirView->SaveSelectedNames();
  2510. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2511. CustomCommand(FileList, *Command, NULL);
  2512. Result = true;
  2513. }
  2514. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2515. {
  2516. DebugAssert(Side == osRemote);
  2517. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2518. (Operation == foRemoteMove), reinterpret_cast<TTerminal *>(Param));
  2519. }
  2520. else if (Operation == foLock)
  2521. {
  2522. DebugAssert(Side == osRemote);
  2523. LockFiles(FileList, true);
  2524. Result = true;
  2525. }
  2526. else if (Operation == foUnlock)
  2527. {
  2528. DebugAssert(Side == osRemote);
  2529. LockFiles(FileList, false);
  2530. Result = true;
  2531. }
  2532. else
  2533. {
  2534. DebugFail();
  2535. }
  2536. }
  2537. __finally
  2538. {
  2539. BatchEnd(BatchStorage);
  2540. }
  2541. return Result;
  2542. }
  2543. //---------------------------------------------------------------------------
  2544. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2545. {
  2546. if (Side == osCurrent)
  2547. {
  2548. Side = FCurrentSide;
  2549. }
  2550. return Side;
  2551. }
  2552. //---------------------------------------------------------------------------
  2553. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2554. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2555. {
  2556. Side = GetSide(Side);
  2557. bool Result;
  2558. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, (Side == osLocal), NULL);
  2559. try
  2560. {
  2561. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2562. }
  2563. __finally
  2564. {
  2565. delete FileList;
  2566. }
  2567. return Result;
  2568. }
  2569. //---------------------------------------------------------------------------
  2570. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2571. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2572. bool NoConfirmation, void * Param)
  2573. {
  2574. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2575. {
  2576. if ((Operation == foCopy) || (Operation == foMove))
  2577. {
  2578. if (GetSide(Side) == osLocal)
  2579. {
  2580. Configuration->Usage->Inc(L"UploadsCommand");
  2581. }
  2582. else
  2583. {
  2584. Configuration->Usage->Inc(L"DownloadsCommand");
  2585. }
  2586. }
  2587. }
  2588. }
  2589. //---------------------------------------------------------------------------
  2590. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2591. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2592. {
  2593. TTransferOperationParam Param;
  2594. if ((WinConfiguration->Interface != ifCommander) ||
  2595. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2596. {
  2597. Flags &= ~cocShortCutHint;
  2598. }
  2599. TCustomDirView * DView = DirView(Side);
  2600. Param.Options =
  2601. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2602. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2603. if (FLAGSET(Flags, cocQueue))
  2604. {
  2605. Param.Queue = asOn;
  2606. }
  2607. else if (FLAGSET(Flags, cocNonQueue))
  2608. {
  2609. Param.Queue = asOff;
  2610. }
  2611. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2612. }
  2613. //---------------------------------------------------------------------------
  2614. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2615. {
  2616. try
  2617. {
  2618. if (ErrorList->Count)
  2619. {
  2620. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2621. if (MessageDialog(Message, qtError,
  2622. qaOK | qaCancel, HELP_NONE) == qaOK)
  2623. {
  2624. unsigned int Answer;
  2625. int Index = 0;
  2626. do
  2627. {
  2628. DebugAssert(Index >= 0 && Index < ErrorList->Count);
  2629. TQueryButtonAlias Aliases[2];
  2630. Aliases[0].Button = qaYes;
  2631. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2632. Aliases[1].Button = qaNo;
  2633. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2634. TMessageParams Params;
  2635. Params.Aliases = Aliases;
  2636. Params.AliasesCount = LENOF(Aliases);
  2637. Answer = MoreMessageDialog(
  2638. FMTLOAD(ERROR_LIST_NUMBER, (Index+1, ErrorList->Count, ErrorList->Strings[Index])),
  2639. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2640. (Index ? qaYes : 0) | (Index < ErrorList->Count - 1 ? qaNo : 0) |
  2641. qaOK, HELP_NONE, &Params);
  2642. if (Answer == qaNo)
  2643. {
  2644. Index++;
  2645. }
  2646. if (Answer == qaYes)
  2647. {
  2648. Index--;
  2649. }
  2650. }
  2651. while (Answer != qaOK);
  2652. }
  2653. }
  2654. }
  2655. __finally
  2656. {
  2657. TStrings * List = ErrorList;
  2658. ErrorList = NULL;
  2659. for (int i = 0; i < List->Count; i++)
  2660. {
  2661. delete List->Objects[i];
  2662. }
  2663. delete List;
  2664. }
  2665. }
  2666. //---------------------------------------------------------------------------
  2667. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2668. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2669. {
  2670. // needed for checking filemasks, as there's no directory object
  2671. // associated with the file object
  2672. File->FullFileName = FullFileName;
  2673. TFileMasks::TParams MaskParams;
  2674. MaskParams.Size = File->Size;
  2675. MaskParams.Modification = File->Modification;
  2676. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2677. }
  2678. //---------------------------------------------------------------------------
  2679. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2680. {
  2681. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2682. Side = GetSide(Side);
  2683. TCustomDirView * DView = DirView(Side);
  2684. TListItem * FocusedItem = DView->ItemFocused;
  2685. UnicodeString Name;
  2686. if ((FocusedItem != NULL) && !DView->ItemIsParentDirectory(FocusedItem))
  2687. {
  2688. Name = DView->ItemFileName(FocusedItem);
  2689. }
  2690. else
  2691. {
  2692. Name = LoadStr(NEW_FILE);
  2693. }
  2694. UnicodeString Names = Name;
  2695. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2696. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2697. HELP_EDIT_NEW, History.get(), true))
  2698. {
  2699. while (!Names.IsEmpty())
  2700. {
  2701. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2702. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2703. UnicodeString TargetFileName;
  2704. UnicodeString LocalFileName;
  2705. UnicodeString RootTempDir;
  2706. UnicodeString TempDir;
  2707. UnicodeString RemoteDirectory;
  2708. bool ExistingFile = false;
  2709. if (Side == osRemote)
  2710. {
  2711. Name = AbsolutePath(FTerminal->CurrentDirectory, Name);
  2712. TRemoteFile * File = NULL;
  2713. if (FTerminal->FileExists(Name, &File))
  2714. {
  2715. try
  2716. {
  2717. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2718. ExistingFile = true;
  2719. }
  2720. __finally
  2721. {
  2722. delete File;
  2723. }
  2724. }
  2725. if (!ExistingFile)
  2726. {
  2727. RemoteDirectory = UnixExtractFilePath(Name);
  2728. TemporaryDirectoryForRemoteFiles(
  2729. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2730. TargetFileName = UnixExtractFileName(Name);
  2731. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2732. LocalFileName = TempDir +
  2733. // We probably do not want to trim the VMS version here
  2734. FTerminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2735. }
  2736. }
  2737. else
  2738. {
  2739. if (ExtractFilePath(Name).IsEmpty())
  2740. {
  2741. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2742. }
  2743. else
  2744. {
  2745. LocalFileName = ExpandFileName(Name);
  2746. }
  2747. TargetFileName = ExtractFileName(Name);
  2748. }
  2749. if (!ExistingFile)
  2750. {
  2751. if (!FileExists(ApiPath(LocalFileName)))
  2752. {
  2753. int File = FileCreate(ApiPath(LocalFileName));
  2754. if (File < 0)
  2755. {
  2756. if (!RootTempDir.IsEmpty())
  2757. {
  2758. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2759. }
  2760. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2761. }
  2762. else
  2763. {
  2764. FileClose(File);
  2765. }
  2766. }
  2767. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2768. const TEditorData * ExternalEditor = NULL;
  2769. TFileMasks::TParams MaskParams; // size not known
  2770. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2771. false, MaskParams);
  2772. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2773. ExternalEditor, RootTempDir, RemoteDirectory);
  2774. }
  2775. }
  2776. }
  2777. }
  2778. //---------------------------------------------------------------------------
  2779. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2780. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2781. {
  2782. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2783. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2784. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2785. return
  2786. ((ExecuteFileBy == efInternalEditor)) ||
  2787. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2788. }
  2789. //---------------------------------------------------------------------------
  2790. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2791. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2792. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2793. UnicodeString RemoteDirectory)
  2794. {
  2795. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2796. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2797. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2798. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2799. Side = GetSide(Side);
  2800. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2801. if (Side == osRemote)
  2802. {
  2803. Data->Terminal = Terminal;
  2804. Data->Queue = Queue;
  2805. Data->SessionData = CloneCurrentSessionData();
  2806. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2807. Data->RemoteDirectory = RemoteDirectory;
  2808. Data->SessionName = Terminal->SessionData->SessionName;
  2809. Data->LocalRootDirectory = LocalRootDirectory;
  2810. Data->OriginalFileName = OriginalFileName;
  2811. Data->Command = L""; // will be changed later for external editor
  2812. }
  2813. if (ExecuteFileBy == efInternalEditor)
  2814. {
  2815. if (Side == osRemote)
  2816. {
  2817. UnicodeString Caption = UnixIncludeTrailingBackslash(RemoteDirectory) + OriginalFileName +
  2818. L" - " + Terminal->SessionData->SessionName;
  2819. TForm * Editor;
  2820. try
  2821. {
  2822. TNotifyEvent OnFileChanged = NULL;
  2823. TNotifyEvent OnSaveAll = NULL;
  2824. TAnyModifiedEvent OnAnyModified = NULL;
  2825. // Edited files are uploaded in background queue, what we do not support with encrypted files
  2826. if (Terminal->IsCapable[fsBackgroundTransfers])
  2827. {
  2828. OnFileChanged = FEditorManager->FileChanged;
  2829. OnSaveAll = SaveAllInternalEditors;
  2830. OnAnyModified = AnyInternalEditorModified;
  2831. }
  2832. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  2833. FEditorManager->FileReload, FEditorManager->FileClosed,
  2834. OnSaveAll, OnAnyModified,
  2835. Caption, FStandaloneEditing, SessionColor, Terminal->SessionData->InternalEditorEncoding);
  2836. }
  2837. catch(...)
  2838. {
  2839. if (!LocalRootDirectory.IsEmpty())
  2840. {
  2841. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  2842. }
  2843. throw;
  2844. }
  2845. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  2846. }
  2847. else
  2848. {
  2849. DebugAssert(!FStandaloneEditing);
  2850. TForm * Editor =
  2851. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  2852. SaveAllInternalEditors, AnyInternalEditorModified,
  2853. L"", false, SessionColor, -1);
  2854. FLocalEditors->Add(Editor);
  2855. }
  2856. }
  2857. else
  2858. {
  2859. HANDLE Process;
  2860. if (ExecuteFileBy == efExternalEditor)
  2861. {
  2862. UnicodeString Program, Params, Dir;
  2863. Data->Command = ExternalEditor->ExternalEditor;
  2864. ReformatFileNameCommand(Data->Command);
  2865. SplitCommand(Data->Command, Program, Params, Dir);
  2866. Params = ExpandFileNameCommand(Params, FileName);
  2867. Program = ExpandEnvironmentVariables(Program);
  2868. if (!ExecuteShell(Program, Params, Process))
  2869. {
  2870. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  2871. }
  2872. }
  2873. else
  2874. {
  2875. DebugAssert(Side == osRemote);
  2876. if (!ExecuteShell(FileName, L"", Process))
  2877. {
  2878. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  2879. }
  2880. }
  2881. if ((Side == osLocal) ||
  2882. ((ExecuteFileBy == efShell) &&
  2883. !WinConfiguration->Editor.SDIShellEditor) ||
  2884. ((ExecuteFileBy == efExternalEditor) &&
  2885. !ExternalEditor->SDIExternalEditor))
  2886. {
  2887. // no need for handle
  2888. if (Process != NULL)
  2889. {
  2890. DebugCheck(CloseHandle(Process));
  2891. }
  2892. Process = INVALID_HANDLE_VALUE;
  2893. }
  2894. else
  2895. {
  2896. if (Process == NULL)
  2897. {
  2898. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  2899. }
  2900. }
  2901. if (Side == osRemote)
  2902. {
  2903. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  2904. }
  2905. }
  2906. }
  2907. //---------------------------------------------------------------------------
  2908. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  2909. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2910. void * /*Arg*/)
  2911. {
  2912. if (Token != NULL)
  2913. {
  2914. EditorFormFileSave(static_cast<TForm *>(Token));
  2915. }
  2916. }
  2917. //---------------------------------------------------------------------------
  2918. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  2919. {
  2920. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  2921. {
  2922. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  2923. }
  2924. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  2925. }
  2926. //---------------------------------------------------------------------------
  2927. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  2928. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2929. void * Arg)
  2930. {
  2931. if ((Token != NULL) &&
  2932. IsEditorFormModified(static_cast<TForm *>(Token)))
  2933. {
  2934. *static_cast<bool *>(Arg) = true;
  2935. }
  2936. }
  2937. //---------------------------------------------------------------------------
  2938. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  2939. TObject * /*Sender*/, bool & Modified)
  2940. {
  2941. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  2942. {
  2943. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  2944. {
  2945. Modified = true;
  2946. }
  2947. }
  2948. if (!Modified)
  2949. {
  2950. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  2951. }
  2952. }
  2953. //---------------------------------------------------------------------------
  2954. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  2955. {
  2956. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  2957. }
  2958. //---------------------------------------------------------------------------
  2959. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  2960. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  2961. UnicodeString & Result, UnicodeString & RootDirectory)
  2962. {
  2963. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  2964. {
  2965. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  2966. Result = RootDirectory;
  2967. }
  2968. else
  2969. {
  2970. RootDirectory = L"";
  2971. Result = WinConfiguration->ExpandedTemporaryDirectory();
  2972. Result = IncludeTrailingBackslash(Result);
  2973. }
  2974. if (WinConfiguration->TemporaryDirectoryAppendSession)
  2975. {
  2976. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  2977. }
  2978. if (WinConfiguration->TemporaryDirectoryAppendPath)
  2979. {
  2980. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  2981. {
  2982. RemoteDirectory.Delete(1, 1);
  2983. }
  2984. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  2985. }
  2986. if (!ForceDirectories(ApiPath(Result)))
  2987. {
  2988. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  2989. }
  2990. }
  2991. //---------------------------------------------------------------------------
  2992. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  2993. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  2994. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  2995. {
  2996. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2997. if (ForceText)
  2998. {
  2999. CopyParam.TransferMode = tmAscii;
  3000. }
  3001. TemporaryFileCopyParam(CopyParam);
  3002. if (AllFiles)
  3003. {
  3004. CopyParam.IncludeFileMask = TFileMasks();
  3005. }
  3006. if (TempDir.IsEmpty())
  3007. {
  3008. TemporaryDirectoryForRemoteFiles(FTerminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3009. }
  3010. DebugAssert(!FAutoOperation);
  3011. FAutoOperation = AutoOperation;
  3012. Terminal->ExceptionOnFail = true;
  3013. try
  3014. {
  3015. try
  3016. {
  3017. // turn off confirmations, as for MDI editors we may possibly download
  3018. // the same file over
  3019. Terminal->CopyToLocal(
  3020. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3021. if (GetTargetNames)
  3022. {
  3023. for (int i = 0; i < FileList->Count; i++)
  3024. {
  3025. FileList->Strings[i] =
  3026. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3027. }
  3028. }
  3029. }
  3030. catch(...)
  3031. {
  3032. if (!RootTempDir.IsEmpty())
  3033. {
  3034. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3035. }
  3036. throw;
  3037. }
  3038. }
  3039. __finally
  3040. {
  3041. FAutoOperation = false;
  3042. Terminal->ExceptionOnFail = false;
  3043. }
  3044. }
  3045. //---------------------------------------------------------------------------
  3046. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3047. {
  3048. // Do not waste time checking for default editor list the next time,
  3049. // or testing if default editor is not notepad.
  3050. bool TryNextTime = false;
  3051. UnicodeString UsageState;
  3052. if (!WinConfiguration->EditorList->IsDefaultList())
  3053. {
  3054. UsageState = "H";
  3055. }
  3056. else
  3057. {
  3058. UnicodeString Executable;
  3059. UnicodeString ExecutableDescription;
  3060. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3061. {
  3062. UnicodeString Message =
  3063. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3064. unsigned int Answer =
  3065. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3066. if (Answer != qaOK)
  3067. {
  3068. UsageState = "R";
  3069. }
  3070. else
  3071. {
  3072. UsageState = "A";
  3073. TEditorData EditorData;
  3074. EditorData.Editor = edExternal;
  3075. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3076. EditorData.DecideExternalEditorText();
  3077. TEditorList EditorList;
  3078. EditorList = *WinConfiguration->EditorList;
  3079. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3080. WinConfiguration->EditorList = &EditorList;
  3081. }
  3082. }
  3083. }
  3084. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3085. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3086. }
  3087. //---------------------------------------------------------------------------
  3088. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3089. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3090. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3091. {
  3092. if (ExecuteFileBy == efDefaultEditor)
  3093. {
  3094. if (!WinConfiguration->OfferedEditorAutoConfig)
  3095. {
  3096. EditorAutoConfig();
  3097. }
  3098. const TEditorPreferences * Editor =
  3099. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3100. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3101. {
  3102. ExecuteFileBy = efInternalEditor;
  3103. ExternalEditor = NULL;
  3104. }
  3105. else if (Editor->Data->Editor == edOpen)
  3106. {
  3107. ExecuteFileBy = efShell;
  3108. ExternalEditor = NULL;
  3109. }
  3110. else
  3111. {
  3112. ExecuteFileBy = efExternalEditor;
  3113. ExternalEditor = Editor->Data;
  3114. }
  3115. }
  3116. }
  3117. //---------------------------------------------------------------------------
  3118. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3119. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3120. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3121. {
  3122. UnicodeString OriginalFileName;
  3123. UnicodeString LocalRootDirectory;
  3124. UnicodeString RemoteDirectory;
  3125. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3126. (Side == osLocal), MaskParams);
  3127. UnicodeString Counter;
  3128. UnicodeString LocalFileName;
  3129. bool Handled = false;
  3130. if (Side == osRemote)
  3131. {
  3132. // We need to trim VMS version here, so that we use name without version
  3133. // when uploading back to create a new version of the file
  3134. OriginalFileName = FTerminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3135. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3136. TObject * Token = NULL;
  3137. UnicodeString LocalDirectory;
  3138. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3139. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3140. LocalDirectory))
  3141. {
  3142. if (Token != NULL)
  3143. {
  3144. TForm * Form = dynamic_cast<TForm *>(Token);
  3145. if (Form->WindowState == wsMinimized)
  3146. {
  3147. ShowWindow(Form->Handle, SW_RESTORE);
  3148. }
  3149. else
  3150. {
  3151. Form->SetFocus();
  3152. }
  3153. Handled = true;
  3154. }
  3155. else
  3156. {
  3157. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3158. }
  3159. }
  3160. if (!Handled)
  3161. {
  3162. TStringList * FileList1 = new TStringList();
  3163. try
  3164. {
  3165. FileList1->AddObject(FullFileName, Object);
  3166. TemporarilyDownloadFiles(FileList1,
  3167. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3168. LocalRootDirectory, LocalDirectory, true, true, true);
  3169. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3170. }
  3171. __finally
  3172. {
  3173. delete FileList1;
  3174. }
  3175. switch (ExecuteFileBy)
  3176. {
  3177. case efShell:
  3178. Counter = "RemoteFilesExecuted";
  3179. break;
  3180. case efInternalEditor:
  3181. Counter = "RemoteFilesOpenedInInternalEditor";
  3182. break;
  3183. case efExternalEditor:
  3184. Counter = "RemoteFilesOpenedInExternalEditor";
  3185. break;
  3186. default:
  3187. DebugFail();
  3188. }
  3189. }
  3190. }
  3191. else
  3192. {
  3193. LocalFileName = FullFileName;
  3194. OriginalFileName = ExtractFileName(FullFileName);
  3195. switch (ExecuteFileBy)
  3196. {
  3197. case efShell:
  3198. Counter = "LocalFilesExecuted";
  3199. break;
  3200. case efInternalEditor:
  3201. Counter = "LocalFilesOpenedInInternalEditor";
  3202. break;
  3203. case efExternalEditor:
  3204. Counter = "LocalFilesOpenedInExternalEditor";
  3205. break;
  3206. default:
  3207. DebugFail();
  3208. }
  3209. }
  3210. if (!Handled)
  3211. {
  3212. Configuration->Usage->Inc(Counter);
  3213. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3214. ExternalEditor, LocalRootDirectory, RemoteDirectory);
  3215. }
  3216. }
  3217. //---------------------------------------------------------------------------
  3218. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3219. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3220. bool AllSelected, bool OnFocused)
  3221. {
  3222. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3223. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3224. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3225. Side = GetSide(Side);
  3226. TCustomDirView * DView = DirView(Side);
  3227. TStrings * FileList = AllSelected ?
  3228. DView->CreateFileList(OnFocused, Side == osLocal) :
  3229. DView->CreateFocusedFileList(Side == osLocal);
  3230. try
  3231. {
  3232. DebugAssert(AllSelected || (FileList->Count == 1));
  3233. for (int i = 0; i < FileList->Count; i++)
  3234. {
  3235. UnicodeString ListFileName = FileList->Strings[i];
  3236. UnicodeString FileNameOnly = (Side == osRemote) ?
  3237. UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3238. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3239. if (!DView->ItemIsDirectory(Item))
  3240. {
  3241. UnicodeString FullFileName;
  3242. if (Side == osRemote)
  3243. {
  3244. FullFileName = RemoteDirView->Path + ListFileName;
  3245. }
  3246. else
  3247. {
  3248. FullFileName = ListFileName;
  3249. }
  3250. TFileMasks::TParams MaskParams;
  3251. MaskParams.Size = DView->ItemFileSize(Item);
  3252. TDateTimePrecision Precision;
  3253. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3254. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3255. FileList->Objects[i], MaskParams);
  3256. }
  3257. }
  3258. }
  3259. __finally
  3260. {
  3261. delete FileList;
  3262. }
  3263. }
  3264. //---------------------------------------------------------------------------
  3265. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3266. {
  3267. // do not forget to add additional options to TemporarilyDownloadFiles, and AS
  3268. CopyParam.FileNameCase = ncNoChange;
  3269. CopyParam.PreserveRights = false;
  3270. CopyParam.PreserveReadOnly = false;
  3271. CopyParam.ReplaceInvalidChars = true;
  3272. CopyParam.IncludeFileMask = TFileMasks();
  3273. CopyParam.NewerOnly = false;
  3274. CopyParam.FileMask = L"";
  3275. }
  3276. //---------------------------------------------------------------------------
  3277. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3278. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3279. {
  3280. TTerminalManager * Manager = TTerminalManager::Instance();
  3281. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3282. {
  3283. if (!NonVisualDataModule->Busy)
  3284. {
  3285. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3286. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3287. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3288. if (Data->Terminal == NULL)
  3289. {
  3290. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3291. if (SameSiteTerminal != NULL)
  3292. {
  3293. UnicodeString Message =
  3294. FMTLOAD(EDIT_SESSION_REATTACH,
  3295. (FileNameOnly, Data->SessionName, FileNameOnly));
  3296. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3297. {
  3298. Data->Terminal = Terminal;
  3299. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3300. Data->SessionName = Terminal->SessionData->SessionName;
  3301. // We might also overwrite session data
  3302. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3303. }
  3304. else
  3305. {
  3306. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3307. Abort();
  3308. }
  3309. }
  3310. }
  3311. // foreground session should reconnect itself
  3312. else if (Terminal != Data->Terminal)
  3313. {
  3314. UnicodeString Message =
  3315. MainInstructions(
  3316. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3317. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3318. {
  3319. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3320. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3321. }
  3322. else
  3323. {
  3324. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3325. Abort();
  3326. }
  3327. }
  3328. }
  3329. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3330. {
  3331. Configuration->Usage->Inc(L"EditInactiveSession");
  3332. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3333. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3334. (ExtractFileName(FileName), Data->SessionName)));
  3335. }
  3336. }
  3337. TStrings * FileList = new TStringList();
  3338. try
  3339. {
  3340. FileList->Add(FileName);
  3341. // consider using the same settings (preset) as when the file was downloaded
  3342. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3343. TemporaryFileCopyParam(CopyParam);
  3344. if (Data->ForceText)
  3345. {
  3346. CopyParam.TransferMode = tmAscii;
  3347. }
  3348. // so i do not need to worry if masking algorithm works in all cases
  3349. // ("" means "copy file name", no masking is actually done)
  3350. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3351. {
  3352. CopyParam.FileMask = L"";
  3353. }
  3354. else
  3355. {
  3356. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3357. }
  3358. DebugAssert(Data->Queue != NULL);
  3359. int Params = cpNoConfirmation | cpTemporary;
  3360. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3361. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3362. QueueItem->CompleteEvent = UploadCompleteEvent;
  3363. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3364. }
  3365. __finally
  3366. {
  3367. delete FileList;
  3368. }
  3369. }
  3370. //---------------------------------------------------------------------------
  3371. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3372. const UnicodeString FileName, const TEditedFileData * Data)
  3373. {
  3374. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3375. DebugAssert(!NonVisualDataModule->Busy);
  3376. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3377. {
  3378. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3379. (ExtractFileName(FileName), Data->SessionName)));
  3380. }
  3381. TTerminal * PrevTerminal = TTerminalManager::Instance()->ActiveTerminal;
  3382. TTerminalManager::Instance()->ActiveTerminal = Data->Terminal;
  3383. NonVisualDataModule->StartBusy();
  3384. try
  3385. {
  3386. std::unique_ptr<TRemoteFile> File;
  3387. UnicodeString RemoteFileName =
  3388. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3389. FTerminal->ExceptionOnFail = true;
  3390. try
  3391. {
  3392. TRemoteFile * AFile = NULL;
  3393. FTerminal->ReadFile(RemoteFileName, AFile);
  3394. File.reset(AFile);
  3395. if (!File->HaveFullFileName)
  3396. {
  3397. File->FullFileName = RemoteFileName;
  3398. }
  3399. }
  3400. __finally
  3401. {
  3402. FTerminal->ExceptionOnFail = false;
  3403. }
  3404. std::unique_ptr<TStrings> FileList(new TStringList());
  3405. FileList->AddObject(RemoteFileName, File.get());
  3406. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3407. UnicodeString TempDir = ExtractFilePath(FileName);
  3408. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3409. TempDir, true, true, true);
  3410. // sanity check, the target file name should be still the same
  3411. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3412. }
  3413. __finally
  3414. {
  3415. NonVisualDataModule->EndBusy();
  3416. // it actually may not exist anymore...
  3417. TTerminalManager::Instance()->ActiveTerminal = PrevTerminal;
  3418. }
  3419. }
  3420. //---------------------------------------------------------------------------
  3421. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3422. const TEditedFileData * Data, bool & KeepOpen)
  3423. {
  3424. // Command is set for external editors only (not for "shell" open).
  3425. if (!Data->Command.IsEmpty())
  3426. {
  3427. bool AnyFound = false;
  3428. bool AnyMDI = false;
  3429. bool AnyNonMDI = false;
  3430. bool AnyDetect = false;
  3431. TEditorList * EditorList = new TEditorList();
  3432. try
  3433. {
  3434. *EditorList = *WinConfiguration->EditorList;
  3435. for (int i = 0; i < EditorList->Count; i++)
  3436. {
  3437. const TEditorPreferences * Editor = EditorList->Editors[i];
  3438. if ((Editor->Data->Editor == edExternal) &&
  3439. (Editor->Data->ExternalEditor == Data->Command))
  3440. {
  3441. AnyFound = true;
  3442. if (Editor->Data->SDIExternalEditor)
  3443. {
  3444. AnyNonMDI = true;
  3445. if (Editor->Data->DetectMDIExternalEditor)
  3446. {
  3447. AnyDetect = true;
  3448. }
  3449. }
  3450. else
  3451. {
  3452. AnyMDI = true;
  3453. }
  3454. }
  3455. }
  3456. bool EnableMDI = false;
  3457. bool DisableDetect = false;
  3458. if (AnyMDI)
  3459. {
  3460. KeepOpen = true;
  3461. if (AnyNonMDI)
  3462. {
  3463. // there is at least one instance of the editor with MDI support enabled,
  3464. // and one with disabled, enable it for all instances
  3465. EnableMDI = true;
  3466. }
  3467. }
  3468. else if (AnyFound && !AnyDetect)
  3469. {
  3470. // at least once instance found but all have MDI autodetection disabled
  3471. // => close the file (default action)
  3472. }
  3473. else
  3474. {
  3475. // no instance of the editor has MDI support enabled
  3476. TMessageParams Params;
  3477. if (AnyFound)
  3478. {
  3479. // there is at least one instance of the editor
  3480. Params.Params |= mpNeverAskAgainCheck;
  3481. }
  3482. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3483. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3484. switch (Answer)
  3485. {
  3486. case qaNeverAskAgain:
  3487. DisableDetect = true;
  3488. break;
  3489. case qaNo:
  3490. EnableMDI = true;
  3491. KeepOpen = true;
  3492. break;
  3493. }
  3494. }
  3495. if (AnyFound && (EnableMDI || DisableDetect))
  3496. {
  3497. bool Changed = false;
  3498. for (int i = 0; i < EditorList->Count; i++)
  3499. {
  3500. const TEditorPreferences * Editor = EditorList->Editors[i];
  3501. if ((Editor->Data->Editor == edExternal) &&
  3502. (Editor->Data->ExternalEditor == Data->Command) &&
  3503. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3504. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3505. {
  3506. Changed = true;
  3507. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3508. if (EnableMDI)
  3509. {
  3510. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3511. }
  3512. if (DisableDetect)
  3513. {
  3514. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3515. }
  3516. EditorList->Change(i, UpdatedEditor);
  3517. }
  3518. }
  3519. if (Changed)
  3520. {
  3521. WinConfiguration->EditorList = EditorList;
  3522. }
  3523. }
  3524. }
  3525. __finally
  3526. {
  3527. delete EditorList;
  3528. }
  3529. }
  3530. else
  3531. {
  3532. // "open" case
  3533. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3534. qaOK, HELP_APP_EARLY_CLOSED);
  3535. }
  3536. }
  3537. //---------------------------------------------------------------------------
  3538. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3539. {
  3540. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3541. }
  3542. //---------------------------------------------------------------------------
  3543. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3544. {
  3545. SideEnter(osRemote);
  3546. }
  3547. //---------------------------------------------------------------------------
  3548. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3549. {
  3550. MakeNextInTabOrder(RemoteDirView, RemoteDriveView);
  3551. SideEnter(osRemote);
  3552. }
  3553. //---------------------------------------------------------------------------
  3554. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3555. {
  3556. FCurrentSide = Side;
  3557. if (Visible)
  3558. {
  3559. UpdateControls();
  3560. }
  3561. }
  3562. //---------------------------------------------------------------------------
  3563. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3564. TStrings * FileList, bool Alternative)
  3565. {
  3566. TCustomDirView * DView = DirView(Side);
  3567. DView->SaveSelection();
  3568. DView->SaveSelectedNames();
  3569. DebugAssert(!FAlternativeDelete);
  3570. FAlternativeDelete = Alternative;
  3571. try
  3572. {
  3573. if (Side == osRemote)
  3574. {
  3575. DebugAssert(Terminal != NULL);
  3576. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3577. }
  3578. else
  3579. {
  3580. try
  3581. {
  3582. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3583. }
  3584. __finally
  3585. {
  3586. ReloadLocalDirectory();
  3587. }
  3588. }
  3589. FAlternativeDelete = false;
  3590. }
  3591. catch(...)
  3592. {
  3593. FAlternativeDelete = false;
  3594. DView->DiscardSavedSelection();
  3595. throw;
  3596. }
  3597. DView->RestoreSelection();
  3598. }
  3599. //---------------------------------------------------------------------------
  3600. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3601. {
  3602. DebugAssert(Terminal);
  3603. RemoteDirView->SaveSelection();
  3604. RemoteDirView->SaveSelectedNames();
  3605. try
  3606. {
  3607. if (Lock)
  3608. {
  3609. Terminal->LockFiles(FileList);
  3610. }
  3611. else
  3612. {
  3613. Terminal->UnlockFiles(FileList);
  3614. }
  3615. }
  3616. catch(...)
  3617. {
  3618. RemoteDirView->DiscardSavedSelection();
  3619. throw;
  3620. }
  3621. RemoteDirView->RestoreSelection();
  3622. }
  3623. //---------------------------------------------------------------------------
  3624. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TTerminal *& Session,
  3625. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3626. bool NoConfirmation, bool Move)
  3627. {
  3628. DebugAssert(Terminal != NULL);
  3629. // update Terminal->StateData->RemoteDirectory
  3630. UpdateTerminal(Terminal);
  3631. if (Session == NULL)
  3632. {
  3633. Session = TTerminalManager::Instance()->ActiveTerminal;
  3634. }
  3635. if (Session == TTerminalManager::Instance()->ActiveTerminal)
  3636. {
  3637. if (RemoteDriveView->DropTarget != NULL)
  3638. {
  3639. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3640. }
  3641. else if (RemoteDirView->DropTarget != NULL)
  3642. {
  3643. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3644. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3645. }
  3646. else
  3647. {
  3648. Target = RemoteDirView->Path;
  3649. }
  3650. }
  3651. else
  3652. {
  3653. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(Session));
  3654. Target = ManagedTerminal->StateData->RemoteDirectory;
  3655. }
  3656. Target = UnixIncludeTrailingBackslash(Target);
  3657. if (FileList->Count == 1)
  3658. {
  3659. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3660. }
  3661. else
  3662. {
  3663. FileMask = AnyMask;
  3664. }
  3665. DirectCopy = FTerminal->IsCapable[fcRemoteCopy] || FTerminal->IsCapable[fcSecondaryShell];
  3666. bool Result = true;
  3667. if (!NoConfirmation)
  3668. {
  3669. bool Multi = (FileList->Count > 1);
  3670. if (Move)
  3671. {
  3672. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3673. }
  3674. else
  3675. {
  3676. TStrings * Sessions = TTerminalManager::Instance()->TerminalList;
  3677. TStrings * Directories = new TStringList;
  3678. try
  3679. {
  3680. for (int Index = 0; Index < Sessions->Count; Index++)
  3681. {
  3682. TManagedTerminal * Terminal =
  3683. dynamic_cast<TManagedTerminal *>(Sessions->Objects[Index]);
  3684. Directories->Add(Terminal->StateData->RemoteDirectory);
  3685. }
  3686. TDirectRemoteCopy AllowDirectCopy;
  3687. if (FTerminal->IsCapable[fcRemoteCopy] || FTerminal->CommandSessionOpened)
  3688. {
  3689. DebugAssert(DirectCopy);
  3690. AllowDirectCopy = drcAllow;
  3691. }
  3692. else if (FTerminal->IsCapable[fcSecondaryShell])
  3693. {
  3694. DebugAssert(DirectCopy);
  3695. AllowDirectCopy = drcConfirmCommandSession;
  3696. }
  3697. else
  3698. {
  3699. DebugAssert(!DirectCopy);
  3700. AllowDirectCopy = drcDisallow;
  3701. }
  3702. void * ASession = Session;
  3703. Result = DoRemoteCopyDialog(Sessions, Directories, AllowDirectCopy,
  3704. Multi, ASession, Target, FileMask, DirectCopy, TTerminalManager::Instance()->ActiveTerminal);
  3705. Session = static_cast<TTerminal *>(ASession);
  3706. }
  3707. __finally
  3708. {
  3709. delete Directories;
  3710. }
  3711. }
  3712. }
  3713. return Result;
  3714. }
  3715. //---------------------------------------------------------------------------
  3716. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3717. TStrings * FileList, bool NoConfirmation, bool Move, TTerminal * Session)
  3718. {
  3719. bool DirectCopy;
  3720. UnicodeString Target, FileMask;
  3721. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3722. if (Result)
  3723. {
  3724. if (!Move && !DirectCopy)
  3725. {
  3726. Configuration->Usage->Inc("RemoteCopyTemp");
  3727. UnicodeString RootTempDir;
  3728. UnicodeString TempDir;
  3729. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3730. TStrings * TemporaryFilesList = new TStringList();
  3731. try
  3732. {
  3733. TMakeLocalFileListParams MakeFileListParam;
  3734. MakeFileListParam.FileList = TemporaryFilesList;
  3735. MakeFileListParam.FileTimes = NULL;
  3736. MakeFileListParam.IncludeDirs = true;
  3737. MakeFileListParam.Recursive = false;
  3738. ProcessLocalDirectory(TempDir, FTerminal->MakeLocalFileList, &MakeFileListParam);
  3739. TTerminalManager::Instance()->ActiveTerminal = Session;
  3740. if (TemporaryFilesList->Count > 0)
  3741. {
  3742. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3743. CopyParam.FileMask = FileMask;
  3744. DebugAssert(!FAutoOperation);
  3745. FAutoOperation = true;
  3746. FTerminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3747. }
  3748. }
  3749. __finally
  3750. {
  3751. delete TemporaryFilesList;
  3752. FAutoOperation = false;
  3753. if (!RootTempDir.IsEmpty())
  3754. {
  3755. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3756. }
  3757. }
  3758. }
  3759. else
  3760. {
  3761. RemoteDirView->SaveSelection();
  3762. RemoteDirView->SaveSelectedNames();
  3763. try
  3764. {
  3765. if (Move)
  3766. {
  3767. Configuration->Usage->Inc("RemoteMove");
  3768. Terminal->MoveFiles(FileList, Target, FileMask);
  3769. }
  3770. else
  3771. {
  3772. Configuration->Usage->Inc("RemoteCopyDirect");
  3773. DebugAssert(DirectCopy);
  3774. DebugAssert(Session == FTerminal);
  3775. if (FTerminal->IsCapable[fcRemoteCopy] ||
  3776. FTerminal->CommandSessionOpened ||
  3777. CommandSessionFallback())
  3778. {
  3779. Terminal->CopyFiles(FileList, Target, FileMask);
  3780. }
  3781. }
  3782. }
  3783. catch(...)
  3784. {
  3785. RemoteDirView->DiscardSavedSelection();
  3786. throw;
  3787. }
  3788. RemoteDirView->RestoreSelection();
  3789. }
  3790. }
  3791. return Result;
  3792. }
  3793. //---------------------------------------------------------------------------
  3794. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3795. const UnicodeString & Path, TRemoteProperties & Properties)
  3796. {
  3797. Properties.Valid = Properties.Valid << vpEncrypt;
  3798. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  3799. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  3800. }
  3801. //---------------------------------------------------------------------------
  3802. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3803. {
  3804. Side = GetSide(Side);
  3805. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3806. TRemoteProperties * AProperties = (Side == osRemote ? &Properties : NULL);
  3807. UnicodeString Name = LoadStr(NEW_FOLDER);
  3808. int AllowedChanges =
  3809. FLAGMASK((Side == osRemote) && Terminal->IsCapable[fcModeChanging], cpMode);
  3810. bool SaveSettings = false;
  3811. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  3812. {
  3813. TWindowLock Lock(this);
  3814. if (Side == osRemote)
  3815. {
  3816. if (SaveSettings)
  3817. {
  3818. GUIConfiguration->NewDirectoryProperties = Properties;
  3819. }
  3820. CreateRemoteDirectory(Name, Properties);
  3821. }
  3822. else
  3823. {
  3824. DirView(Side)->CreateDirectory(Name);
  3825. }
  3826. }
  3827. }
  3828. //---------------------------------------------------------------------------
  3829. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  3830. {
  3831. TWindowLock Lock(this);
  3832. DirView(Side)->ExecuteHomeDirectory();
  3833. }
  3834. //---------------------------------------------------------------------------
  3835. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  3836. {
  3837. DoOpenDirectoryDialog(odBrowse, Side);
  3838. }
  3839. //---------------------------------------------------------------------------
  3840. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  3841. {
  3842. UnicodeString Path = Bookmark->GetSideDirectory(Side);
  3843. bool Result = !Path.IsEmpty();
  3844. if (Result)
  3845. {
  3846. // While we might get here when the session is closed (from location profiles),
  3847. // it's not a problem as the Path setter is noop then
  3848. DirView(Side)->Path = Path;
  3849. }
  3850. return Result;
  3851. }
  3852. //---------------------------------------------------------------------------
  3853. void __fastcall TCustomScpExplorerForm::CalculateSize(
  3854. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  3855. bool & Close)
  3856. {
  3857. // terminal can be already closed (e.g. dropped connection)
  3858. if (Terminal != NULL)
  3859. {
  3860. try
  3861. {
  3862. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  3863. }
  3864. catch(...)
  3865. {
  3866. if (!Terminal->Active)
  3867. {
  3868. Close = true;
  3869. }
  3870. throw;
  3871. }
  3872. }
  3873. }
  3874. //---------------------------------------------------------------------------
  3875. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  3876. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  3877. bool & Close)
  3878. {
  3879. // terminal can be already closed (e.g. dropped connection)
  3880. if (Terminal != NULL)
  3881. {
  3882. Configuration->Usage->Inc(L"ChecksumCalculated");
  3883. try
  3884. {
  3885. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  3886. }
  3887. catch(...)
  3888. {
  3889. if (!Terminal->Active)
  3890. {
  3891. Close = true;
  3892. }
  3893. throw;
  3894. }
  3895. }
  3896. }
  3897. //---------------------------------------------------------------------------
  3898. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  3899. {
  3900. bool Result;
  3901. if (Side == osRemote)
  3902. {
  3903. TRemoteTokenList * GroupList = NULL;
  3904. TRemoteTokenList * UserList = NULL;
  3905. try
  3906. {
  3907. TRemoteProperties CurrentProperties;
  3908. if (Terminal->LoadFilesProperties(FileList))
  3909. {
  3910. RemoteDirView->Invalidate();
  3911. }
  3912. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  3913. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  3914. if (CapableGroupChanging || CapableOwnerChanging)
  3915. {
  3916. if (CapableGroupChanging)
  3917. {
  3918. GroupList = Terminal->Groups->Duplicate();
  3919. }
  3920. if (CapableOwnerChanging)
  3921. {
  3922. UserList = Terminal->Users->Duplicate();
  3923. }
  3924. TRemoteDirectory * Files = Terminal->Files;
  3925. int Count = Files->Count;
  3926. if (Count > 100)
  3927. {
  3928. Count = 100;
  3929. }
  3930. for (int Index = 0; Index < Count; Index++)
  3931. {
  3932. TRemoteFile * File = Files->Files[Index];
  3933. if (CapableGroupChanging)
  3934. {
  3935. GroupList->AddUnique(File->Group);
  3936. }
  3937. if (CapableOwnerChanging)
  3938. {
  3939. UserList->AddUnique(File->Owner);
  3940. }
  3941. }
  3942. // if we haven't collected tokens for all files in current directory,
  3943. // make sure we collect them at least for all selected files.
  3944. // (note that so far the files in FileList has to be from current direcotry)
  3945. if (Count < Files->Count)
  3946. {
  3947. for (int Index = 0; Index < FileList->Count; Index++)
  3948. {
  3949. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  3950. if (CapableGroupChanging)
  3951. {
  3952. GroupList->AddUnique(File->Group);
  3953. }
  3954. if (CapableOwnerChanging)
  3955. {
  3956. UserList->AddUnique(File->Owner);
  3957. }
  3958. }
  3959. }
  3960. }
  3961. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  3962. int Flags = 0;
  3963. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  3964. if (CapableOwnerChanging) Flags |= cpOwner;
  3965. if (CapableGroupChanging) Flags |= cpGroup;
  3966. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  3967. if (Terminal->IsCapable[fcCalculatingChecksum])
  3968. {
  3969. CalculateChecksumEvent = CalculateChecksum;
  3970. }
  3971. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  3972. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  3973. TRemoteProperties NewProperties = CurrentProperties;
  3974. Result =
  3975. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  3976. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  3977. Terminal->IsCapable[fcGroupOwnerChangingByID],
  3978. CalculateSize, CalculateChecksumEvent);
  3979. if (Result)
  3980. {
  3981. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  3982. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  3983. }
  3984. }
  3985. __finally
  3986. {
  3987. delete GroupList;
  3988. delete UserList;
  3989. }
  3990. }
  3991. else
  3992. {
  3993. DirView(Side)->DisplayPropertiesMenu();
  3994. Result = true;
  3995. }
  3996. return Result;
  3997. }
  3998. //---------------------------------------------------------------------------
  3999. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4000. {
  4001. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4002. {
  4003. FIgnoreNextDialogChar = Key;
  4004. }
  4005. Key = 0;
  4006. }
  4007. //---------------------------------------------------------------------------
  4008. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4009. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4010. {
  4011. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4012. if (Command != NULL)
  4013. {
  4014. KeyProcessed(Key, Shift);
  4015. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4016. {
  4017. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4018. false, false, const_cast<TCustomCommandType *>(Command));
  4019. }
  4020. }
  4021. }
  4022. //---------------------------------------------------------------------------
  4023. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4024. {
  4025. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4026. {
  4027. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4028. }
  4029. if (!DirView(osCurrent)->IsEditing())
  4030. {
  4031. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4032. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4033. {
  4034. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4035. if (((Action->ShortCut == KeyShortCut) ||
  4036. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4037. AllowedAction(Action, aaShortCut))
  4038. {
  4039. // Has to be called before the action as the dialog char is already in queue.
  4040. // So when the action consumes message queue, we already need to have the
  4041. // FIgnoreNextDialogChar set
  4042. KeyProcessed(Key, Shift);
  4043. // Reset reference to previous component (when menu/toolbar was clicked).
  4044. // Needed to detect that action was invoked by keyboard shortcut
  4045. // in TNonVisualDataModule::ExplorerActionsExecute
  4046. Action->ActionComponent = NULL;
  4047. Action->Execute();
  4048. return;
  4049. }
  4050. }
  4051. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4052. {
  4053. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4054. {
  4055. KeyProcessed(Key, Shift);
  4056. TTerminalManager::Instance()->ActiveTerminalIndex = i;
  4057. return;
  4058. }
  4059. }
  4060. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4061. {
  4062. KeyProcessed(Key, Shift);
  4063. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4064. }
  4065. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4066. if (IsCustomShortCut(CustomShortCut))
  4067. {
  4068. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4069. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4070. if (WinConfiguration->SharedBookmarks != NULL)
  4071. {
  4072. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4073. if ((Bookmark != NULL) &&
  4074. OpenBookmark(FCurrentSide, Bookmark))
  4075. {
  4076. KeyProcessed(Key, Shift);
  4077. }
  4078. }
  4079. }
  4080. }
  4081. TForm::KeyDown(Key, Shift);
  4082. }
  4083. //---------------------------------------------------------------------------
  4084. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4085. {
  4086. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4087. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  4088. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4089. DebugAssert(Terminal);
  4090. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4091. bool SecurityEnabled = !SessionInfo.SecurityProtocolName.IsEmpty();
  4092. SessionStatusBar->Panels->Items[Offset + 0]->Enabled = SecurityEnabled;
  4093. // expanded from ?: to avoid memory leaks
  4094. if (SecurityEnabled)
  4095. {
  4096. SessionStatusBar->Panels->Items[Offset + 0]->Hint =
  4097. FMTLOAD(STATUS_SECURE, (SessionInfo.SecurityProtocolName));
  4098. }
  4099. else
  4100. {
  4101. SessionStatusBar->Panels->Items[Offset + 0]->Hint = LoadStr(STATUS_INSECURE);
  4102. }
  4103. if (FileSystemInfo.ProtocolName.IsEmpty())
  4104. {
  4105. SessionStatusBar->Panels->Items[Offset + 1]->Caption = SessionInfo.ProtocolName;
  4106. }
  4107. else
  4108. {
  4109. SessionStatusBar->Panels->Items[Offset + 1]->Caption = FileSystemInfo.ProtocolName;
  4110. }
  4111. SessionStatusBar->Panels->Items[Offset + 1]->Hint = LoadStr(STATUS_PROTOCOL_HINT);
  4112. SessionStatusBar->Panels->Items[Offset + 2]->Enabled =
  4113. (!SessionInfo.CSCompression.IsEmpty() || !SessionInfo.SCCompression.IsEmpty());
  4114. UnicodeString CSCompressionStr = DefaultStr(SessionInfo.CSCompression, LoadStr(NO_STR));
  4115. UnicodeString SCCompressionStr = DefaultStr(SessionInfo.SCCompression, LoadStr(NO_STR));
  4116. UnicodeString CompressionStr = CSCompressionStr;
  4117. if (CSCompressionStr != SCCompressionStr)
  4118. {
  4119. CompressionStr += UnicodeString(L"/") + SCCompressionStr;
  4120. }
  4121. SessionStatusBar->Panels->Items[Offset + 2]->Hint = FMTLOAD(STATUS_COMPRESSION_HINT, (CompressionStr));
  4122. SessionStatusBar->Panels->Items[Offset + 3]->Hint = LoadStr(STATUS_DURATION_HINT);
  4123. UpdateStatusBar();
  4124. }
  4125. //---------------------------------------------------------------------------
  4126. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4127. {
  4128. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4129. DebugAssert(SessionStatusBar != NULL);
  4130. if (!Terminal || !Terminal->Active || Terminal->Status < ssOpened)
  4131. {
  4132. // note: (Terminal->Status < sshReady) currently never happens here,
  4133. // so STATUS_CONNECTING is never used
  4134. SessionStatusBar->SimplePanel = true;
  4135. SessionStatusBar->SimpleText = LoadStr(
  4136. !Terminal || !Terminal->Active ? STATUS_NOT_CONNECTED : STATUS_CONNECTING);
  4137. }
  4138. else
  4139. {
  4140. DebugAssert(Terminal);
  4141. SessionStatusBar->SimplePanel = false;
  4142. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4143. if (!FNote.IsEmpty())
  4144. {
  4145. SessionStatusBar->Panels->Items[0]->Caption = FNote;
  4146. }
  4147. else
  4148. {
  4149. UpdateStatusPanelText(SessionStatusBar->Panels->Items[0]);
  4150. }
  4151. SessionStatusBar->Panels->Items[0]->Hint = FNoteHints;
  4152. SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1]->Caption =
  4153. FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4154. }
  4155. }
  4156. //---------------------------------------------------------------------------
  4157. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4158. {
  4159. Panel->Caption = L"";
  4160. }
  4161. //---------------------------------------------------------------------------
  4162. void __fastcall TCustomScpExplorerForm::Idle()
  4163. {
  4164. if (FShowing || FStandaloneEditing)
  4165. {
  4166. FEditorManager->Check();
  4167. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4168. // that needs to know if queue view is visible (and it may be closed after queue update)
  4169. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4170. }
  4171. if (FShowing)
  4172. {
  4173. if (!NonVisualDataModule->Busy &&
  4174. // Menu is opened or drag&drop is going on
  4175. (Mouse->Capture == NULL))
  4176. {
  4177. if (FRefreshRemoteDirectory)
  4178. {
  4179. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4180. {
  4181. Terminal->RefreshDirectory();
  4182. }
  4183. FRefreshRemoteDirectory = false;
  4184. }
  4185. if (FRefreshLocalDirectory)
  4186. {
  4187. ReloadLocalDirectory();
  4188. FRefreshLocalDirectory = false;
  4189. }
  4190. if (WinConfiguration->RefreshRemotePanel)
  4191. {
  4192. TManagedTerminal * ManagedTerminal =
  4193. dynamic_cast<TManagedTerminal *>(Terminal);
  4194. if ((ManagedTerminal != NULL) && (Terminal->Status == ssOpened) &&
  4195. (Now() - ManagedTerminal->DirectoryLoaded >
  4196. WinConfiguration->RefreshRemotePanelInterval))
  4197. {
  4198. RemoteDirView->ReloadDirectory();
  4199. }
  4200. }
  4201. }
  4202. }
  4203. if (FShowing || FStandaloneEditing)
  4204. {
  4205. if (FQueueStatusInvalidated)
  4206. {
  4207. UpdateQueueStatus(false);
  4208. }
  4209. RefreshQueueItems();
  4210. }
  4211. if (FShowing)
  4212. {
  4213. UpdateStatusBar();
  4214. }
  4215. FIgnoreNextDialogChar = 0;
  4216. }
  4217. //---------------------------------------------------------------------------
  4218. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4219. {
  4220. try
  4221. {
  4222. FUserActionTimer->Enabled = false;
  4223. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4224. {
  4225. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4226. {
  4227. FPendingQueueActionItem->ProcessUserAction();
  4228. }
  4229. }
  4230. }
  4231. __finally
  4232. {
  4233. FPendingQueueActionItem = NULL;
  4234. }
  4235. }
  4236. //---------------------------------------------------------------------------
  4237. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4238. {
  4239. if (WinConfiguration->MinimizeToTray)
  4240. {
  4241. UpdateTrayIcon();
  4242. FTrayIcon->Visible = true;
  4243. if (Visible)
  4244. {
  4245. ShowWindow(Handle, SW_HIDE);
  4246. }
  4247. }
  4248. }
  4249. //---------------------------------------------------------------------------
  4250. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4251. {
  4252. // WORKAROUND
  4253. // When restoring maximized window from minimization,
  4254. // rarely some controls do not align properly.
  4255. // Two instances seen (both for Commander):
  4256. // - When restoring, window is temporarily narrower (not maximized),
  4257. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4258. // Once maximized already, top dock shinks back, but the session PageControl,
  4259. // do not align up, leaving space between TopDock and PageControl.
  4260. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4261. for (int Index = 0; Index < ControlCount; Index++)
  4262. {
  4263. RealignControl(Controls[Index]);
  4264. }
  4265. if (FTrayIcon->Visible)
  4266. {
  4267. FTrayIcon->Visible = false;
  4268. if (Visible)
  4269. {
  4270. ShowWindow(Handle, SW_SHOW);
  4271. }
  4272. }
  4273. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4274. {
  4275. FNeedSession = false;
  4276. NonVisualDataModule->StartBusy();
  4277. try
  4278. {
  4279. NeedSession(false);
  4280. }
  4281. __finally
  4282. {
  4283. NonVisualDataModule->EndBusy();
  4284. }
  4285. }
  4286. }
  4287. //---------------------------------------------------------------------------
  4288. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4289. {
  4290. FTrayIcon->Hint = GetMainForm()->Caption;
  4291. }
  4292. //---------------------------------------------------------------------------
  4293. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4294. {
  4295. UpdateTrayIcon();
  4296. }
  4297. //---------------------------------------------------------------------------
  4298. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4299. {
  4300. // workaround
  4301. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4302. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4303. // But WM_ACTIVATEAPP message can be received even when
  4304. // the main window is minimized to the tray and internal editor window is focused
  4305. // (after another application was previously active)
  4306. if (::IsIconic(Handle))
  4307. {
  4308. if (!IsAppIconic())
  4309. {
  4310. SetAppIconic(true);
  4311. }
  4312. }
  4313. ::ApplicationRestore();
  4314. Application->BringToFront();
  4315. }
  4316. //---------------------------------------------------------------------------
  4317. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4318. {
  4319. RestoreApp();
  4320. }
  4321. //---------------------------------------------------------------------------
  4322. void __fastcall TCustomScpExplorerForm::NewSession(bool FromSite, const UnicodeString & SessionUrl)
  4323. {
  4324. if (OpenInNewWindow())
  4325. {
  4326. // todo: Pass FromSite
  4327. ExecuteNewInstance(SessionUrl);
  4328. }
  4329. else
  4330. {
  4331. TTerminalManager::Instance()->NewSession(FromSite, SessionUrl);
  4332. }
  4333. }
  4334. //---------------------------------------------------------------------------
  4335. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4336. {
  4337. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->SessionName;
  4338. // current working directories become defaults here, what is not right
  4339. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4340. StoredSessions->NewSession(SessionName, SessionData.get());
  4341. // modified only, explicit
  4342. StoredSessions->Save(false, true);
  4343. // encode session name because of slashes in hierarchical sessions
  4344. return EncodeUrlString(SessionName);
  4345. }
  4346. //---------------------------------------------------------------------------
  4347. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4348. {
  4349. if (OpenInNewWindow())
  4350. {
  4351. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4352. }
  4353. else
  4354. {
  4355. // current working directories become defaults here, what is not right
  4356. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4357. TTerminalManager * Manager = TTerminalManager::Instance();
  4358. TTerminal * Terminal = Manager->NewTerminal(SessionData.get());
  4359. Manager->ActiveTerminal = Terminal;
  4360. }
  4361. }
  4362. //---------------------------------------------------------------------------
  4363. void __fastcall TCustomScpExplorerForm::RenameSession()
  4364. {
  4365. UnicodeString Name = Terminal->SessionData->Name;
  4366. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME))
  4367. {
  4368. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4369. TSessionData::ValidateName(Name);
  4370. // This is inconsistent with how color (for example) is handled.
  4371. Terminal->SessionData->Name = Name;
  4372. UpdateControls();
  4373. // Add/Remove distinguishing paths from sessions of the same name.
  4374. DoTerminalListChanged();
  4375. }
  4376. }
  4377. //---------------------------------------------------------------------------
  4378. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4379. {
  4380. DebugAssert(Queue != NULL);
  4381. bool Result = Queue->IsEmpty;
  4382. if (!Result)
  4383. {
  4384. SetFocus();
  4385. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4386. }
  4387. return Result;
  4388. }
  4389. //---------------------------------------------------------------------------
  4390. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4391. {
  4392. return CanCloseQueue(FQueue);
  4393. }
  4394. //---------------------------------------------------------------------------
  4395. void __fastcall TCustomScpExplorerForm::CloseSession()
  4396. {
  4397. if (CanCloseQueue())
  4398. {
  4399. TTerminalManager::Instance()->FreeActiveTerminal();
  4400. }
  4401. }
  4402. //---------------------------------------------------------------------------
  4403. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4404. {
  4405. if (OpenInNewWindow())
  4406. {
  4407. // encode session name because of slashes in hierarchical sessions
  4408. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4409. }
  4410. else
  4411. {
  4412. TTerminalManager * Manager = TTerminalManager::Instance();
  4413. TTerminal * Terminal = Manager->NewTerminal(Data);
  4414. Manager->ActiveTerminal = Terminal;
  4415. }
  4416. }
  4417. //---------------------------------------------------------------------------
  4418. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4419. {
  4420. if (OpenInNewWindow())
  4421. {
  4422. ExecuteNewInstance(Name);
  4423. }
  4424. else
  4425. {
  4426. TTerminalManager * Manager = TTerminalManager::Instance();
  4427. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4428. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4429. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  4430. }
  4431. }
  4432. //---------------------------------------------------------------------------
  4433. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4434. bool &CanClose)
  4435. {
  4436. if (Terminal != NULL)
  4437. {
  4438. if (Terminal->Active && WinConfiguration->ConfirmClosingSession)
  4439. {
  4440. unsigned int Result;
  4441. TMessageParams Params(mpNeverAskAgainCheck);
  4442. UnicodeString Message;
  4443. int Answers = qaOK | qaCancel;
  4444. if (TTerminalManager::Instance()->Count > 1)
  4445. {
  4446. if (!WinConfiguration->AutoSaveWorkspace)
  4447. {
  4448. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE2);
  4449. Answers = qaYes | qaNo | qaCancel;
  4450. }
  4451. else
  4452. {
  4453. Message = MainInstructions(LoadStr(CLOSE_SESSIONS));
  4454. }
  4455. }
  4456. else
  4457. {
  4458. Message = MainInstructions(FMTLOAD(CLOSE_SESSION, (Terminal->SessionData->SessionName)));
  4459. }
  4460. if (WinConfiguration->AutoSaveWorkspace)
  4461. {
  4462. Message =
  4463. FORMAT("%s\n\n%s", (Message,
  4464. FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()))));
  4465. }
  4466. SetFocus();
  4467. Result = MessageDialog(Message, qtConfirmation,
  4468. Answers, HELP_NONE, &Params);
  4469. if (Result == qaNeverAskAgain)
  4470. {
  4471. WinConfiguration->ConfirmClosingSession = false;
  4472. }
  4473. if (Result == qaNo)
  4474. {
  4475. CanClose = SaveWorkspace(true);
  4476. // note that the workspace will be saved redundatly again from FormClose
  4477. }
  4478. else
  4479. {
  4480. CanClose =
  4481. (Result == qaOK) ||
  4482. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4483. (Result == qaNeverAskAgain);
  4484. }
  4485. }
  4486. if (CanClose)
  4487. {
  4488. CanClose = CanCloseQueue();
  4489. }
  4490. }
  4491. if (CanClose)
  4492. {
  4493. CanClose =
  4494. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4495. FEditorManager->CloseExternalFilesWithoutProcess();
  4496. if (CanClose)
  4497. {
  4498. while (CanClose && (FLocalEditors->Count > 0))
  4499. {
  4500. int PrevCount = FLocalEditors->Count;
  4501. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4502. CanClose = (FLocalEditors->Count < PrevCount);
  4503. }
  4504. if (CanClose)
  4505. {
  4506. CanClose = FEditorManager->Empty(true);
  4507. if (!CanClose)
  4508. {
  4509. SetFocus();
  4510. CanClose =
  4511. (MessageDialog(
  4512. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4513. }
  4514. }
  4515. }
  4516. }
  4517. }
  4518. //---------------------------------------------------------------------------
  4519. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4520. {
  4521. TForm * Form = dynamic_cast<TForm *>(Sender);
  4522. DebugAssert(Form != NULL);
  4523. Form->Close();
  4524. }
  4525. //---------------------------------------------------------------------------
  4526. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4527. {
  4528. TForm * Form = dynamic_cast<TForm *>(Sender);
  4529. delete Form;
  4530. }
  4531. //---------------------------------------------------------------------------
  4532. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4533. {
  4534. while (FLocalEditors->Count > 0)
  4535. {
  4536. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4537. }
  4538. }
  4539. //---------------------------------------------------------------------------
  4540. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4541. TObject *Sender)
  4542. {
  4543. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4544. try
  4545. {
  4546. SetProperties(osRemote, FileList);
  4547. }
  4548. __finally
  4549. {
  4550. delete FileList;
  4551. }
  4552. }
  4553. //---------------------------------------------------------------------------
  4554. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4555. {
  4556. if (value)
  4557. {
  4558. if (Component == fcCustomCommandsBand)
  4559. {
  4560. UpdateCustomCommandsToolbar();
  4561. }
  4562. }
  4563. }
  4564. //---------------------------------------------------------------------------
  4565. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4566. {
  4567. TControl * Control = GetComponent(Component);
  4568. DebugAssert(Control);
  4569. bool Changed = (Control->Visible != value);
  4570. if (Changed)
  4571. {
  4572. ComponentShowing(Component, value);
  4573. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4574. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4575. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4576. if (value)
  4577. {
  4578. int RemainingHeight = Control->Parent->ClientHeight;
  4579. int RemainingWidth = Control->Parent->ClientWidth;
  4580. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4581. {
  4582. TControl * ChildControl = Control->Parent->Controls[i];
  4583. if (ChildControl->Visible)
  4584. {
  4585. switch (ChildControl->Align)
  4586. {
  4587. case alTop:
  4588. case alBottom:
  4589. RemainingHeight -= ChildControl->Height;
  4590. break;
  4591. case alLeft:
  4592. case alRight:
  4593. RemainingWidth -= ChildControl->Width;
  4594. break;
  4595. }
  4596. }
  4597. }
  4598. int Reserve = ScaleByTextHeight(this, 32);
  4599. // queue in explorer, trees in commander
  4600. if (Control->Height > RemainingHeight - Reserve)
  4601. {
  4602. Control->Height = RemainingHeight / 2;
  4603. }
  4604. if (Control->Width > RemainingWidth - Reserve)
  4605. {
  4606. Control->Width = RemainingWidth / 2;
  4607. }
  4608. }
  4609. Control->Visible = value;
  4610. if (WasFocused && Visible)
  4611. {
  4612. DirView(osCurrent)->SetFocus();
  4613. }
  4614. FixControlsPlacement();
  4615. }
  4616. }
  4617. //---------------------------------------------------------------------------
  4618. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4619. {
  4620. TControl * Control = GetComponent(Component);
  4621. if (Control == NULL)
  4622. {
  4623. return false;
  4624. }
  4625. else
  4626. {
  4627. return Control->Visible;
  4628. }
  4629. }
  4630. //---------------------------------------------------------------------------
  4631. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4632. {
  4633. bool Result;
  4634. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4635. {
  4636. Result = !IsUWP();
  4637. }
  4638. else
  4639. {
  4640. Result = true;
  4641. }
  4642. return Result;
  4643. }
  4644. //---------------------------------------------------------------------------
  4645. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4646. {
  4647. if (RemoteDirView->ItemFocused != NULL)
  4648. {
  4649. RemoteDirView->ItemFocused->MakeVisible(false);
  4650. }
  4651. QueueSplitter->Visible = QueuePanel->Visible;
  4652. RemotePanelSplitter->Visible = RemoteDriveView->Visible;
  4653. }
  4654. //---------------------------------------------------------------------------
  4655. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4656. {
  4657. switch (Component) {
  4658. case fcStatusBar: return RemoteStatusBar;
  4659. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4660. case fcQueueView: return QueuePanel;
  4661. case fcQueueToolbar: return QueueDock;
  4662. case fcRemoteTree: return RemoteDriveView;
  4663. case fcSessionsTabs: return SessionsPageControl;
  4664. default: return NULL;
  4665. }
  4666. }
  4667. //---------------------------------------------------------------------------
  4668. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4669. TObject *Sender, TListColumn *Column, TPoint &Point)
  4670. {
  4671. DebugAssert(NonVisualDataModule && Column && Sender);
  4672. NonVisualDataModule->ListColumn = Column;
  4673. TPopupMenu * DirViewColumnMenu;
  4674. if (Sender == RemoteDirView)
  4675. {
  4676. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4677. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4678. (Column->Index == uvName);
  4679. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4680. (Column->Index == uvSize);
  4681. }
  4682. else
  4683. {
  4684. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4685. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4686. (Column->Index == dvName);
  4687. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4688. (Column->Index == dvSize);
  4689. }
  4690. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  4691. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  4692. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  4693. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4694. }
  4695. //---------------------------------------------------------------------------
  4696. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4697. TObject *Sender, TListItem *Item, bool &AllowExec)
  4698. {
  4699. DoDirViewExecFile(Sender, Item, AllowExec);
  4700. }
  4701. //---------------------------------------------------------------------------
  4702. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4703. TListItem * Item, bool & AllowExec)
  4704. {
  4705. DebugAssert(Sender && Item && Configuration);
  4706. DebugAssert(AllowExec);
  4707. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  4708. bool Remote = (ADirView == DirView(osRemote));
  4709. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks;
  4710. TOperationSide Side = (Remote ? osRemote : osLocal);
  4711. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  4712. // on files only (not directories)
  4713. // and only when symlinks are resolved (apply to remote panel only)
  4714. if (!ADirView->ItemIsDirectory(Item) &&
  4715. (ResolvedSymlinks || FForceExecution || Terminal->IsEncryptingFiles()))
  4716. {
  4717. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  4718. !FForceExecution &&
  4719. (ResolvedSymlinks || Terminal->IsEncryptingFiles()))
  4720. {
  4721. if (WinConfiguration->DoubleClickAction == dcaCopy)
  4722. {
  4723. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  4724. UnlockWindow();
  4725. try
  4726. {
  4727. ExecuteFileOperation(
  4728. foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  4729. }
  4730. __finally
  4731. {
  4732. LockWindow();
  4733. }
  4734. AllowExec = false;
  4735. }
  4736. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  4737. {
  4738. if (!Remote || !WinConfiguration->DisableOpenEdit)
  4739. {
  4740. ExecuteFile(Side, efDefaultEditor);
  4741. AllowExec = false;
  4742. }
  4743. }
  4744. else
  4745. {
  4746. DebugFail();
  4747. }
  4748. }
  4749. // if we have not done anything special, fall back to default behavior
  4750. if (AllowExec)
  4751. {
  4752. if (Remote && !WinConfiguration->DisableOpenEdit)
  4753. {
  4754. ExecuteFile(osRemote, efShell);
  4755. AllowExec = false;
  4756. }
  4757. }
  4758. }
  4759. }
  4760. //---------------------------------------------------------------------------
  4761. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  4762. {
  4763. return ((Side == osRemote) || (Side == osCurrent));
  4764. }
  4765. //---------------------------------------------------------------------------
  4766. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  4767. {
  4768. DebugFail();
  4769. }
  4770. //---------------------------------------------------------------------------
  4771. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  4772. {
  4773. DebugFail();
  4774. }
  4775. //---------------------------------------------------------------------------
  4776. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  4777. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, bool UseDefaults)
  4778. {
  4779. TSynchronizeParamType Params;
  4780. Params.LocalDirectory = LocalDirectory;
  4781. Params.RemoteDirectory = RemoteDirectory;
  4782. int UnusedParams =
  4783. (GUIConfiguration->SynchronizeParams &
  4784. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize));
  4785. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  4786. Params.Options = GUIConfiguration->SynchronizeOptions;
  4787. bool SaveSettings = false;
  4788. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  4789. &DoSynchronizeTooManyDirectories);
  4790. DebugAssert(FSynchronizeController == NULL);
  4791. FSynchronizeController = &Controller;
  4792. bool Result;
  4793. try
  4794. {
  4795. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4796. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  4797. int Options =
  4798. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  4799. DebugAssert(FOnFeedSynchronizeError == NULL);
  4800. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  4801. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  4802. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  4803. if (Result)
  4804. {
  4805. if (SaveSettings)
  4806. {
  4807. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  4808. GUIConfiguration->SynchronizeOptions = Params.Options;
  4809. }
  4810. else
  4811. {
  4812. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  4813. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  4814. FLAGSET(Params.Options, soSynchronize))
  4815. {
  4816. GUIConfiguration->SynchronizeOptions =
  4817. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  4818. soSynchronize;
  4819. }
  4820. }
  4821. LocalDirectory = Params.LocalDirectory;
  4822. RemoteDirectory = Params.RemoteDirectory;
  4823. }
  4824. }
  4825. __finally
  4826. {
  4827. FSynchronizeController = NULL;
  4828. DebugAssert(FOnFeedSynchronizeError == NULL);
  4829. FOnFeedSynchronizeError = NULL;
  4830. }
  4831. return Result;
  4832. }
  4833. //---------------------------------------------------------------------------
  4834. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  4835. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  4836. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  4837. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  4838. TSynchronizeOptions * Options, bool Full)
  4839. {
  4840. // Keep-up-to-date mode
  4841. if (Terminal->Status == ssOpened)
  4842. {
  4843. try
  4844. {
  4845. int PParams = Params.Params;
  4846. if (!Full)
  4847. {
  4848. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  4849. if (FLAGSET(Params.Options, soRecurse))
  4850. {
  4851. PParams |= TTerminal::spSubDirs;
  4852. }
  4853. }
  4854. else
  4855. {
  4856. // if keepuptodate is non-recursive,
  4857. // full sync before has to be non-recursive as well
  4858. if (FLAGCLEAR(Params.Options, soRecurse))
  4859. {
  4860. PParams |= TTerminal::spNoRecurse;
  4861. }
  4862. }
  4863. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  4864. PParams, Checklist, Options);
  4865. }
  4866. catch (Exception & E)
  4867. {
  4868. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  4869. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  4870. {
  4871. // noop, already logged in MoreMessageDialog
  4872. }
  4873. else
  4874. {
  4875. ShowExtendedExceptionEx(Terminal, &E);
  4876. throw;
  4877. }
  4878. }
  4879. }
  4880. }
  4881. //---------------------------------------------------------------------------
  4882. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  4883. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  4884. const UnicodeString ErrorStr)
  4885. {
  4886. if (!Directory.IsEmpty())
  4887. {
  4888. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  4889. }
  4890. else
  4891. {
  4892. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  4893. }
  4894. }
  4895. //---------------------------------------------------------------------------
  4896. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  4897. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  4898. {
  4899. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  4900. {
  4901. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  4902. }
  4903. else
  4904. {
  4905. TMessageParams Params(mpNeverAskAgainCheck);
  4906. unsigned int Result = MessageDialog(
  4907. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  4908. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  4909. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  4910. {
  4911. MaxDirectories *= 2;
  4912. if (Result == qaNeverAskAgain)
  4913. {
  4914. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  4915. }
  4916. }
  4917. else
  4918. {
  4919. Abort();
  4920. }
  4921. }
  4922. }
  4923. //---------------------------------------------------------------------------
  4924. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  4925. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  4926. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  4927. TSynchronizeOptions * Options)
  4928. {
  4929. // Keep-up-to-date mode
  4930. DebugAssert(!FAutoOperation);
  4931. void * BatchStorage;
  4932. BatchStart(BatchStorage);
  4933. FAutoOperation = true;
  4934. bool AnyOperation = false;
  4935. TDateTime StartTime = Now();
  4936. TSynchronizeChecklist * AChecklist = NULL;
  4937. try
  4938. {
  4939. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  4940. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  4941. {
  4942. FSynchronizeProgressForm->Start();
  4943. }
  4944. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  4945. static_cast<TTerminal::TSynchronizeMode>(Mode),
  4946. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  4947. Options);
  4948. SAFE_DESTROY(FSynchronizeProgressForm);
  4949. AnyOperation = (AChecklist->CheckedCount > 0);
  4950. if (AnyOperation)
  4951. {
  4952. TSynchronizeProgress SynchronizeProgress(AChecklist);
  4953. CreateProgressForm(&SynchronizeProgress);
  4954. try
  4955. {
  4956. Terminal->SynchronizeApply(
  4957. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  4958. SynchronizeProcessedItem, NULL, NULL, NULL);
  4959. }
  4960. __finally
  4961. {
  4962. DestroyProgressForm();
  4963. }
  4964. }
  4965. }
  4966. __finally
  4967. {
  4968. if (Checklist == NULL)
  4969. {
  4970. delete AChecklist;
  4971. }
  4972. else
  4973. {
  4974. *Checklist = AChecklist;
  4975. }
  4976. FAutoOperation = false;
  4977. SAFE_DESTROY(FSynchronizeProgressForm);
  4978. BatchEnd(BatchStorage);
  4979. ReloadLocalDirectory();
  4980. if (AnyOperation)
  4981. {
  4982. OperationComplete(StartTime);
  4983. }
  4984. }
  4985. }
  4986. //---------------------------------------------------------------------------
  4987. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  4988. {
  4989. // can be called from command line
  4990. return Visible &&
  4991. ((DirView(osRemote)->SelCount > 0) ||
  4992. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  4993. }
  4994. //---------------------------------------------------------------------------
  4995. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  4996. {
  4997. LogSynchronizeEvent(Terminal, Message);
  4998. }
  4999. //---------------------------------------------------------------------------
  5000. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5001. int Params, TSynchronizeOptions & Options)
  5002. {
  5003. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5004. {
  5005. Options.Filter = new TStringList();
  5006. Options.Filter->CaseSensitive = false;
  5007. Options.Filter->Duplicates = Types::dupAccept;
  5008. if (DirView(osRemote)->SelCount > 0)
  5009. {
  5010. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5011. }
  5012. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5013. {
  5014. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5015. }
  5016. Options.Filter->Sort();
  5017. }
  5018. }
  5019. //---------------------------------------------------------------------------
  5020. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5021. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5022. {
  5023. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5024. UnicodeString AdditionalParams =
  5025. FORMAT(L"%s \"%s\" \"%s\" %d %d %s", (
  5026. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5027. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options,
  5028. TProgramParams::FormatSwitch(DEFAULTS_SWITCH)));
  5029. TCopyParamType Defaults;
  5030. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5031. ConfigStorage->AccessMode = smRead;
  5032. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false, false))
  5033. {
  5034. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5035. }
  5036. ConfigStorage.reset(NULL);
  5037. std::unique_ptr<TStringList> Options(new TStringList());
  5038. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5039. GUIConfiguration->SaveCopyParam(OptionsStorage.get(), CopyParams, &Defaults);
  5040. if (Options->Count > 0)
  5041. {
  5042. AdditionalParams +=
  5043. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAW_CONFIG_SWITCH), StringsToParams(Options.get())));
  5044. }
  5045. ExecuteNewInstance(SessionName, AdditionalParams);
  5046. }
  5047. //---------------------------------------------------------------------------
  5048. struct TSynchronizeParams
  5049. {
  5050. UnicodeString LocalDirectory;
  5051. UnicodeString RemoteDirectory;
  5052. TSynchronizeMode Mode;
  5053. TSynchronizeChecklist * Checklist;
  5054. int Params;
  5055. TCopyParamType * CopyParam;
  5056. TDateTime CollectElapsed;
  5057. TDateTime * StartTime;
  5058. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5059. };
  5060. //---------------------------------------------------------------------------
  5061. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5062. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5063. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5064. {
  5065. DebugAssert(!FAutoOperation);
  5066. void * BatchStorage;
  5067. BatchStart(BatchStorage);
  5068. FAutoOperation = true;
  5069. TFileOperationStatistics Statistics;
  5070. TDateTime Start = Now();
  5071. try
  5072. {
  5073. Params.OnProcessedItem = OnProcessedItem;
  5074. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5075. CreateProgressForm(&SynchronizeProgress);
  5076. Terminal->SynchronizeApply(
  5077. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5078. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5079. &Statistics);
  5080. }
  5081. __finally
  5082. {
  5083. Params.OnProcessedItem = NULL;
  5084. FAutoOperation = false;
  5085. DestroyProgressForm();
  5086. BatchEnd(BatchStorage);
  5087. ReloadLocalDirectory();
  5088. }
  5089. if (WinConfiguration->SynchronizeSummary)
  5090. {
  5091. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5092. // The statistics should be 0 anyway in this case
  5093. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5094. {
  5095. Message += L"\n";
  5096. if (Statistics.FilesUploaded > 0)
  5097. {
  5098. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5099. }
  5100. if (Statistics.FilesDownloaded > 0)
  5101. {
  5102. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5103. }
  5104. if (Statistics.FilesDeletedLocal > 0)
  5105. {
  5106. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5107. }
  5108. if (Statistics.FilesDeletedRemote > 0)
  5109. {
  5110. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5111. }
  5112. }
  5113. TDateTime Elapsed = (Now() - Start);
  5114. Message +=
  5115. L"\n" +
  5116. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5117. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5118. TMessageParams Params(mpNeverAskAgainCheck);
  5119. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5120. if (Result == qaNeverAskAgain)
  5121. {
  5122. WinConfiguration->SynchronizeSummary = false;
  5123. }
  5124. }
  5125. }
  5126. //---------------------------------------------------------------------------
  5127. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5128. {
  5129. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5130. {
  5131. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5132. }
  5133. // Not set in keep-up-to-date mode - Synchronize() method
  5134. if (Token != NULL)
  5135. {
  5136. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5137. if (Params.OnProcessedItem != NULL)
  5138. {
  5139. Params.OnProcessedItem(NULL, ChecklistItem);
  5140. }
  5141. }
  5142. }
  5143. //---------------------------------------------------------------------------
  5144. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5145. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5146. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5147. {
  5148. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5149. *Params.StartTime = Now();
  5150. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5151. }
  5152. //---------------------------------------------------------------------------
  5153. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5154. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5155. {
  5156. // terminal can be already closed (e.g. dropped connection)
  5157. if (Terminal != NULL)
  5158. {
  5159. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5160. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5161. }
  5162. }
  5163. //---------------------------------------------------------------------------
  5164. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5165. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5166. {
  5167. TAutoBatch AutoBatch(this);
  5168. TAutoFlag AutoOperationFlag(FAutoOperation);
  5169. if (Side == osRemote)
  5170. {
  5171. std::unique_ptr<TStrings> FileList(new TStringList());
  5172. FileList->AddObject(FileName, RemoteFile);
  5173. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5174. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5175. RemoteDirView->SaveSelection();
  5176. RemoteDirView->SaveSelectedNames();
  5177. try
  5178. {
  5179. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5180. }
  5181. catch(...)
  5182. {
  5183. RemoteDirView->DiscardSavedSelection();
  5184. throw;
  5185. }
  5186. RemoteDirView->RestoreSelection();
  5187. }
  5188. else if (DebugAlwaysTrue(Side == osLocal))
  5189. {
  5190. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5191. {
  5192. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5193. }
  5194. UnicodeString Directory = ExtractFileDir(FileName);
  5195. ReloadLocalDirectory(Directory);
  5196. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5197. if (!SamePaths(Directory, NewDirectory))
  5198. {
  5199. ReloadLocalDirectory(NewDirectory);
  5200. }
  5201. }
  5202. }
  5203. //---------------------------------------------------------------------------
  5204. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5205. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5206. TSynchronizeMode & Mode, bool & SaveMode, bool UseDefaults)
  5207. {
  5208. int Result;
  5209. int Params = GUIConfiguration->SynchronizeParams;
  5210. bool SaveSettings = false;
  5211. int Options =
  5212. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5213. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5214. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5215. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5216. bool Continue =
  5217. UseDefaults ||
  5218. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5219. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs);
  5220. if (Continue)
  5221. {
  5222. Configuration->Usage->Inc(L"Synchronizations");
  5223. UpdateCopyParamCounters(CopyParam);
  5224. TSynchronizeOptions SynchronizeOptions;
  5225. GetSynchronizeOptions(Params, SynchronizeOptions);
  5226. if (SaveSettings)
  5227. {
  5228. GUIConfiguration->SynchronizeParams = Params;
  5229. }
  5230. else
  5231. {
  5232. SaveMode = false;
  5233. }
  5234. TDateTime StartTime = Now();
  5235. TSynchronizeChecklist * Checklist = NULL;
  5236. try
  5237. {
  5238. DebugAssert(!FAutoOperation);
  5239. FAutoOperation = true;
  5240. try
  5241. {
  5242. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  5243. FSynchronizeProgressForm->Start();
  5244. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5245. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5246. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5247. &SynchronizeOptions);
  5248. }
  5249. __finally
  5250. {
  5251. FAutoOperation = false;
  5252. SAFE_DESTROY(FSynchronizeProgressForm);
  5253. }
  5254. TSynchronizeParams SynchronizeParams;
  5255. SynchronizeParams.LocalDirectory = LocalDirectory;
  5256. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5257. SynchronizeParams.Mode = Mode;
  5258. SynchronizeParams.CopyParam = &CopyParam;
  5259. SynchronizeParams.Params = Params;
  5260. SynchronizeParams.Checklist = Checklist;
  5261. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5262. SynchronizeParams.StartTime = &StartTime;
  5263. SynchronizeParams.OnProcessedItem = NULL;
  5264. Result = Checklist->Count;
  5265. if (Checklist->Count > 0)
  5266. {
  5267. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5268. {
  5269. if (!DoSynchronizeChecklistDialog(
  5270. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5271. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, &SynchronizeParams))
  5272. {
  5273. Result = -1;
  5274. }
  5275. }
  5276. else
  5277. {
  5278. FullSynchronize(SynchronizeParams, NULL, NULL);
  5279. }
  5280. }
  5281. else
  5282. {
  5283. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5284. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5285. }
  5286. }
  5287. __finally
  5288. {
  5289. delete Checklist;
  5290. }
  5291. OperationComplete(StartTime);
  5292. }
  5293. else
  5294. {
  5295. Result = -1;
  5296. }
  5297. return Result;
  5298. }
  5299. //---------------------------------------------------------------------------
  5300. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5301. const UnicodeString LocalDirectory, const UnicodeString RemoteDirectory,
  5302. bool & Continue, bool Collect)
  5303. {
  5304. if (Collect)
  5305. {
  5306. DebugAssert(FSynchronizeProgressForm != NULL);
  5307. if (!FSynchronizeProgressForm->Started)
  5308. {
  5309. FSynchronizeProgressForm->Start();
  5310. }
  5311. FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Continue);
  5312. }
  5313. else
  5314. {
  5315. DebugAssert(FSynchronizeProgressForm == NULL);
  5316. DebugAssert(FProgressForm != NULL);
  5317. }
  5318. }
  5319. //---------------------------------------------------------------------------
  5320. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5321. {
  5322. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  5323. TRemoteFile * File = NULL;
  5324. Terminal->ReadFile(FullFileName, File);
  5325. if (File != NULL)
  5326. {
  5327. std::unique_ptr<TRemoteFile> FileOwner(File);
  5328. TAutoFlag Flag(FStandaloneEditing);
  5329. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5330. Application->ShowMainForm = false;
  5331. Application->Run();
  5332. }
  5333. }
  5334. //---------------------------------------------------------------------------
  5335. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  5336. {
  5337. DebugFail();
  5338. }
  5339. //---------------------------------------------------------------------------
  5340. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5341. {
  5342. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5343. SessionData->Assign(Terminal->SessionData);
  5344. UpdateSessionData(SessionData.get());
  5345. TTerminalManager::Instance()->UpdateSessionCredentials(SessionData.get());
  5346. if (!Terminal->SessionData->HasSessionName())
  5347. {
  5348. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5349. // once IsWorkspace is cleared
  5350. SessionData->Name = UnicodeString();
  5351. SessionData->IsWorkspace = false;
  5352. }
  5353. return SessionData.release();
  5354. }
  5355. //---------------------------------------------------------------------------
  5356. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5357. {
  5358. TSessionData * SessionData = CloneCurrentSessionData();
  5359. try
  5360. {
  5361. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5362. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5363. }
  5364. __finally
  5365. {
  5366. delete SessionData;
  5367. }
  5368. }
  5369. //---------------------------------------------------------------------------
  5370. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5371. {
  5372. TTerminalManager * Manager = TTerminalManager::Instance();
  5373. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5374. if (DebugAlwaysTrue(Terminal != NULL))
  5375. {
  5376. // Update (Managed)Terminal->StateData
  5377. UpdateTerminal(Terminal);
  5378. }
  5379. Manager->SaveWorkspace(DataList.get());
  5380. return DataList.release();
  5381. }
  5382. //---------------------------------------------------------------------------
  5383. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5384. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5385. {
  5386. WinConfiguration->LastWorkspace = Name;
  5387. if (!SavePasswords)
  5388. {
  5389. for (int Index = 0; Index < DataList->Count; Index++)
  5390. {
  5391. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5392. if (SessionData->Link.IsEmpty())
  5393. {
  5394. SessionData->ClearSessionPasswords();
  5395. }
  5396. }
  5397. }
  5398. StoredSessions->NewWorkspace(Name, DataList);
  5399. // modified only
  5400. StoredSessions->Save(false, Explicit);
  5401. }
  5402. //---------------------------------------------------------------------------
  5403. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5404. {
  5405. return
  5406. DefaultStr(WinConfiguration->LastWorkspace,
  5407. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5408. }
  5409. //---------------------------------------------------------------------------
  5410. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5411. {
  5412. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5413. bool AnyNonStoredSessionWithPassword = false;
  5414. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5415. bool AllNonStoredSessionsAnonymous = true;
  5416. TTerminalManager * Manager = TTerminalManager::Instance();
  5417. for (int Index = 0; Index < DataList->Count; Index++)
  5418. {
  5419. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5420. if (SessionData->Link.IsEmpty())
  5421. {
  5422. if (SessionData->HasAnySessionPassword())
  5423. {
  5424. AnyNonStoredSessionWithPassword = true;
  5425. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  5426. {
  5427. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5428. }
  5429. }
  5430. if (!SameText(SessionData->UserName, AnonymousUserName))
  5431. {
  5432. AllNonStoredSessionsAnonymous = false;
  5433. }
  5434. }
  5435. }
  5436. bool SavePasswords;
  5437. bool * PSavePasswords;
  5438. bool NotRecommendedSavingPasswords =
  5439. !CustomWinConfiguration->UseMasterPassword &&
  5440. !AllNonStoredSessionsAnonymous;
  5441. if (Configuration->DisablePasswordStoring ||
  5442. !AnyNonStoredSessionWithPassword)
  5443. {
  5444. PSavePasswords = NULL;
  5445. SavePasswords = false;
  5446. }
  5447. else
  5448. {
  5449. PSavePasswords = &SavePasswords;
  5450. SavePasswords =
  5451. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5452. !NotRecommendedSavingPasswords;
  5453. }
  5454. UnicodeString Name = WorkspaceName();
  5455. bool CreateShortcut = false;
  5456. bool Result =
  5457. DoSaveWorkspaceDialog(
  5458. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5459. EnableAutoSave);
  5460. if (Result)
  5461. {
  5462. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5463. if (CreateShortcut)
  5464. {
  5465. TOperationVisualizer Visualizer;
  5466. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5467. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5468. }
  5469. if (EnableAutoSave)
  5470. {
  5471. WinConfiguration->AutoSaveWorkspace = true;
  5472. WinConfiguration->AutoWorkspace = Name;
  5473. if (PSavePasswords != NULL)
  5474. {
  5475. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5476. }
  5477. }
  5478. }
  5479. return Result;
  5480. }
  5481. //---------------------------------------------------------------------------
  5482. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TTerminal * Terminal)
  5483. {
  5484. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  5485. DebugAssert(ManagedTerminal != NULL);
  5486. SAFE_DESTROY(ManagedTerminal->RemoteExplorerState);
  5487. if (WinConfiguration->PreservePanelState)
  5488. {
  5489. ManagedTerminal->RemoteExplorerState = RemoteDirView->SaveState();
  5490. }
  5491. UpdateSessionData(ManagedTerminal->StateData);
  5492. }
  5493. //---------------------------------------------------------------------------
  5494. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5495. {
  5496. // Keep in sync with TSessionData::CopyStateData
  5497. DebugAssert(Data != NULL);
  5498. // This is inconsistent with how color (for example) is handled.
  5499. Data->Name = Terminal->SessionData->Name;
  5500. // cannot use RemoteDirView->Path, because it is empty if connection
  5501. // was already closed
  5502. // also only peek, we may not be connected at all atm
  5503. // so make sure we do not try retrieving current directory from the server
  5504. // (particularly with FTP)
  5505. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5506. if (!ACurrentDirectory.IsEmpty())
  5507. {
  5508. Data->RemoteDirectory = ACurrentDirectory;
  5509. }
  5510. Data->Color = SessionColor;
  5511. }
  5512. //---------------------------------------------------------------------------
  5513. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5514. {
  5515. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5516. DebugAssert(Toolbar != NULL);
  5517. for (int i = 0; i < Toolbar->Items->Count; i++)
  5518. {
  5519. TTBXCustomDropDownItem * DropDownItem;
  5520. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5521. if (DropDownItem != NULL)
  5522. {
  5523. ToolbarItemResize(DropDownItem,
  5524. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5525. Toolbar->NonClientWidth);
  5526. break;
  5527. }
  5528. }
  5529. }
  5530. //---------------------------------------------------------------------------
  5531. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5532. {
  5533. Item->EditWidth = Width;
  5534. }
  5535. //---------------------------------------------------------------------------
  5536. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5537. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5538. {
  5539. for (int i = 0; i < Toolbar->Items->Count; i++)
  5540. {
  5541. TTBXCustomDropDownItem * DropDownItem;
  5542. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5543. if (DropDownItem != NULL)
  5544. {
  5545. ASize.x -= DropDownItem->EditWidth;
  5546. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5547. }
  5548. }
  5549. }
  5550. //---------------------------------------------------------------------------
  5551. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5552. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5553. {
  5554. if (WinConfiguration->DDWarnLackOfTempSpace)
  5555. {
  5556. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5557. if (!ADrive.IsEmpty())
  5558. {
  5559. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5560. DebugAssert(RequiredSpace >= 0);
  5561. __int64 RequiredWithReserve;
  5562. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5563. if (FreeSpace < RequiredWithReserve)
  5564. {
  5565. unsigned int Result;
  5566. TMessageParams Params(mpNeverAskAgainCheck);
  5567. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5568. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5569. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5570. if (Result == qaNeverAskAgain)
  5571. {
  5572. WinConfiguration->DDWarnLackOfTempSpace = false;
  5573. }
  5574. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5575. }
  5576. }
  5577. }
  5578. }
  5579. //---------------------------------------------------------------------------
  5580. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5581. {
  5582. DoOpenDirectoryDialog(odAddBookmark, Side);
  5583. }
  5584. //---------------------------------------------------------------------------
  5585. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5586. {
  5587. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5588. TStringList * VisitedDirectories = CreateSortedStringList((Side == osRemote));
  5589. try
  5590. {
  5591. TCustomDirView * DView = DirView(Side);
  5592. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5593. {
  5594. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5595. }
  5596. }
  5597. catch (...)
  5598. {
  5599. delete VisitedDirectories;
  5600. throw;
  5601. }
  5602. return VisitedDirectories;
  5603. }
  5604. //---------------------------------------------------------------------------
  5605. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5606. TOpenDirectoryMode Mode, TOperationSide Side)
  5607. {
  5608. bool Continue = true;
  5609. if (Mode == odAddBookmark)
  5610. {
  5611. TMessageParams Params(mpNeverAskAgainCheck);
  5612. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5613. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5614. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  5615. unsigned int Answer =
  5616. MessageDialog(Message,
  5617. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  5618. if (Answer == qaNeverAskAgain)
  5619. {
  5620. Continue = true;
  5621. WinConfiguration->UseSharedBookmarks = true;
  5622. }
  5623. else if (Answer == qaOK)
  5624. {
  5625. Continue = true;
  5626. WinConfiguration->UseSharedBookmarks = false;
  5627. }
  5628. else
  5629. {
  5630. Continue = false;
  5631. }
  5632. }
  5633. if (Continue)
  5634. {
  5635. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  5636. try
  5637. {
  5638. UnicodeString Name = DirView(Side)->PathName;
  5639. bool Repeat;
  5640. do
  5641. {
  5642. Repeat = false;
  5643. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  5644. // do not allow switching to location profiles,
  5645. // if we are not connected
  5646. HasDirView[osLocal] && (Terminal != NULL)))
  5647. {
  5648. TWindowLock Lock(this);
  5649. if (!TryOpenDirectory(Side, Name))
  5650. {
  5651. Repeat = true;
  5652. Mode = odBrowse;
  5653. }
  5654. }
  5655. }
  5656. while (Repeat);
  5657. }
  5658. __finally
  5659. {
  5660. delete VisitedDirectories;
  5661. }
  5662. }
  5663. }
  5664. //---------------------------------------------------------------------------
  5665. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  5666. {
  5667. DebugAssert(!FTerminal->CommandSessionOpened);
  5668. return TTerminalManager::Instance()->ConnectTerminal(FTerminal->CommandSession);
  5669. }
  5670. //---------------------------------------------------------------------------
  5671. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  5672. {
  5673. bool Result = FTerminal->IsCapable[Capability] ||
  5674. FTerminal->CommandSessionOpened;
  5675. if (!Result)
  5676. {
  5677. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  5678. if (!GUIConfiguration->ConfirmCommandSession)
  5679. {
  5680. Result = true;
  5681. }
  5682. else
  5683. {
  5684. TMessageParams Params(mpNeverAskAgainCheck);
  5685. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  5686. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  5687. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  5688. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  5689. if (Answer == qaNeverAskAgain)
  5690. {
  5691. GUIConfiguration->ConfirmCommandSession = false;
  5692. Result = true;
  5693. }
  5694. else if (Answer == qaOK)
  5695. {
  5696. Result = true;
  5697. }
  5698. }
  5699. if (Result)
  5700. {
  5701. Result = CommandSessionFallback();
  5702. }
  5703. }
  5704. return Result;
  5705. }
  5706. //---------------------------------------------------------------------------
  5707. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  5708. {
  5709. if (EnsureCommandSessionFallback(fcAnyCommand))
  5710. {
  5711. DoConsoleDialog(Terminal, Command);
  5712. }
  5713. }
  5714. //---------------------------------------------------------------------------
  5715. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  5716. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  5717. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  5718. {
  5719. if (IsFileControl(DropSourceControl, osRemote) &&
  5720. (FDDExtMapFile != NULL))
  5721. {
  5722. Accept = true;
  5723. }
  5724. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  5725. DebugAssert(FDDTargetControl != NULL);
  5726. }
  5727. //---------------------------------------------------------------------------
  5728. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  5729. _di_IDataObject DataObj, int KeyState,
  5730. const TPoint & Point, int & Effect, bool & Accept)
  5731. {
  5732. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  5733. }
  5734. //---------------------------------------------------------------------------
  5735. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  5736. _di_IDataObject DataObj, int KeyState,
  5737. const TPoint & Point, int & Effect, bool & Accept)
  5738. {
  5739. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  5740. }
  5741. //---------------------------------------------------------------------------
  5742. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  5743. TObject * Sender)
  5744. {
  5745. DebugUsedParam(Sender);
  5746. DebugAssert(FDDTargetControl == Sender);
  5747. FDDTargetControl = NULL;
  5748. }
  5749. //---------------------------------------------------------------------------
  5750. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  5751. {
  5752. FileControlDDDragLeave(SessionsPageControl);
  5753. }
  5754. //---------------------------------------------------------------------------
  5755. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  5756. {
  5757. FileControlDDDragLeave(QueueView3);
  5758. }
  5759. //---------------------------------------------------------------------------
  5760. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  5761. {
  5762. DebugAssert(GetSide(Side) == osRemote);
  5763. DebugUsedParam(Side);
  5764. bool Edit = false;
  5765. TRemoteFile * File = NULL;
  5766. UnicodeString FileName;
  5767. UnicodeString PointTo;
  5768. bool SymbolicLink = true;
  5769. if (RemoteDirView->ItemFocused)
  5770. {
  5771. DebugAssert(RemoteDirView->ItemFocused->Data);
  5772. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  5773. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  5774. if (Edit)
  5775. {
  5776. FileName = File->FileName;
  5777. PointTo = File->LinkTo;
  5778. }
  5779. else
  5780. {
  5781. PointTo = File->FileName;
  5782. }
  5783. }
  5784. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  5785. Terminal->IsCapable[fcHardLink]))
  5786. {
  5787. Configuration->Usage->Inc(L"RemoteLinksCreated");
  5788. if (Edit)
  5789. {
  5790. DebugAssert(File->FileName == FileName);
  5791. int Params = dfNoRecursive;
  5792. Terminal->ExceptionOnFail = true;
  5793. try
  5794. {
  5795. Terminal->DeleteFile(L"", File, &Params);
  5796. }
  5797. __finally
  5798. {
  5799. Terminal->ExceptionOnFail = false;
  5800. }
  5801. }
  5802. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  5803. }
  5804. }
  5805. //---------------------------------------------------------------------------
  5806. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  5807. {
  5808. return
  5809. (Terminal != NULL) &&
  5810. ((GetSide(Side) != osRemote) ||
  5811. (Terminal->ResolvingSymlinks &&
  5812. Terminal->IsCapable[fcSymbolicLink]));
  5813. }
  5814. //---------------------------------------------------------------------------
  5815. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  5816. {
  5817. return
  5818. (FCurrentSide == osRemote) &&
  5819. (RemoteDirView->ItemFocused != NULL) &&
  5820. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  5821. Terminal->SessionData->ResolveSymlinks;
  5822. }
  5823. //---------------------------------------------------------------------------
  5824. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  5825. {
  5826. DebugAssert(!WinConfiguration->DisableOpenEdit);
  5827. FForceExecution = true;
  5828. try
  5829. {
  5830. DirView(osCurrent)->ExecuteCurrentFile();
  5831. }
  5832. __finally
  5833. {
  5834. FForceExecution = false;
  5835. }
  5836. }
  5837. //---------------------------------------------------------------------------
  5838. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  5839. {
  5840. TEditorData ExternalEditor;
  5841. ExternalEditor.Editor = edExternal;
  5842. bool Remember = false;
  5843. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc,
  5844. (GetSide(osCurrent) == osRemote)))
  5845. {
  5846. if (Remember)
  5847. {
  5848. TEditorList * EditorList = new TEditorList;
  5849. try
  5850. {
  5851. *EditorList = *WinConfiguration->EditorList;
  5852. bool Found = false;
  5853. int i = 0;
  5854. while (!Found && (i < EditorList->Count))
  5855. {
  5856. const TEditorPreferences * Editor = EditorList->Editors[i];
  5857. if ((Editor->Data->Editor == edExternal) &&
  5858. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  5859. {
  5860. Found = true;
  5861. }
  5862. i++;
  5863. }
  5864. if (!Found)
  5865. {
  5866. EditorList->Add(new TEditorPreferences(ExternalEditor));
  5867. WinConfiguration->EditorList = EditorList;
  5868. }
  5869. }
  5870. __finally
  5871. {
  5872. delete EditorList;
  5873. }
  5874. }
  5875. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  5876. }
  5877. }
  5878. //---------------------------------------------------------------------------
  5879. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  5880. {
  5881. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  5882. if (FFileFindTerminal == Sender)
  5883. {
  5884. FFileFindTerminal = NULL;
  5885. HideFileFindDialog();
  5886. }
  5887. if (FClipboardTerminal == Sender)
  5888. {
  5889. ClipboardClear(); // implies ClipboardStop
  5890. }
  5891. }
  5892. //---------------------------------------------------------------------------
  5893. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  5894. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  5895. {
  5896. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  5897. DebugAssert(Terminal != NULL);
  5898. if (Data->Terminal == Terminal)
  5899. {
  5900. Data->Terminal = NULL;
  5901. }
  5902. }
  5903. //---------------------------------------------------------------------------
  5904. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  5905. {
  5906. UpdateControls();
  5907. SessionColor = TColor(0);
  5908. UpdateRemotePathComboBox(false);
  5909. NeedSession(true);
  5910. }
  5911. //---------------------------------------------------------------------------
  5912. void __fastcall TCustomScpExplorerForm::NeedSession(bool ReloadSessions)
  5913. {
  5914. try
  5915. {
  5916. try
  5917. {
  5918. TTerminalManager::Instance()->NewSession(false, L"", ReloadSessions, this);
  5919. }
  5920. __finally
  5921. {
  5922. if (!WinConfiguration->KeepOpenWhenNoSession &&
  5923. (!Terminal || !Terminal->Active))
  5924. {
  5925. TerminateApplication();
  5926. }
  5927. }
  5928. }
  5929. catch (EAbort &)
  5930. {
  5931. // swallow
  5932. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  5933. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  5934. }
  5935. }
  5936. //---------------------------------------------------------------------------
  5937. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged()
  5938. {
  5939. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  5940. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  5941. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  5942. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  5943. try
  5944. {
  5945. while ((SessionsPageControl->PageCount > TerminalList->Count + 1) ||
  5946. // Clear the design time unthemed tab
  5947. ((SessionsPageControl->PageCount > 0) &&
  5948. (dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[SessionsPageControl->PageCount - 1]) == NULL)))
  5949. {
  5950. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  5951. }
  5952. for (int Index = 0; Index <= TerminalList->Count; Index++)
  5953. {
  5954. TThemeTabSheet * TabSheet;
  5955. if (Index >= SessionsPageControl->PageCount)
  5956. {
  5957. TabSheet = new TThemeTabSheet(SessionsPageControl);
  5958. TabSheet->PageControl = SessionsPageControl;
  5959. }
  5960. else
  5961. {
  5962. TabSheet = DebugNotNull(dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[Index]));
  5963. }
  5964. bool IsSessionTab = (Index < TerminalList->Count);
  5965. if (IsSessionTab)
  5966. {
  5967. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  5968. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  5969. UpdateSessionTab(TabSheet);
  5970. }
  5971. else
  5972. {
  5973. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  5974. TabSheet->Tag = 0; // not really needed
  5975. // We know that we are at the last page, sotherwise we could not call this (it assumes that new session tab is the last one)
  5976. UpdateNewSessionTab();
  5977. }
  5978. TabSheet->ShowCloseButton = IsSessionTab;
  5979. }
  5980. }
  5981. __finally
  5982. {
  5983. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  5984. }
  5985. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  5986. }
  5987. //---------------------------------------------------------------------------
  5988. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  5989. {
  5990. DoTerminalListChanged();
  5991. }
  5992. //---------------------------------------------------------------------------
  5993. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  5994. {
  5995. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  5996. DebugAssert(TabSheet->ImageIndex == 0);
  5997. TabSheet->Caption =
  5998. WinConfiguration->SelectiveToolbarText ?
  5999. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  6000. UnicodeString();
  6001. }
  6002. //---------------------------------------------------------------------------
  6003. TTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  6004. {
  6005. return reinterpret_cast<TTerminal *>(TabSheet->Tag);
  6006. }
  6007. //---------------------------------------------------------------------------
  6008. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  6009. {
  6010. if (DebugAlwaysTrue(TabSheet != NULL))
  6011. {
  6012. TManagedTerminal * ManagedTerminal =
  6013. dynamic_cast<TManagedTerminal *>(GetSessionTabTerminal(TabSheet));
  6014. if (DebugAlwaysTrue(ManagedTerminal != NULL))
  6015. {
  6016. TColor Color = (ManagedTerminal == FTerminal) ? FSessionColor : ManagedTerminal->StateData->Color;
  6017. TabSheet->ImageIndex = AddSessionColor(Color);
  6018. UnicodeString TabCaption = TTerminalManager::Instance()->GetTerminalTitle(ManagedTerminal, true);
  6019. TabSheet->Caption = SessionsPageControl->FormatCaptionWithCloseButton(TabCaption);
  6020. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  6021. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  6022. {
  6023. ThemeTabSheet->Shadowed = !ManagedTerminal->Active;
  6024. }
  6025. }
  6026. }
  6027. }
  6028. //---------------------------------------------------------------------------
  6029. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6030. {
  6031. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6032. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  6033. bool Result = (Terminal != NULL);
  6034. if (Result)
  6035. {
  6036. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6037. }
  6038. else
  6039. {
  6040. try
  6041. {
  6042. NewSession(false);
  6043. }
  6044. __finally
  6045. {
  6046. DoTerminalListChanged();
  6047. }
  6048. FSessionsPageControlNewSessionTime = Now();
  6049. }
  6050. return Result;
  6051. }
  6052. //---------------------------------------------------------------------------
  6053. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6054. {
  6055. SessionTabSwitched();
  6056. }
  6057. //---------------------------------------------------------------------------
  6058. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6059. {
  6060. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6061. DebugAssert(TransferList != NULL);
  6062. UnicodeString Name;
  6063. if (TransferList->ItemIndex <= 0)
  6064. {
  6065. Name = L"";
  6066. }
  6067. else
  6068. {
  6069. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6070. }
  6071. if (FCopyParamAutoSelected.IsEmpty())
  6072. {
  6073. // if previous preset was not autoselected, make new preset the "default"
  6074. FCopyParamDefault = Name;
  6075. }
  6076. GUIConfiguration->CopyParamCurrent = Name;
  6077. }
  6078. //---------------------------------------------------------------------------
  6079. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6080. {
  6081. // sanity check
  6082. bool ExistingPreset =
  6083. (FTransferListHoverIndex >= 0) &&
  6084. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6085. DebugAssert(ExistingPreset);
  6086. if (ExistingPreset)
  6087. {
  6088. HDC DC = GetDC(0);
  6089. TCanvas * Canvas = new TCanvas();
  6090. try
  6091. {
  6092. Canvas->Handle = DC;
  6093. Canvas->Font = GetToolbarFont(this);
  6094. UnicodeString Name;
  6095. if (FTransferListHoverIndex == 0)
  6096. {
  6097. Name = L"";
  6098. }
  6099. else
  6100. {
  6101. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6102. }
  6103. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6104. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6105. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6106. static_cast<TObject*>(GetComponent(fcTransferList)));
  6107. UnicodeString InfoStr =
  6108. GUIConfiguration->CopyParamPreset[Name].
  6109. GetInfoStr(L"; ",
  6110. FLAGMASK(Terminal != NULL, Terminal->UsableCopyParamAttrs(0).General));
  6111. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6112. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6113. {
  6114. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6115. {
  6116. InfoStr.SetLength(InfoStr.Length() - 1);
  6117. }
  6118. InfoStr += Ellipsis;
  6119. }
  6120. // UpdateCaption does not cause invalidation of whole submenu, while
  6121. // setting Caption property does.
  6122. // also it probably does not resize the label, even if necessary
  6123. // (we do not want that anyway)
  6124. TransferLabel->UpdateCaption(InfoStr);
  6125. }
  6126. __finally
  6127. {
  6128. Canvas->Handle = NULL;
  6129. ReleaseDC(0, DC);
  6130. delete Canvas;
  6131. }
  6132. }
  6133. }
  6134. //---------------------------------------------------------------------------
  6135. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6136. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6137. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6138. {
  6139. if (FTransferListHoverIndex != AHoverIndex)
  6140. {
  6141. FTransferListHoverIndex = AHoverIndex;
  6142. UpdateTransferLabel();
  6143. }
  6144. }
  6145. //---------------------------------------------------------------------------
  6146. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6147. {
  6148. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6149. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6150. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6151. {
  6152. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6153. {
  6154. OpenDirectory(GetSide(osCurrent));
  6155. Message.Result = 1;
  6156. }
  6157. else
  6158. {
  6159. TForm::Dispatch(&Message);
  6160. }
  6161. }
  6162. else
  6163. {
  6164. TForm::Dispatch(&Message);
  6165. }
  6166. }
  6167. //---------------------------------------------------------------------------
  6168. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6169. {
  6170. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6171. if ((FIgnoreNextDialogChar != 0) &&
  6172. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6173. {
  6174. Message.Result = 1;
  6175. }
  6176. else
  6177. {
  6178. TForm::Dispatch(&Message);
  6179. }
  6180. FIgnoreNextDialogChar = 0;
  6181. }
  6182. //---------------------------------------------------------------------------
  6183. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6184. {
  6185. // The four low-order bits are used internally by Windows
  6186. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6187. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6188. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6189. // 61730 - restore thru double click - undocumented
  6190. // 61490 - maximize thru double click - undocumented
  6191. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6192. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6193. {
  6194. SysResizing(Cmd);
  6195. }
  6196. TForm::Dispatch(&Message);
  6197. }
  6198. //---------------------------------------------------------------------------
  6199. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6200. {
  6201. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6202. // Also there no point returning TRUE as we are not able to
  6203. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6204. // Hence the process termination might be safer :)
  6205. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6206. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6207. {
  6208. Message.Result = FALSE;
  6209. }
  6210. else
  6211. {
  6212. Message.Result = TRUE;
  6213. }
  6214. // Do not call default handling as that triggers OnCloseQuery,
  6215. // where our implementation will popup confirmation dialogs, what we do not want,
  6216. // as per Vista guidelines:
  6217. // https://docs.microsoft.com/en-us/windows/desktop/shutdown/shutting-down
  6218. }
  6219. //---------------------------------------------------------------------------
  6220. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6221. {
  6222. if (Message.EndSession && IsApplicationMinimized())
  6223. {
  6224. // WORKAROUND
  6225. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6226. // when it receives WM_ENDSESSION,
  6227. // but that sometimes (particularly when application is minimized) cause crashes.
  6228. // Crash popups message that blocks log off.
  6229. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6230. // so we call ExitProcess() immediately, not even trying to cleanup.
  6231. // It still causes beep, so there's likely some popup, but it does not block
  6232. // log off.
  6233. ExitProcess(0);
  6234. }
  6235. TForm::Dispatch(&Message);
  6236. }
  6237. #ifdef _DEBUG
  6238. //---------------------------------------------------------------------------
  6239. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6240. {
  6241. TForm::Dispatch(&Message);
  6242. }
  6243. #endif
  6244. //---------------------------------------------------------------------------
  6245. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6246. {
  6247. }
  6248. //---------------------------------------------------------------------------
  6249. void __fastcall TCustomScpExplorerForm::DoShow()
  6250. {
  6251. // only now are the controls resized finally, so the size constraints
  6252. // will not conflict with possibly very small window size
  6253. RestoreFormParams();
  6254. FixControlsPlacement();
  6255. if (Position == poDefaultPosOnly)
  6256. {
  6257. CutFormToDesktop(this);
  6258. }
  6259. TForm::DoShow();
  6260. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6261. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6262. if (Terminal == NULL)
  6263. {
  6264. StartingDisconnected();
  6265. }
  6266. UpdatePixelsPerInchMainWindowCounter();
  6267. FShowing = true;
  6268. }
  6269. //---------------------------------------------------------------------------
  6270. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6271. {
  6272. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6273. }
  6274. //---------------------------------------------------------------------------
  6275. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  6276. {
  6277. DoTerminalListChanged();
  6278. }
  6279. //---------------------------------------------------------------------------
  6280. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6281. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6282. TNotifyEvent OnBalloonClick, TObject * UserData)
  6283. {
  6284. bool Do;
  6285. UnicodeString Message;
  6286. if (E == NULL)
  6287. {
  6288. Message = Str;
  6289. Do = true;
  6290. }
  6291. else
  6292. {
  6293. Do = ExceptionMessage(E, Message);
  6294. }
  6295. if (Do && WinConfiguration->BalloonNotifications)
  6296. {
  6297. UnicodeString Title;
  6298. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6299. {
  6300. Message = TrimLeft(Message);
  6301. }
  6302. else
  6303. {
  6304. Message = UnformatMessage(Message);
  6305. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6306. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6307. Title = LoadResourceString(Captions[Type]);
  6308. if (Terminal != NULL)
  6309. {
  6310. Title = FORMAT(L"%s - %s",
  6311. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), Title));
  6312. }
  6313. }
  6314. if (Seconds == 0)
  6315. {
  6316. Seconds = WinConfiguration->NotificationsTimeout;
  6317. }
  6318. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6319. }
  6320. }
  6321. //---------------------------------------------------------------------------
  6322. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6323. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6324. UnicodeString HelpKeyword, const TMessageParams * Params,
  6325. TTerminal * Terminal)
  6326. {
  6327. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6328. (FOnFeedSynchronizeError != NULL)) &&
  6329. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6330. {
  6331. if (FOnFeedSynchronizeError != NULL)
  6332. {
  6333. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6334. }
  6335. else
  6336. {
  6337. DebugAssert(FErrorList != NULL);
  6338. TStringList * MoreMessagesCopy = NULL;
  6339. if (MoreMessages)
  6340. {
  6341. MoreMessagesCopy = new TStringList();
  6342. MoreMessagesCopy->Assign(MoreMessages);
  6343. }
  6344. FErrorList->AddObject(Message, MoreMessagesCopy);
  6345. }
  6346. PopupTrayBalloon(Terminal, Message, Type);
  6347. return ContinueAnswer(Answers);
  6348. }
  6349. else
  6350. {
  6351. bool UseBalloon = FTrayIcon->Visible;
  6352. if (UseBalloon)
  6353. {
  6354. PopupTrayBalloon(Terminal, Message, Type);
  6355. }
  6356. unsigned int Result;
  6357. try
  6358. {
  6359. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6360. }
  6361. __finally
  6362. {
  6363. // cancel only balloon we popped up, otherwise we may cancel notification
  6364. // balloon that was there before the message dialog
  6365. // (such as "dd confirmation opt in balloon)
  6366. if (UseBalloon)
  6367. {
  6368. FTrayIcon->CancelBalloon();
  6369. }
  6370. }
  6371. return Result;
  6372. }
  6373. }
  6374. //---------------------------------------------------------------------------
  6375. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6376. TTerminal * Terminal, Exception * E)
  6377. {
  6378. if (FTrayIcon->Visible)
  6379. {
  6380. PopupTrayBalloon(Terminal, L"", qtError, E);
  6381. }
  6382. // particularly prevent opening new session from jump list,
  6383. // while exception is shown
  6384. NonVisualDataModule->StartBusy();
  6385. try
  6386. {
  6387. ShowExtendedExceptionEx(Terminal, E);
  6388. }
  6389. __finally
  6390. {
  6391. NonVisualDataModule->EndBusy();
  6392. FTrayIcon->CancelBalloon();
  6393. }
  6394. }
  6395. //---------------------------------------------------------------------------
  6396. void __fastcall TCustomScpExplorerForm::TerminalReady()
  6397. {
  6398. // cannot rely on active page being page for active terminal,
  6399. // as it can happen that active page is the "new session" page
  6400. // (e.g. when reconnecting active terminal, while login dialog
  6401. // invoked from "new session" page is modal)
  6402. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6403. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  6404. }
  6405. //---------------------------------------------------------------------------
  6406. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6407. TTerminal * Terminal, Exception * E)
  6408. {
  6409. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6410. if (!Terminal->Active)
  6411. {
  6412. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6413. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6414. {
  6415. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6416. }
  6417. }
  6418. }
  6419. //---------------------------------------------------------------------------
  6420. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6421. UnicodeString Message, TQueryType Type,
  6422. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6423. {
  6424. if ((E == NULL) ||
  6425. ExceptionMessage(E, Message))
  6426. {
  6427. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6428. if (Important)
  6429. {
  6430. Seconds *= 5;
  6431. }
  6432. UnicodeString NoteMessage(UnformatMessage(Message));
  6433. if (Terminal != NULL)
  6434. {
  6435. NoteMessage = FORMAT(L"%s: %s",
  6436. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), NoteMessage));
  6437. }
  6438. if (WinConfiguration->BalloonNotifications)
  6439. {
  6440. AddNote(NoteMessage);
  6441. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6442. }
  6443. else
  6444. {
  6445. FlashOnBackground();
  6446. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6447. }
  6448. }
  6449. }
  6450. //---------------------------------------------------------------------------
  6451. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6452. {
  6453. RestoreApp();
  6454. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6455. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6456. !NonVisualDataModule->Busy)
  6457. {
  6458. TTerminal * Terminal = TerminalNoteData->Terminal;
  6459. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6460. if (!ComponentVisible[fcQueueView])
  6461. {
  6462. ToggleQueueVisibility();
  6463. GoToQueue();
  6464. }
  6465. }
  6466. }
  6467. //---------------------------------------------------------------------------
  6468. void __fastcall TCustomScpExplorerForm::QueueEvent(TTerminal * ATerminal,
  6469. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6470. {
  6471. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  6472. UnicodeString Message;
  6473. TNotifyEvent OnClick = NULL;
  6474. TObject * UserData = NULL;
  6475. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6476. switch (Event)
  6477. {
  6478. case qeEmptyButMonitored:
  6479. if ((ATerminal != Terminal) || QueueInvisible)
  6480. {
  6481. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6482. OnClick = QueueEmptyNoteClicked;
  6483. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6484. TerminalNoteData->Terminal = ATerminal;
  6485. UserData = TerminalNoteData;
  6486. }
  6487. break;
  6488. case qeEmpty:
  6489. OperationComplete(ManagedTerminal->QueueOperationStart);
  6490. break;
  6491. case qePendingUserAction:
  6492. if ((ATerminal != Terminal) ||
  6493. (QueueInvisible && !IsQueueAutoPopup()))
  6494. {
  6495. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6496. }
  6497. break;
  6498. default:
  6499. DebugFail();
  6500. }
  6501. if (!Message.IsEmpty())
  6502. {
  6503. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6504. }
  6505. }
  6506. //---------------------------------------------------------------------------
  6507. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6508. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6509. {
  6510. if (FDDExtMapFile != NULL)
  6511. {
  6512. CloseHandle(FDDExtMapFile);
  6513. FDDExtMapFile = NULL;
  6514. }
  6515. if (WinConfiguration->DDFakeFile)
  6516. {
  6517. DDFakeFileInitDrag(FileList, Created);
  6518. }
  6519. }
  6520. //---------------------------------------------------------------------------
  6521. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  6522. {
  6523. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  6524. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  6525. {
  6526. FFakeFileDropTarget = FileName;
  6527. }
  6528. }
  6529. //---------------------------------------------------------------------------
  6530. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  6531. {
  6532. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6533. if (!ForceDirectories(ApiPath(Result)))
  6534. {
  6535. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  6536. }
  6537. FileSetAttr(ApiPath(Result), faHidden);
  6538. return Result;
  6539. }
  6540. //---------------------------------------------------------------------------
  6541. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  6542. bool & Created)
  6543. {
  6544. FFakeFileDropTarget = UnicodeString();
  6545. FDragFakeDirectory = CreateFakeTransferDirectory();
  6546. FileList->AddItem(NULL, FDragFakeDirectory);
  6547. Created = true;
  6548. if (!WinConfiguration->IsDDExtRunning() || (GetWindowsBuild() >= 17134))
  6549. {
  6550. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  6551. }
  6552. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6553. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6554. {
  6555. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6556. TDragExtCommStruct* CommStruct;
  6557. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6558. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6559. DebugAssert(CommStruct != NULL);
  6560. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  6561. CommStruct->Dragging = true;
  6562. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  6563. LENOF(CommStruct->DropDest));
  6564. NULL_TERMINATE(CommStruct->DropDest);
  6565. UnmapViewOfFile(CommStruct);
  6566. }
  6567. FDDMoveSlipped = false;
  6568. }
  6569. //---------------------------------------------------------------------------
  6570. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  6571. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  6572. {
  6573. bool Result;
  6574. if (Sender == RemoteDirView)
  6575. {
  6576. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  6577. }
  6578. else
  6579. {
  6580. DebugAssert(Sender == RemoteDriveView);
  6581. TStrings * FileList = RemoteDriveView->DragFileList();
  6582. try
  6583. {
  6584. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  6585. }
  6586. __finally
  6587. {
  6588. delete FileList;
  6589. }
  6590. }
  6591. if (FDDTargetControl == RemoteDriveView)
  6592. {
  6593. RemoteDriveView->UpdateDropTarget();
  6594. }
  6595. // foRemoteMove happens when file/dir is dragged within the remote tree view
  6596. // or from tree view to dir view.
  6597. // foMove happens when file/dir is dragged from remote tree view outside of
  6598. // application via dragex
  6599. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  6600. (DropSourceControl == RemoteDriveView))
  6601. {
  6602. RemoteDriveView->UpdateDropSource();
  6603. }
  6604. return Result;
  6605. }
  6606. //---------------------------------------------------------------------------
  6607. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  6608. {
  6609. // This also handles drops of remote files to queue.
  6610. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  6611. SAFE_DESTROY(FDDFileList);
  6612. if (!FFakeFileDropTarget.IsEmpty())
  6613. {
  6614. RemoveDir(ApiPath(FFakeFileDropTarget));
  6615. }
  6616. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  6617. {
  6618. try
  6619. {
  6620. TDragResult DDResult = (Sender == RemoteDirView) ?
  6621. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  6622. // Focus is moved to the target application,
  6623. // but as we are going to present the UI, we need to steal the focus back.
  6624. // This most likely won't work though (windows does not allow application
  6625. // to steal focus most of the time)
  6626. Application->BringToFront();
  6627. // On older version of Windows we never got drMove here, see also comment below.
  6628. // On Windows 10, we get the "move".
  6629. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  6630. {
  6631. UnicodeString TargetDirectory;
  6632. TFileOperation Operation;
  6633. // drInvalid may mean drMove, see comment below
  6634. switch (DDResult)
  6635. {
  6636. case drCopy:
  6637. Operation = foCopy;
  6638. break;
  6639. case drMove:
  6640. Operation = foMove;
  6641. break;
  6642. default:
  6643. DebugFail();
  6644. case drInvalid:
  6645. // prefer "copy" for safety
  6646. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  6647. break;
  6648. }
  6649. if (FDDMoveSlipped)
  6650. {
  6651. Operation = foMove;
  6652. }
  6653. TTransferOperationParam Param;
  6654. UnicodeString CounterName;
  6655. bool ForceQueue;
  6656. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  6657. {
  6658. // we get drInvalid both if d&d was intercepted by ddext,
  6659. // and when users drops on no-drop location.
  6660. // we tell the difference by existence of response from ddext,
  6661. // so we ignore absence of response in this case
  6662. if (DDResult != drInvalid)
  6663. {
  6664. // here we know that the extension is installed,
  6665. // as it is checked as soon as drag&drop starts from
  6666. // RemoteFileControlDDCreateDragFileList
  6667. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  6668. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  6669. }
  6670. }
  6671. else
  6672. {
  6673. // download using fake file
  6674. Param.Temp = false;
  6675. Param.DragDrop = true;
  6676. if (ForceQueue)
  6677. {
  6678. Param.Queue = asOn;
  6679. }
  6680. if (Sender == RemoteDirView)
  6681. {
  6682. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  6683. }
  6684. if (RemoteFileControlFileOperation(Sender, Operation,
  6685. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  6686. {
  6687. Configuration->Usage->Inc(CounterName);
  6688. }
  6689. }
  6690. }
  6691. }
  6692. __finally
  6693. {
  6694. CloseHandle(FDDExtMapFile);
  6695. FDDExtMapFile = NULL;
  6696. delete FDragFakeMonitors;
  6697. FDragFakeMonitors = NULL;
  6698. RemoveDir(ApiPath(FDragFakeDirectory));
  6699. FDragFakeDirectory = L"";
  6700. FFakeFileDropTarget = UnicodeString();
  6701. }
  6702. }
  6703. if (FDragDropSshTerminate.get() != NULL)
  6704. {
  6705. FDragDropSshTerminate->Rethrow();
  6706. }
  6707. }
  6708. //---------------------------------------------------------------------------
  6709. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  6710. TObject * Sender, int dwEffect, HRESULT & /*Result*/)
  6711. {
  6712. HCURSOR SlippedCopyCursor;
  6713. FDDMoveSlipped =
  6714. (FDragMoveCursor != NULL) &&
  6715. (!WinConfiguration->DDAllowMoveInit) && (dwEffect == DROPEFFECT_Copy) &&
  6716. ((IsFileControl(FDDTargetControl, osRemote) && (GetKeyState(VK_CONTROL) >= 0) &&
  6717. FTerminal->IsCapable[fcRemoteMove]) ||
  6718. (IsFileControl(FDDTargetControl, osLocal) && (GetKeyState(VK_SHIFT) < 0)));
  6719. SlippedCopyCursor = FDDMoveSlipped ? FDragMoveCursor : Dragdrop::DefaultCursor;
  6720. DragDropFiles(Sender)->CHCopy = SlippedCopyCursor;
  6721. DragDropFiles(Sender)->CHScrollCopy = SlippedCopyCursor;
  6722. // Remember drop effect so we know (when user drops files), if we copy or move
  6723. FLastDropEffect = dwEffect;
  6724. }
  6725. //---------------------------------------------------------------------------
  6726. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  6727. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  6728. {
  6729. bool Result;
  6730. if (FDDTargetControl == QueueView3)
  6731. {
  6732. Directory = DefaultDownloadTargetDirectory();
  6733. Result = true;
  6734. CounterName = L"DownloadsDragDropQueue";
  6735. ForceQueue = true;
  6736. }
  6737. else if (!FFakeFileDropTarget.IsEmpty())
  6738. {
  6739. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  6740. Result = true;
  6741. ForceQueue = false;
  6742. CounterName = L"DownloadsDragDropFakeFile";
  6743. }
  6744. else
  6745. {
  6746. ForceQueue = false;
  6747. Enabled = false;
  6748. try
  6749. {
  6750. int Timer = 0;
  6751. Result = false;
  6752. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  6753. {
  6754. {
  6755. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6756. TDragExtCommStruct* CommStruct;
  6757. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6758. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6759. DebugAssert(CommStruct != NULL);
  6760. Result = !CommStruct->Dragging;
  6761. if (Result)
  6762. {
  6763. Directory = ExtractFilePath(CommStruct->DropDest);
  6764. CounterName = L"DownloadsDragDropExternalExt";
  6765. }
  6766. UnmapViewOfFile(CommStruct);
  6767. }
  6768. if (!Result)
  6769. {
  6770. Sleep(50);
  6771. Timer += 50;
  6772. Application->ProcessMessages();
  6773. }
  6774. }
  6775. }
  6776. __finally
  6777. {
  6778. Enabled = true;
  6779. }
  6780. }
  6781. return Result;
  6782. }
  6783. //---------------------------------------------------------------------------
  6784. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  6785. const UnicodeString TempDir, int SecDelay)
  6786. {
  6787. TDateTime Alarm = Now() + (double)((double)SecDelay*OneMillisecond);
  6788. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  6789. if (FDelayedDeletionTimer == NULL)
  6790. {
  6791. DebugAssert(HandleAllocated());
  6792. FDelayedDeletionTimer = new TTimer(this);
  6793. FDelayedDeletionTimer->Interval = 10000;
  6794. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  6795. }
  6796. else
  6797. {
  6798. FDelayedDeletionTimer->Enabled = true;
  6799. }
  6800. }
  6801. //---------------------------------------------------------------------------
  6802. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  6803. {
  6804. DebugAssert(FDelayedDeletionList != NULL);
  6805. TDateTime N = Now();
  6806. TDateTime Alert;
  6807. UnicodeString Directory;
  6808. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  6809. {
  6810. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  6811. if ((N >= Alert) || (Sender == NULL))
  6812. {
  6813. Directory = FDelayedDeletionList->Strings[Index];
  6814. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  6815. {
  6816. FDelayedDeletionList->Delete(Index);
  6817. }
  6818. }
  6819. }
  6820. if (FDelayedDeletionList->Count == 0)
  6821. {
  6822. FDelayedDeletionTimer->Enabled = false;
  6823. }
  6824. }
  6825. //---------------------------------------------------------------------------
  6826. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  6827. {
  6828. if (IsFileControl(FDDTargetControl, osRemote) ||
  6829. (FDDTargetControl == SessionsPageControl))
  6830. {
  6831. TTerminal * TargetTerminal = NULL;
  6832. TFileOperation Operation = foNone;
  6833. if (FDDTargetControl == SessionsPageControl)
  6834. {
  6835. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  6836. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  6837. // do not allow dropping on the "+" tab
  6838. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  6839. if (TargetTerminal != NULL)
  6840. {
  6841. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  6842. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  6843. {
  6844. Operation = foRemoteMove;
  6845. }
  6846. else
  6847. {
  6848. Operation = foRemoteCopy;
  6849. }
  6850. }
  6851. }
  6852. else
  6853. {
  6854. // when move from remote side is disabled, we allow copying inside the remote
  6855. // panel, but we interpret is as moving (we also slip in the move cursor)
  6856. if ((FLastDropEffect == DROPEFFECT_MOVE) ||
  6857. (!WinConfiguration->DDAllowMoveInit && (FLastDropEffect == DROPEFFECT_COPY) &&
  6858. FDDMoveSlipped))
  6859. {
  6860. Operation = foRemoteMove;
  6861. }
  6862. else if (FLastDropEffect == DROPEFFECT_COPY)
  6863. {
  6864. Operation = foRemoteCopy;
  6865. }
  6866. }
  6867. if (Operation != foNone)
  6868. {
  6869. RemoteFileControlFileOperation(DropSourceControl,
  6870. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  6871. }
  6872. // abort drag&drop
  6873. Abort();
  6874. }
  6875. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  6876. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  6877. (FDDTargetControl != QueueView3))
  6878. {
  6879. DebugAssert(!FDragTempDir.IsEmpty());
  6880. TTransferType Type;
  6881. UnicodeString TempDir = FDragTempDir;
  6882. // We clear FDragTempDir before calling
  6883. // just in case it fail (raises exception)
  6884. FDragTempDir = L"";
  6885. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  6886. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  6887. // empty directory parameter means temp directory -> don't display it!
  6888. UnicodeString TargetDir = L"";
  6889. int Options = 0;
  6890. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  6891. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  6892. {
  6893. Abort();
  6894. }
  6895. // TargetDir is set when dropped on local file control
  6896. // (this was workaround for legacy dirview event handling, now it should be
  6897. // made prettier)
  6898. if (TargetDir.IsEmpty())
  6899. {
  6900. TargetDir = TempDir;
  6901. if (ForceDirectories(ApiPath(TargetDir)))
  6902. {
  6903. DebugAssert(Terminal && !TargetDir.IsEmpty());
  6904. FPendingTempSpaceWarn = true;
  6905. try
  6906. {
  6907. FDragDropOperation = true;
  6908. int Params = cpTemporary |
  6909. (Type == ttMove ? cpDelete : 0);
  6910. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  6911. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  6912. }
  6913. __finally
  6914. {
  6915. FDragDropOperation = false;
  6916. FPendingTempSpaceWarn = false;
  6917. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  6918. }
  6919. }
  6920. else
  6921. {
  6922. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  6923. }
  6924. }
  6925. }
  6926. }
  6927. //---------------------------------------------------------------------------
  6928. void __fastcall TCustomScpExplorerForm::DDDownload(
  6929. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  6930. {
  6931. void * BatchStorage;
  6932. BatchStart(BatchStorage);
  6933. try
  6934. {
  6935. UpdateCopyParamCounters(*CopyParam);
  6936. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  6937. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  6938. {
  6939. RemoteDriveView->UpdateDropSource();
  6940. }
  6941. }
  6942. __finally
  6943. {
  6944. BatchEnd(BatchStorage);
  6945. }
  6946. }
  6947. //---------------------------------------------------------------------------
  6948. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  6949. {
  6950. public:
  6951. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  6952. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename)
  6953. {
  6954. }
  6955. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  6956. {
  6957. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  6958. TDataObjectFilesEx::AllowData(FormatEtc);
  6959. }
  6960. };
  6961. //---------------------------------------------------------------------------
  6962. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  6963. TObject * Sender, TDataObject *& DataObject)
  6964. {
  6965. if (FDDExtMapFile != NULL)
  6966. {
  6967. TFileList * FileList = DragDropFiles(Sender)->FileList;
  6968. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  6969. {
  6970. Abort();
  6971. }
  6972. if (FileList->Count > 0)
  6973. {
  6974. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  6975. if (!FilesObject->IsValid(true, true))
  6976. {
  6977. FilesObject->_Release();
  6978. }
  6979. else
  6980. {
  6981. DataObject = FilesObject;
  6982. }
  6983. }
  6984. }
  6985. }
  6986. //---------------------------------------------------------------------------
  6987. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  6988. {
  6989. DebugFail();
  6990. }
  6991. //---------------------------------------------------------------------------
  6992. void __fastcall TCustomScpExplorerForm::GoToTree()
  6993. {
  6994. ComponentVisible[fcRemoteTree] = true;
  6995. RemoteDriveView->SetFocus();
  6996. }
  6997. //---------------------------------------------------------------------------
  6998. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  6999. TPanelExport Export)
  7000. {
  7001. TCustomDirView * DirView = this->DirView(Side);
  7002. std::unique_ptr<TStrings> ExportData(new TStringList());
  7003. switch (Export)
  7004. {
  7005. case pePath:
  7006. ExportData->Add(DirView->PathName);
  7007. break;
  7008. case peFileList:
  7009. case peFullFileList:
  7010. {
  7011. bool FullPath = (Export == peFullFileList);
  7012. DirView->CreateFileList(false, FullPath, ExportData.get());
  7013. for (int Index = 0; Index < ExportData->Count; Index++)
  7014. {
  7015. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7016. {
  7017. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7018. }
  7019. }
  7020. }
  7021. break;
  7022. default:
  7023. DebugFail();
  7024. }
  7025. return ExportData.release();
  7026. }
  7027. //---------------------------------------------------------------------------
  7028. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7029. TPanelExport Export, TPanelExportDestination Destination)
  7030. {
  7031. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7032. PanelExportStore(Side, Export, Destination, ExportData.get());
  7033. }
  7034. //---------------------------------------------------------------------------
  7035. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7036. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7037. TStrings * ExportData)
  7038. {
  7039. if (Destination == pedClipboard)
  7040. {
  7041. TInstantOperationVisualizer Visualizer;
  7042. CopyToClipboard(ExportData);
  7043. }
  7044. else
  7045. {
  7046. DebugFail();
  7047. }
  7048. }
  7049. //---------------------------------------------------------------------------
  7050. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7051. {
  7052. TCustomDirView * DirView = this->DirView(Side);
  7053. TFileFilter Filter;
  7054. DefaultFileFilter(Filter);
  7055. Filter.Masks = DirView->Mask;
  7056. if (DoFilterMaskDialog(DirView, &Filter))
  7057. {
  7058. DirView->Mask = TFileMasks::NormalizeMask(Filter.Masks);
  7059. Configuration->Usage->Inc(L"Filters");
  7060. }
  7061. }
  7062. //---------------------------------------------------------------------------
  7063. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7064. {
  7065. return FQueueController->DefaultOperation();
  7066. }
  7067. //---------------------------------------------------------------------------
  7068. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7069. TQueueOperation Operation, void ** Param)
  7070. {
  7071. switch (Operation)
  7072. {
  7073. case qoPreferences:
  7074. return true;
  7075. case qoGoTo:
  7076. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7077. case qoOnceEmpty:
  7078. return !FQueueController->Empty;
  7079. default:
  7080. return FQueueController->AllowOperation(Operation, Param);
  7081. }
  7082. }
  7083. //---------------------------------------------------------------------------
  7084. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7085. {
  7086. if (DebugAlwaysTrue(QueueView3->Visible))
  7087. {
  7088. QueueView3->SetFocus();
  7089. }
  7090. }
  7091. //---------------------------------------------------------------------------
  7092. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7093. TQueueOperation Operation, void * Param)
  7094. {
  7095. if (Operation == qoGoTo)
  7096. {
  7097. GoToQueue();
  7098. }
  7099. else if (Operation == qoPreferences)
  7100. {
  7101. PreferencesDialog(pmQueue);
  7102. }
  7103. else
  7104. {
  7105. FQueueController->ExecuteOperation(Operation, Param);
  7106. }
  7107. }
  7108. //---------------------------------------------------------------------------
  7109. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7110. {
  7111. return (Queue != NULL) && Queue->Enabled;
  7112. }
  7113. //---------------------------------------------------------------------------
  7114. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7115. {
  7116. DebugAssert(Queue != NULL);
  7117. if (Queue != NULL)
  7118. {
  7119. Queue->Enabled = !Queue->Enabled;
  7120. }
  7121. }
  7122. //---------------------------------------------------------------------------
  7123. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7124. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7125. {
  7126. FQueueActedItem = QueueView3->ItemFocused;
  7127. }
  7128. //---------------------------------------------------------------------------
  7129. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7130. {
  7131. return TopDock->Height + QueueSplitter->Height;
  7132. }
  7133. //---------------------------------------------------------------------------
  7134. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7135. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7136. {
  7137. // when queue is hidden by double-clicking splitter, stray attempt to
  7138. // resize the panel with strange value arrives, make sure it is ignored
  7139. if (ComponentVisible[fcQueueView])
  7140. {
  7141. int HeightLimit = ClientHeight - GetStaticComponentsHeight() -
  7142. RemotePanel->Constraints->MinHeight;
  7143. if (NewSize > HeightLimit)
  7144. {
  7145. NewSize = HeightLimit;
  7146. }
  7147. }
  7148. else
  7149. {
  7150. Accept = false;
  7151. }
  7152. }
  7153. //---------------------------------------------------------------------------
  7154. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7155. TDragObject *& /*DragObject*/)
  7156. {
  7157. FQueueActedItem = QueueView3->ItemFocused;
  7158. }
  7159. //---------------------------------------------------------------------------
  7160. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7161. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7162. {
  7163. Accept = true;
  7164. if (Source == QueueView3)
  7165. {
  7166. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7167. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7168. if (Accept)
  7169. {
  7170. TQueueItemProxy * QueueItem;
  7171. TQueueItemProxy * DestQueueItem;
  7172. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7173. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7174. Accept = (QueueItem != DestQueueItem) &&
  7175. (QueueItem->Status == TQueueItem::qsPending) &&
  7176. (DestQueueItem->Status == TQueueItem::qsPending);
  7177. }
  7178. }
  7179. }
  7180. //---------------------------------------------------------------------------
  7181. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7182. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7183. {
  7184. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7185. {
  7186. TQueueItemProxy * QueueItem;
  7187. TQueueItemProxy * DestQueueItem;
  7188. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7189. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7190. QueueItem->Move(DestQueueItem);
  7191. }
  7192. }
  7193. //---------------------------------------------------------------------------
  7194. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7195. {
  7196. if ((QueueView3->ItemFocused == NULL) &&
  7197. (QueueView3->Items->Count > 0))
  7198. {
  7199. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7200. }
  7201. QueueLabelUpdateStatus();
  7202. }
  7203. //---------------------------------------------------------------------------
  7204. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7205. {
  7206. QueueLabelUpdateStatus();
  7207. }
  7208. //---------------------------------------------------------------------------
  7209. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7210. {
  7211. QueueLabel->UpdateStatus();
  7212. }
  7213. //---------------------------------------------------------------------------
  7214. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7215. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7216. {
  7217. if (Selected)
  7218. {
  7219. NonVisualDataModule->UpdateNonVisibleActions();
  7220. }
  7221. }
  7222. //---------------------------------------------------------------------------
  7223. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7224. {
  7225. TDragDropFilesEx * Result = NULL;
  7226. if (Sender == SessionsPageControl)
  7227. {
  7228. Result = FSessionsDragDropFilesEx;
  7229. }
  7230. else if (Sender == QueueView3)
  7231. {
  7232. Result = FQueueDragDropFilesEx;
  7233. }
  7234. else
  7235. {
  7236. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7237. if (DirView != NULL)
  7238. {
  7239. Result = DirView->DragDropFilesEx;
  7240. }
  7241. else
  7242. {
  7243. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7244. if (DriveView != NULL)
  7245. {
  7246. Result = DriveView->DragDropFilesEx;
  7247. }
  7248. }
  7249. }
  7250. DebugAssert(Result != NULL);
  7251. return Result;
  7252. }
  7253. //---------------------------------------------------------------------------
  7254. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7255. {
  7256. return (DView->SelCount == DView->FilesCount);
  7257. }
  7258. //---------------------------------------------------------------------------
  7259. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7260. {
  7261. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7262. }
  7263. //---------------------------------------------------------------------------
  7264. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7265. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue)
  7266. {
  7267. TFileOperation Operation;
  7268. switch (Effect)
  7269. {
  7270. case DROPEFFECT_MOVE:
  7271. Operation = foMove;
  7272. break;
  7273. case DROPEFFECT_COPY:
  7274. // occures on WinXP (reported by user)
  7275. default:
  7276. Operation = foCopy;
  7277. break;
  7278. };
  7279. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7280. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7281. if (DragDropFilesEx->FileList->Count > 0)
  7282. {
  7283. TStrings * FileList = new TStringList();
  7284. try
  7285. {
  7286. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7287. {
  7288. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7289. }
  7290. FDragDropOperation = true;
  7291. TTransferOperationParam Param;
  7292. Param.TargetDirectory = TargetPath;
  7293. // upload, no temp dirs
  7294. Param.Temp = false;
  7295. Param.DragDrop = true;
  7296. Param.Options =
  7297. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7298. if (ForceQueue)
  7299. {
  7300. Param.Queue = asOn;
  7301. }
  7302. if (ExecuteFileOperation(Operation, osLocal, FileList,
  7303. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  7304. {
  7305. if (IsFileControl(DropSourceControl, osLocal))
  7306. {
  7307. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7308. }
  7309. else
  7310. {
  7311. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7312. }
  7313. }
  7314. }
  7315. __finally
  7316. {
  7317. FDragDropOperation = false;
  7318. delete FileList;
  7319. }
  7320. }
  7321. }
  7322. //---------------------------------------------------------------------------
  7323. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7324. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7325. UnicodeString TargetPath, bool & /*DoOperation*/)
  7326. {
  7327. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false);
  7328. }
  7329. //---------------------------------------------------------------------------
  7330. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7331. TObject * Sender, int grfKeyState, int & dwEffect)
  7332. {
  7333. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7334. if ((dwEffect != DROPEFFECT_None) &&
  7335. IsFileControl(DropSourceControl, osRemote))
  7336. {
  7337. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7338. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7339. (RemoteDirView->DropTarget == NULL))
  7340. {
  7341. dwEffect = DROPEFFECT_None;
  7342. }
  7343. else
  7344. {
  7345. if (dwEffect == DROPEFFECT_Copy)
  7346. {
  7347. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  7348. // currently we support copying always (at least via temporary directory);
  7349. // remove associated checks once this all proves stable and working
  7350. bool CopyCapable = true;
  7351. // if we do not support neither of operations, there's no discussion
  7352. if (!MoveCapable && !CopyCapable)
  7353. {
  7354. dwEffect = DROPEFFECT_None;
  7355. }
  7356. // when moving is disabled, we need to keep effect to "copy",
  7357. // which will be later interpretted as move (with slipped-in cursor)
  7358. else if (!WinConfiguration->DDAllowMoveInit && FLAGCLEAR(grfKeyState, MK_CONTROL))
  7359. {
  7360. // no-op, keep copy
  7361. }
  7362. else
  7363. {
  7364. // The default effect inside remote panel is move,
  7365. // unless we do not support it, but support copy
  7366. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7367. {
  7368. dwEffect = MoveCapable ? DROPEFFECT_Move : DROPEFFECT_Copy;
  7369. }
  7370. else
  7371. {
  7372. // with ctrl-down, we want copy unless it is not supported
  7373. dwEffect = CopyCapable ? DROPEFFECT_Copy : DROPEFFECT_None;
  7374. }
  7375. }
  7376. }
  7377. }
  7378. }
  7379. }
  7380. //---------------------------------------------------------------------------
  7381. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7382. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7383. {
  7384. if (FDDTotalSize >= 0)
  7385. {
  7386. if (File->IsDirectory)
  7387. {
  7388. FDDTotalSize = -1;
  7389. }
  7390. else
  7391. {
  7392. FDDTotalSize += File->Size;
  7393. }
  7394. }
  7395. DebugAssert(!FDragTempDir.IsEmpty());
  7396. // TODO: this is quite ineffective
  7397. // TODO: what if invalid character replacement is disabled?
  7398. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7399. UnicodeString TransferFileName;
  7400. if (Sender == RemoteDriveView)
  7401. {
  7402. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7403. }
  7404. else
  7405. {
  7406. TransferFileName = File->FileName;
  7407. }
  7408. FDDFileList->AddObject(TransferFileName, File);
  7409. }
  7410. //---------------------------------------------------------------------------
  7411. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7412. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7413. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7414. {
  7415. // sometimes we do not get DDEnd so the list is not released
  7416. SAFE_DESTROY(FDDFileList);
  7417. FDDFileList = new TStringList();
  7418. FDragTempDir = WinConfiguration->TemporaryDir();
  7419. FDDTotalSize = 0;
  7420. FDragDropSshTerminate.reset(NULL);
  7421. }
  7422. //---------------------------------------------------------------------------
  7423. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7424. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7425. HRESULT & Result)
  7426. {
  7427. if (Result == DRAGDROP_S_DROP)
  7428. {
  7429. try
  7430. {
  7431. GlobalDragImageList->HideDragImage();
  7432. try
  7433. {
  7434. RemoteFileControlDDTargetDrop();
  7435. }
  7436. catch(ESshTerminate & E)
  7437. {
  7438. FDragDropSshTerminate.reset(E.Clone());
  7439. }
  7440. }
  7441. catch (Exception &E)
  7442. {
  7443. // If downloading fails we need to cancel drag&drop, otherwise
  7444. // Explorer shows error
  7445. // But by the way exception probably never reach this point as
  7446. // it's catched on way.
  7447. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7448. Result = DRAGDROP_S_CANCEL;
  7449. ShowExtendedException(Terminal, &E);
  7450. }
  7451. }
  7452. }
  7453. //---------------------------------------------------------------------------
  7454. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7455. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7456. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7457. {
  7458. TFileMasks::TParams MaskParams;
  7459. MaskParams.Size = Size;
  7460. MaskParams.Modification = Modification;
  7461. // this does not re-parse the mask if it is the same as the last time
  7462. FDirViewMatchMask = Masks;
  7463. bool ImplicitMatch;
  7464. Matches =
  7465. // RecurseInclude parameter has no effect as the Path is empty
  7466. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7467. (AllowImplicitMatches || !ImplicitMatch);
  7468. }
  7469. //---------------------------------------------------------------------------
  7470. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7471. TObject * Sender, TListItem * Item, WORD & Indexes)
  7472. {
  7473. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7474. UnicodeString Ext;
  7475. if (DirView == RemoteDirView)
  7476. {
  7477. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7478. }
  7479. else
  7480. {
  7481. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7482. }
  7483. if (SameText(Ext, Configuration->PartialExt))
  7484. {
  7485. Indexes |= oiPartial;
  7486. }
  7487. }
  7488. //---------------------------------------------------------------------------
  7489. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7490. {
  7491. return
  7492. DirViewEnabled(osCurrent) &&
  7493. DirView(osCurrent)->CanPasteFromClipBoard();
  7494. }
  7495. //---------------------------------------------------------------------------
  7496. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7497. {
  7498. bool Result = false;
  7499. if (CanPasteToDirViewFromClipBoard())
  7500. {
  7501. Result = true;
  7502. }
  7503. else
  7504. {
  7505. UnicodeString ClipboardText;
  7506. if (NonEmptyTextFromClipboard(ClipboardText))
  7507. {
  7508. if (StoredSessions->IsUrl(ClipboardText))
  7509. {
  7510. Result = true;
  7511. }
  7512. else
  7513. {
  7514. Result = DirViewEnabled(osCurrent);
  7515. }
  7516. }
  7517. }
  7518. return Result;
  7519. }
  7520. //---------------------------------------------------------------------------
  7521. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7522. {
  7523. if (DoesClipboardContainOurFiles())
  7524. {
  7525. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  7526. {
  7527. TTerminalManager * Manager = TTerminalManager::Instance();
  7528. TTerminal * TergetTerminal = Manager->ActiveTerminal;
  7529. Manager->ActiveTerminal = FClipboardTerminal;
  7530. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TergetTerminal);
  7531. }
  7532. }
  7533. else if (CanPasteToDirViewFromClipBoard())
  7534. {
  7535. DirView(osCurrent)->PasteFromClipBoard();
  7536. }
  7537. else
  7538. {
  7539. UnicodeString ClipboardText;
  7540. if (NonEmptyTextFromClipboard(ClipboardText))
  7541. {
  7542. if (StoredSessions->IsUrl(ClipboardText))
  7543. {
  7544. NewSession(false, ClipboardText);
  7545. }
  7546. else
  7547. {
  7548. DirView(osCurrent)->Path = ClipboardText;
  7549. }
  7550. }
  7551. }
  7552. }
  7553. //---------------------------------------------------------------------------
  7554. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  7555. {
  7556. // TBD
  7557. }
  7558. //---------------------------------------------------------------------------
  7559. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  7560. {
  7561. TCustomDirView * ADirView = DirView(Side);
  7562. ADirView->SelectAll(Mode);
  7563. ADirView->SetFocus();
  7564. }
  7565. //---------------------------------------------------------------------------
  7566. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  7567. {
  7568. TCustomDirView * ADirView = DirView(Side);
  7569. TFileFilter Filter;
  7570. DefaultFileFilter(Filter);
  7571. if (DoSelectMaskDialog(ADirView, Select, &Filter, Configuration))
  7572. {
  7573. Configuration->Usage->Inc(L"MaskSelections");
  7574. ADirView->SelectFiles(Filter, Select);
  7575. ADirView->SetFocus();
  7576. }
  7577. }
  7578. //---------------------------------------------------------------------------
  7579. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  7580. {
  7581. TCustomDirView * ADirView = DirView(Side);
  7582. ADirView->RestoreSelectedNames();
  7583. ADirView->SetFocus();
  7584. }
  7585. //---------------------------------------------------------------------------
  7586. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  7587. {
  7588. TCustomDirView * CurrentDirView = DirView(osCurrent);
  7589. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  7590. {
  7591. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  7592. UnicodeString Ext;
  7593. if (GetSide(osCurrent) == osRemote)
  7594. {
  7595. Ext = UnixExtractFileExt(FileName);
  7596. }
  7597. else
  7598. {
  7599. Ext = ExtractFileExt(FileName);
  7600. }
  7601. if (Ext.IsEmpty())
  7602. {
  7603. Ext = L".";
  7604. }
  7605. TFileFilter Filter;
  7606. Filter.Masks = FORMAT(L"*%s", (Ext));
  7607. Filter.Directories = false;
  7608. CurrentDirView->SelectFiles(Filter, Select);
  7609. }
  7610. }
  7611. //---------------------------------------------------------------------------
  7612. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  7613. const TStatusFileInfo & FileInfo, TOperationSide Side)
  7614. {
  7615. UnicodeString Result;
  7616. if ((Side == osRemote) && (Terminal == NULL))
  7617. {
  7618. // noop
  7619. }
  7620. else
  7621. {
  7622. Result =
  7623. FMTLOAD(FILE_INFO_FORMAT,
  7624. (FormatBytes(FileInfo.SelectedSize),
  7625. FormatBytes(FileInfo.FilesSize),
  7626. FormatNumber(FileInfo.SelectedCount),
  7627. FormatNumber(FileInfo.FilesCount)));
  7628. }
  7629. return Result;
  7630. }
  7631. //---------------------------------------------------------------------------
  7632. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  7633. TTBXStatusPanel * Panel, TOperationSide Side)
  7634. {
  7635. if (Panel->Index == 1)
  7636. {
  7637. ToggleShowHiddenFiles();
  7638. }
  7639. else if (Panel->Index == 2)
  7640. {
  7641. Filter(Side);
  7642. }
  7643. }
  7644. //---------------------------------------------------------------------------
  7645. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  7646. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  7647. {
  7648. DebugAssert(!StatusBar->SimplePanel);
  7649. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  7650. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  7651. }
  7652. //---------------------------------------------------------------------------
  7653. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  7654. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  7655. {
  7656. DebugAssert(StatusBar->Panels->Count >= 3);
  7657. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  7658. if (FileInfo.HiddenCount > 0)
  7659. {
  7660. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  7661. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7662. }
  7663. else
  7664. {
  7665. HiddenFilesPanel->ViewPriority = 0;
  7666. // not really necessary, just to cleanup no-longer-valid data
  7667. HiddenFilesPanel->Caption = L"";
  7668. }
  7669. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  7670. if (FileInfo.FilteredCount > 0)
  7671. {
  7672. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  7673. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7674. }
  7675. else
  7676. {
  7677. FilteredFilesPanel->ViewPriority = 0;
  7678. // not really necessary, just to cleanup no-longer-valid data
  7679. FilteredFilesPanel->Caption = L"";
  7680. }
  7681. }
  7682. //---------------------------------------------------------------------------
  7683. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  7684. TObject * /*Sender*/)
  7685. {
  7686. if (RemoteDirView->Enabled)
  7687. {
  7688. RemoteDirView->SetFocus();
  7689. }
  7690. }
  7691. //---------------------------------------------------------------------------
  7692. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  7693. {
  7694. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  7695. switch (Config.Show)
  7696. {
  7697. case qvShow:
  7698. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  7699. {
  7700. Config.Show = qvHide;
  7701. }
  7702. else
  7703. {
  7704. Config.Show = Config.LastHideShow;
  7705. }
  7706. break;
  7707. case qvHideWhenEmpty:
  7708. if (ComponentVisible[fcQueueView])
  7709. {
  7710. Config.Show = qvHide;
  7711. }
  7712. else
  7713. {
  7714. Config.LastHideShow = Config.Show;
  7715. Config.Show = qvShow;
  7716. }
  7717. break;
  7718. case qvHide:
  7719. Config.LastHideShow = Config.Show;
  7720. Config.Show = qvShow;
  7721. break;
  7722. }
  7723. WinConfiguration->QueueView = Config;
  7724. }
  7725. //---------------------------------------------------------------------------
  7726. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  7727. {
  7728. UnicodeString Result;
  7729. if (FTerminal != NULL)
  7730. {
  7731. switch (WinConfiguration->PathInCaption)
  7732. {
  7733. case picShort:
  7734. Result = TTerminalManager::Instance()->GetTerminalShortPath(FTerminal);
  7735. break;
  7736. case picFull:
  7737. Result = FTerminal->CurrentDirectory;
  7738. break;
  7739. }
  7740. }
  7741. return Result;
  7742. }
  7743. //---------------------------------------------------------------------------
  7744. void __fastcall TCustomScpExplorerForm::UpdateControls()
  7745. {
  7746. TTerminalManager::Instance()->UpdateAppTitle();
  7747. // WORAKRDOUND: Disabling list view when it is not showing yet does not set its
  7748. // background to gray on Windows 7 (works on Windows 10).
  7749. // See also EnableControl
  7750. if (Showing)
  7751. {
  7752. bool HasTerminal = (Terminal != NULL);
  7753. if (HasTerminal)
  7754. {
  7755. // Update path when it changes
  7756. if ((SessionsPageControl->ActivePage != NULL) && (GetSessionTabTerminal(SessionsPageControl->ActivePage) == Terminal))
  7757. {
  7758. UpdateSessionTab(SessionsPageControl->ActivePage);
  7759. }
  7760. if (!RemoteDirView->Enabled)
  7761. {
  7762. RemoteDirView->Enabled = true;
  7763. if (FRemoteDirViewWasFocused)
  7764. {
  7765. ActiveControl = RemoteDirView;
  7766. }
  7767. }
  7768. RemoteDriveView->Enabled = true;
  7769. RemoteDirView->Color = (FSessionColor != 0 ? FSessionColor : clWindow);
  7770. RemoteDriveView->Color = RemoteDirView->Color;
  7771. }
  7772. else
  7773. {
  7774. if (RemoteDirView->Enabled)
  7775. {
  7776. // This is first called when the form is being constructed
  7777. // (not anymore due to Showing test above)
  7778. // but the false is overriden in the constructor later.
  7779. // An even later in TScpCommanderForm::DoShow()
  7780. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  7781. EnableControl(RemoteDirView, false);
  7782. }
  7783. EnableControl(RemoteDriveView, false);
  7784. }
  7785. EnableControl(QueueView3, HasTerminal && Terminal->IsCapable[fsBackgroundTransfers]);
  7786. QueueLabelUpdateStatus();
  7787. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  7788. }
  7789. }
  7790. //---------------------------------------------------------------------------
  7791. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  7792. {
  7793. UpdateControls();
  7794. }
  7795. //---------------------------------------------------------------------------
  7796. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  7797. TObject * Sender)
  7798. {
  7799. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7800. DebugAssert(DirView != NULL);
  7801. DoDirViewLoaded(DirView);
  7802. TransferPresetAutoSelect();
  7803. }
  7804. //---------------------------------------------------------------------------
  7805. void __fastcall TCustomScpExplorerForm::StartUpdates()
  7806. {
  7807. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  7808. // first run after installation
  7809. if (double(Updates.LastCheck) == 0)
  7810. {
  7811. // make sure next time there will be an update (if enabled)
  7812. Updates.LastCheck = TDateTime(1);
  7813. WinConfiguration->Updates = Updates;
  7814. }
  7815. else if ((double(Updates.Period) > 0) &&
  7816. (Now() - Updates.LastCheck >= Updates.Period))
  7817. {
  7818. TThreadMethod OnUpdatesChecked = NULL;
  7819. if (!IsUWP())
  7820. {
  7821. OnUpdatesChecked = UpdatesChecked;
  7822. }
  7823. StartUpdateThread(OnUpdatesChecked);
  7824. }
  7825. }
  7826. //---------------------------------------------------------------------------
  7827. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  7828. {
  7829. UnicodeString Message;
  7830. bool New;
  7831. TQueryType Type;
  7832. GetUpdatesMessage(Message, New, Type, false);
  7833. if (!Message.IsEmpty())
  7834. {
  7835. if (New)
  7836. {
  7837. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  7838. }
  7839. if (!New && (Type != qtWarning))
  7840. {
  7841. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  7842. }
  7843. else
  7844. {
  7845. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  7846. }
  7847. if (New)
  7848. {
  7849. Configuration->Usage->Inc(L"UpdateNotifications");
  7850. }
  7851. }
  7852. }
  7853. //---------------------------------------------------------------------------
  7854. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  7855. {
  7856. RestoreApp();
  7857. bool CanDisplay = !NonVisualDataModule->Busy;
  7858. if (!CanDisplay && (Screen->ActiveForm != NULL))
  7859. {
  7860. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  7861. }
  7862. if (CanDisplay)
  7863. {
  7864. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  7865. CheckForUpdates(true);
  7866. }
  7867. else
  7868. {
  7869. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  7870. }
  7871. }
  7872. //---------------------------------------------------------------------------
  7873. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  7874. TCopyParamRuleData & Data)
  7875. {
  7876. DebugAssert(Terminal != NULL);
  7877. Data.HostName = Terminal->SessionData->HostNameExpanded;
  7878. Data.UserName = Terminal->SessionData->UserNameExpanded;
  7879. Data.RemoteDirectory = RemoteDirView->PathName;
  7880. }
  7881. //---------------------------------------------------------------------------
  7882. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  7883. {
  7884. // Terminal can be null when we are changing local directory implicitly,
  7885. // because it has been deleted, while no session is connected
  7886. // (Login dialog is open)
  7887. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  7888. {
  7889. TCopyParamRuleData Data;
  7890. GetTransferPresetAutoSelectData(Data);
  7891. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  7892. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  7893. if (CopyParamIndex < 0)
  7894. {
  7895. // there is no preset that matches autoselection
  7896. // set preset that we consider "default"
  7897. FCopyParamAutoSelected = L""; // forget last autoselected preset
  7898. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  7899. }
  7900. else
  7901. {
  7902. // there is preset matching autoselection
  7903. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  7904. if (CopyParamName == FCopyParamAutoSelected)
  7905. {
  7906. // autoselected the same preset as the last time
  7907. // make no change (i.e. preserve custom user preset, if any)
  7908. }
  7909. else
  7910. {
  7911. // autoselected the different preset then the last time (or there
  7912. // was default preset set)
  7913. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  7914. GUIConfiguration->CopyParamCurrent = CopyParamName;
  7915. }
  7916. }
  7917. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  7918. {
  7919. if (CopyParamIndex >= 0)
  7920. {
  7921. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  7922. }
  7923. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  7924. try
  7925. {
  7926. int Fmt =
  7927. (CopyParamIndex < 0) ?
  7928. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  7929. COPY_PARAM_AUTOSELECTED;
  7930. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  7931. Data->Message = MainInstructions(Message);
  7932. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  7933. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  7934. CopyParamAttrs);
  7935. if (CopyParamIndex >= 0)
  7936. {
  7937. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  7938. Info = FORMAT(L"%s\n \n%s", (Info,
  7939. FMTLOAD(COPY_PARAM_RULE,
  7940. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  7941. }
  7942. Data->Message += L"\n\n" + Info;
  7943. if (WinConfiguration->CopyParamAutoSelectNotice)
  7944. {
  7945. TransferPresetNoteMessage(Data, true);
  7946. }
  7947. else
  7948. {
  7949. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  7950. Data = NULL; // ownership passed
  7951. }
  7952. }
  7953. __finally
  7954. {
  7955. delete Data;
  7956. }
  7957. }
  7958. }
  7959. }
  7960. //---------------------------------------------------------------------------
  7961. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  7962. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  7963. {
  7964. DebugAssert(NoteData != NULL);
  7965. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  7966. TQueryButtonAlias Aliases[1];
  7967. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  7968. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  7969. Params.Aliases = Aliases;
  7970. Params.AliasesCount = LENOF(Aliases);
  7971. unsigned int Result =
  7972. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  7973. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  7974. switch (Result)
  7975. {
  7976. case qaNeverAskAgain:
  7977. DebugAssert(AllowNeverAskAgain);
  7978. WinConfiguration->CopyParamAutoSelectNotice = false;
  7979. break;
  7980. case qaIgnore:
  7981. PreferencesDialog(pmPresets);
  7982. break;
  7983. }
  7984. }
  7985. //---------------------------------------------------------------------------
  7986. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  7987. {
  7988. // as of now useless, as this is used for notes only, never for balloons, ...
  7989. RestoreApp();
  7990. // .. and we should never be busy here
  7991. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  7992. {
  7993. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  7994. }
  7995. }
  7996. //---------------------------------------------------------------------------
  7997. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  7998. TPreferencesMode APreferencesMode)
  7999. {
  8000. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  8001. TCopyParamRuleData Data;
  8002. if (Terminal != NULL)
  8003. {
  8004. PreferencesData.reset(new TPreferencesDialogData());
  8005. GetTransferPresetAutoSelectData(Data);
  8006. PreferencesData->CopyParamRuleData = &Data;
  8007. }
  8008. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8009. }
  8010. //---------------------------------------------------------------------------
  8011. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8012. const TCustomCommandType & Command)
  8013. {
  8014. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8015. {
  8016. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (Command.Command)));
  8017. }
  8018. }
  8019. //---------------------------------------------------------------------------
  8020. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8021. {
  8022. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8023. TCustomCommandType Command;
  8024. // make sure we use local custom command when remote are not supported
  8025. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8026. {
  8027. Command = FLastCustomCommand;
  8028. }
  8029. else
  8030. {
  8031. Command.Params = Command.Params | ccLocal;
  8032. }
  8033. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8034. FEditingFocusedAdHocCommand = OnFocused;
  8035. bool LocalSide = (FCurrentSide == osLocal);
  8036. int Options =
  8037. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8038. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8039. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8040. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8041. {
  8042. FLastCustomCommand = Command;
  8043. UpdateCustomCommandsToolbar();
  8044. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8045. }
  8046. }
  8047. //---------------------------------------------------------------------------
  8048. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8049. {
  8050. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8051. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8052. DebugAssert(State > 0);
  8053. if (State <= 0)
  8054. {
  8055. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (FLastCustomCommand.Command)));
  8056. }
  8057. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8058. }
  8059. //---------------------------------------------------------------------------
  8060. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8061. TCustomCommandType & Command, int & State)
  8062. {
  8063. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8064. if (Result)
  8065. {
  8066. Command = FLastCustomCommand;
  8067. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8068. }
  8069. return Result;
  8070. }
  8071. //---------------------------------------------------------------------------
  8072. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8073. {
  8074. if (RemoteDirView->ItemFocused != NULL)
  8075. {
  8076. RemoteDirView->ItemFocused->CancelEdit();
  8077. }
  8078. }
  8079. //---------------------------------------------------------------------------
  8080. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8081. {
  8082. DebugAssert(ComponentVisible[Component]);
  8083. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8084. }
  8085. //---------------------------------------------------------------------------
  8086. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8087. {
  8088. // when queue panel is resized here directly, the status bar is stretched
  8089. // over whole space the panel occupied
  8090. PostComponentHide(fcQueueView);
  8091. }
  8092. //---------------------------------------------------------------------------
  8093. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8094. {
  8095. TMessage * M = static_cast<TMessage*>(Message);
  8096. switch (M->Msg)
  8097. {
  8098. case CM_DIALOGCHAR:
  8099. CMDialogChar(*M);
  8100. break;
  8101. case WM_APPCOMMAND:
  8102. WMAppCommand(*M);
  8103. break;
  8104. case WM_SYSCOMMAND:
  8105. WMSysCommand(*M);
  8106. break;
  8107. case WM_QUERYENDSESSION:
  8108. WMQueryEndSession(*M);
  8109. break;
  8110. case WM_ENDSESSION:
  8111. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8112. break;
  8113. #ifdef _DEBUG
  8114. case WM_WINDOWPOSCHANGED:
  8115. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8116. break;
  8117. case CM_CUSTOMSTYLECHANGED:
  8118. TForm::Dispatch(Message);
  8119. break;
  8120. #endif
  8121. case WM_COMPONENT_HIDE:
  8122. {
  8123. Byte Component = static_cast<Byte>(M->WParam);
  8124. // sanity check
  8125. if (ComponentVisible[Component])
  8126. {
  8127. // special treatment
  8128. if (Component == fcQueueView)
  8129. {
  8130. ToggleQueueVisibility();
  8131. DebugAssert(!ComponentVisible[fcQueueView]);
  8132. }
  8133. else
  8134. {
  8135. ComponentVisible[Component] = false;
  8136. }
  8137. }
  8138. }
  8139. break;
  8140. case WM_COPYDATA:
  8141. WMCopyData(*M);
  8142. break;
  8143. case WM_MANAGES_CAPTION:
  8144. // caption managed in TTerminalManager::UpdateAppTitle()
  8145. M->Result = 1;
  8146. break;
  8147. case WM_WANTS_MOUSEWHEEL:
  8148. M->Result = 1;
  8149. break;
  8150. case CM_SHOWINGCHANGED:
  8151. CMShowingChanged(*M);
  8152. break;
  8153. case WM_CLOSE:
  8154. WMClose(*M);
  8155. break;
  8156. case WM_DPICHANGED:
  8157. WMDpiChanged(*M);
  8158. break;
  8159. case CM_DPICHANGED:
  8160. CMDpiChanged(*M);
  8161. break;
  8162. default:
  8163. TForm::Dispatch(Message);
  8164. break;
  8165. }
  8166. }
  8167. //---------------------------------------------------------------------------
  8168. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8169. {
  8170. // noop
  8171. }
  8172. //---------------------------------------------------------------------------
  8173. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8174. {
  8175. TForm::Dispatch(&Message);
  8176. FSessionColors->Clear();
  8177. AddFixedSessionImages();
  8178. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8179. UpdateImages();
  8180. }
  8181. //---------------------------------------------------------------------------
  8182. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8183. {
  8184. TForm::Dispatch(&Message);
  8185. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8186. UpdatePixelsPerInchMainWindowCounter();
  8187. }
  8188. //---------------------------------------------------------------------------
  8189. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8190. {
  8191. // Cannot close window while we are busy.
  8192. // We cannot test this in FormCloseQuery as that is called also from
  8193. // Close(), which is called by CloseApplicationAction. So we can be busy
  8194. // there already even, when it is legitimate to close the application.
  8195. // Possibly a better place to handle this would be WMSysCommand.
  8196. if (NonVisualDataModule->Busy)
  8197. {
  8198. Message.Result = 1;
  8199. }
  8200. else
  8201. {
  8202. TForm::Dispatch(&Message);
  8203. }
  8204. }
  8205. //---------------------------------------------------------------------------
  8206. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8207. {
  8208. TForm::Dispatch(&Message);
  8209. if (Showing && (Terminal == NULL))
  8210. {
  8211. // When we are starting minimized (i.e. from an installer),
  8212. // postpone showing Login dialog until we get restored.
  8213. // Otherwise the Login dialog (and Authentication window) show restored
  8214. // over invidible (minimized) main window.
  8215. if (WindowState == wsMinimized)
  8216. {
  8217. FNeedSession = true;
  8218. }
  8219. else
  8220. {
  8221. // This happens before application ever goes idle, so the toolbars would
  8222. // stay enabled (initial state) until the Login dialog is dismissed.
  8223. UpdateActions();
  8224. NonVisualDataModule->StartBusy();
  8225. try
  8226. {
  8227. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8228. // otherwise the dialog does not receive focus.
  8229. // With Commander interface the ProcessMessages is called already
  8230. // by TDriveView, but with Explorer interface, we need to call it explicily
  8231. Application->ProcessMessages();
  8232. // do not reload sessions, they have been loaded just now (optimization)
  8233. NeedSession(false);
  8234. }
  8235. __finally
  8236. {
  8237. NonVisualDataModule->EndBusy();
  8238. }
  8239. }
  8240. }
  8241. }
  8242. //---------------------------------------------------------------------------
  8243. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8244. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8245. int & MaxHeight)
  8246. {
  8247. // workaround for bug in TWinControl.CalcConstraints
  8248. // Check for empty rect (restore from iconic state) is done there only after
  8249. // call to AdjustClientRect, which enlarges the rect (for forms).
  8250. TRect R = GetClientRect();
  8251. // when restoring from iconic state, place no restrictions
  8252. if (IsRectEmpty(R))
  8253. {
  8254. MinWidth = 0;
  8255. MinHeight = 0;
  8256. MaxWidth = 32000;
  8257. MaxHeight = 32000;
  8258. }
  8259. }
  8260. //---------------------------------------------------------------------------
  8261. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8262. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8263. {
  8264. // terminal can be already closed (e.g. dropped connection)
  8265. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8266. {
  8267. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8268. try
  8269. {
  8270. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8271. }
  8272. catch(...)
  8273. {
  8274. if (!Terminal->Active)
  8275. {
  8276. Close = true;
  8277. }
  8278. throw;
  8279. }
  8280. }
  8281. }
  8282. //---------------------------------------------------------------------------
  8283. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8284. {
  8285. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8286. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8287. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8288. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8289. {
  8290. OnGetSpaceAvailable = GetSpaceAvailable;
  8291. }
  8292. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8293. OnGetSpaceAvailable);
  8294. }
  8295. //---------------------------------------------------------------------------
  8296. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8297. {
  8298. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8299. Terminal->FillSessionDataForCode(Data.get());
  8300. return Data.release();
  8301. }
  8302. //---------------------------------------------------------------------------
  8303. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8304. {
  8305. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8306. DoGenerateUrlDialog(Data.get(), Paths);
  8307. }
  8308. //---------------------------------------------------------------------------
  8309. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8310. {
  8311. GenerateUrl(NULL);
  8312. }
  8313. //---------------------------------------------------------------------------
  8314. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8315. {
  8316. std::unique_ptr<TStrings> Paths(new TStringList());
  8317. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8318. GenerateUrl(Paths.get());
  8319. }
  8320. //---------------------------------------------------------------------------
  8321. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8322. {
  8323. if (value != FSessionColor)
  8324. {
  8325. FSessionColor = value;
  8326. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8327. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8328. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8329. DebugAssert(ColorItem != NULL);
  8330. ColorItem->Color = C;
  8331. NonVisualDataModule->ColorMenuItem->Color = C;
  8332. // Is null when called from LastTerminalClosed
  8333. if (Terminal != NULL)
  8334. {
  8335. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8336. }
  8337. UpdateControls();
  8338. }
  8339. }
  8340. //---------------------------------------------------------------------------
  8341. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8342. {
  8343. bool Result = FNoteTimer->Enabled;
  8344. if (Result)
  8345. {
  8346. // cannot cancel note too early
  8347. bool NotEarly =
  8348. (Now() - FNoteShown >
  8349. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8350. if (Force || NotEarly)
  8351. {
  8352. FNoteTimer->Enabled = false;
  8353. FNote = L"";
  8354. SAFE_DESTROY(FNoteData);
  8355. FOnNoteClick = NULL;
  8356. FNoteHints = FNotes->Text;
  8357. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8358. UpdateStatusBar();
  8359. }
  8360. }
  8361. return Result;
  8362. }
  8363. //---------------------------------------------------------------------------
  8364. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8365. {
  8366. DebugAssert(FNoteTimer->Enabled);
  8367. CancelNote(true);
  8368. }
  8369. //---------------------------------------------------------------------------
  8370. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8371. {
  8372. int P = Note.Pos(L"\n");
  8373. if (P > 0)
  8374. {
  8375. Note.SetLength(P - 1);
  8376. }
  8377. FNotes->Add(FORMAT(L"[%s] %s",
  8378. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8379. while (FNotes->Count > 10)
  8380. {
  8381. FNotes->Delete(0);
  8382. }
  8383. if (UpdateNow)
  8384. {
  8385. FNoteHints = FNotes->Text;
  8386. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8387. UpdateStatusBar();
  8388. }
  8389. }
  8390. //---------------------------------------------------------------------------
  8391. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8392. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8393. {
  8394. int P = Note.Pos(L"\n");
  8395. if (P > 0)
  8396. {
  8397. Note.SetLength(P - 1);
  8398. }
  8399. FNoteHints = FNotes->Text;
  8400. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8401. FNote = Note;
  8402. SAFE_DESTROY(FNoteData);
  8403. FNoteData = NoteData;
  8404. FOnNoteClick = OnNoteClick;
  8405. AddNote(Note, false);
  8406. UpdateStatusBar();
  8407. FNoteShown = Now();
  8408. FNoteTimer->Enabled = false;
  8409. if (Seconds == 0)
  8410. {
  8411. Seconds = WinConfiguration->NotificationsTimeout;
  8412. }
  8413. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8414. FNoteTimer->Enabled = true;
  8415. }
  8416. //---------------------------------------------------------------------------
  8417. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8418. {
  8419. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8420. }
  8421. //---------------------------------------------------------------------------
  8422. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8423. {
  8424. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  8425. {
  8426. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8427. }
  8428. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  8429. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  8430. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  8431. 0, NULL, NULL);
  8432. }
  8433. //---------------------------------------------------------------------------
  8434. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  8435. {
  8436. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  8437. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  8438. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  8439. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  8440. }
  8441. //---------------------------------------------------------------------------
  8442. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  8443. {
  8444. WinConfiguration->FormatSizeBytes = Style;
  8445. }
  8446. //---------------------------------------------------------------------------
  8447. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  8448. {
  8449. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  8450. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  8451. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  8452. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  8453. }
  8454. //---------------------------------------------------------------------------
  8455. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  8456. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  8457. {
  8458. if (Panel->Index == 0)
  8459. {
  8460. if (FOnNoteClick != NULL)
  8461. {
  8462. // prevent the user data from being freed by possible call
  8463. // to CancelNote or PostNote during call to OnNoteClick
  8464. std::unique_ptr<TObject> NoteData(FNoteData);
  8465. TNotifyEvent OnNoteClick = FOnNoteClick;
  8466. FNoteData = NULL;
  8467. // need to cancel the note as we are going to delete its user data
  8468. CancelNote(true);
  8469. OnNoteClick(NoteData.get());
  8470. }
  8471. }
  8472. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  8473. {
  8474. FileSystemInfo();
  8475. }
  8476. }
  8477. //---------------------------------------------------------------------------
  8478. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  8479. {
  8480. // workaround:
  8481. // 1) for unknown reason, disabling window, while minimized,
  8482. // prevents it from restoring, even if it was enabled again meanwhile
  8483. // 2) when disabling the main window, while another has focus
  8484. // minimize is no longer possible
  8485. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  8486. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  8487. // Shouldn't we use IsApplicationMinimized() here?
  8488. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  8489. {
  8490. Enabled = false;
  8491. }
  8492. FLockLevel++;
  8493. }
  8494. //---------------------------------------------------------------------------
  8495. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  8496. {
  8497. DebugAssert(FLockLevel > 0);
  8498. FLockLevel--;
  8499. if (FLockLevel == 0)
  8500. {
  8501. DebugAssert(FLockSuspendLevel == 0);
  8502. Enabled = true;
  8503. }
  8504. }
  8505. //---------------------------------------------------------------------------
  8506. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  8507. {
  8508. // We need to make sure that window is enabled when the last modal window closes
  8509. // otherwise focus is not restored correctly.
  8510. // So we re-enable the window when modal window opens and
  8511. // disable it back after is closes
  8512. if (FLockLevel > 0)
  8513. {
  8514. // while we have nesting counter, we know that we never be called
  8515. // recursivelly as Application->OnModalBegin is called only
  8516. // for the top-level modal window
  8517. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8518. {
  8519. // won't be disabled when conditions in LockWindow() were not satisfied
  8520. FDisabledOnLockSuspend = !Enabled;
  8521. // When minimized to tray (or actually when set to SW_HIDE),
  8522. // setting Enabled makes the window focusable even when there's
  8523. // modal window over it
  8524. if (!FTrayIcon->Visible)
  8525. {
  8526. Enabled = true;
  8527. }
  8528. }
  8529. FLockSuspendLevel++;
  8530. }
  8531. }
  8532. //---------------------------------------------------------------------------
  8533. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  8534. {
  8535. if (FLockSuspendLevel > 0)
  8536. {
  8537. DebugAssert(FLockLevel > 0);
  8538. FLockSuspendLevel--;
  8539. // see comment in SuspendWindowLock
  8540. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8541. {
  8542. // Note that window can be enabled here, when we were minized to tray when
  8543. // was SuspendWindowLock() called.
  8544. // We should possibly do the same check as in LockWindow(),
  8545. // if it is ever possible that the consitions change between
  8546. // SuspendWindowLock() and ResumeWindowLock()
  8547. Enabled = !FDisabledOnLockSuspend;
  8548. }
  8549. }
  8550. }
  8551. //---------------------------------------------------------------------------
  8552. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  8553. {
  8554. if (!TextOnly)
  8555. {
  8556. TTBXComboBoxItem * RemotePathComboBox =
  8557. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  8558. TStrings * Items = RemotePathComboBox->Strings;
  8559. Items->BeginUpdate();
  8560. try
  8561. {
  8562. Items->Clear();
  8563. if (Terminal != NULL)
  8564. {
  8565. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8566. while (!IsUnixRootPath(APath))
  8567. {
  8568. int P = APath.LastDelimiter(L'/');
  8569. DebugAssert(P >= 0);
  8570. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  8571. APath.SetLength(P - 1);
  8572. }
  8573. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  8574. }
  8575. }
  8576. __finally
  8577. {
  8578. RemotePathComboBox->ItemIndex = Items->Count - 1;
  8579. // Setting ItemIndex to -1 does not reset its text
  8580. if (Items->Count == 0)
  8581. {
  8582. RemotePathComboBox->Text = L"";
  8583. }
  8584. Items->EndUpdate();
  8585. }
  8586. }
  8587. }
  8588. //---------------------------------------------------------------------------
  8589. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  8590. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  8591. int & ImageIndex)
  8592. {
  8593. if (AIndex < 0)
  8594. {
  8595. AIndex = Sender->ItemIndex;
  8596. }
  8597. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  8598. }
  8599. //---------------------------------------------------------------------------
  8600. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  8601. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  8602. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  8603. {
  8604. ARect.Left += (10 * AIndex);
  8605. }
  8606. //---------------------------------------------------------------------------
  8607. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  8608. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  8609. {
  8610. AWidth += (10 * AIndex);
  8611. }
  8612. //---------------------------------------------------------------------------
  8613. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  8614. TObject * Sender)
  8615. {
  8616. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  8617. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8618. int Index = RemotePathComboBox->ItemIndex;
  8619. while (Index < RemotePathComboBox->Strings->Count - 1)
  8620. {
  8621. APath = UnixExtractFileDir(APath);
  8622. Index++;
  8623. }
  8624. // VanDyke style paths
  8625. if (APath.IsEmpty())
  8626. {
  8627. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  8628. APath = ROOTDIRECTORY;
  8629. }
  8630. if (RemoteDirView->Path != APath)
  8631. {
  8632. RemoteDirView->Path = APath;
  8633. }
  8634. }
  8635. //---------------------------------------------------------------------------
  8636. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  8637. {
  8638. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  8639. DebugUsedParam(Sender);
  8640. UpdateRemotePathComboBox(true);
  8641. }
  8642. //---------------------------------------------------------------------------
  8643. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  8644. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  8645. {
  8646. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8647. DebugAssert(DirView != NULL);
  8648. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  8649. {
  8650. HWND Edit = ListView_GetEditControl(DirView->Handle);
  8651. // OnEditing is called also from TCustomListView::CanEdit
  8652. if (Edit != NULL)
  8653. {
  8654. EditSelectBaseName(Edit);
  8655. }
  8656. }
  8657. }
  8658. //---------------------------------------------------------------------------
  8659. //---------------------------------------------------------------------------
  8660. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  8661. {
  8662. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  8663. Result->AutoDetectDnD = false;
  8664. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  8665. Result->RenderDataOn = rdoEnterAndDropSync;
  8666. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  8667. return Result;
  8668. }
  8669. //---------------------------------------------------------------------------
  8670. void __fastcall TCustomScpExplorerForm::CreateWnd()
  8671. {
  8672. TForm::CreateWnd();
  8673. if (FSessionsDragDropFilesEx == NULL)
  8674. {
  8675. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  8676. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  8677. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  8678. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  8679. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  8680. }
  8681. if (FQueueDragDropFilesEx == NULL)
  8682. {
  8683. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  8684. // No need to set OnDragOver as we do not have any restrictions
  8685. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  8686. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  8687. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  8688. }
  8689. }
  8690. //---------------------------------------------------------------------------
  8691. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  8692. {
  8693. TForm::DestroyWnd();
  8694. FSessionsDragDropFilesEx->DragDropControl = NULL;
  8695. FQueueDragDropFilesEx->DragDropControl = NULL;
  8696. }
  8697. //---------------------------------------------------------------------------
  8698. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  8699. {
  8700. SideEnter(FCurrentSide);
  8701. FEverShown = true;
  8702. }
  8703. //---------------------------------------------------------------------------
  8704. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  8705. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  8706. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  8707. {
  8708. if (!NonVisualDataModule->Busy)
  8709. {
  8710. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8711. Configuration->Usage->Inc(L"FileFinds");
  8712. LockWindow(true);
  8713. NonVisualDataModule->StartBusy();
  8714. WinConfiguration->LockedInterface = true;
  8715. try
  8716. {
  8717. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  8718. }
  8719. __finally
  8720. {
  8721. WinConfiguration->LockedInterface = false;
  8722. NonVisualDataModule->EndBusy();
  8723. UnlockWindow();
  8724. }
  8725. }
  8726. }
  8727. //---------------------------------------------------------------------------
  8728. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  8729. {
  8730. if (!NonVisualDataModule->Busy)
  8731. {
  8732. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8733. SetFocus();
  8734. RemoteDirView->Path = UnixExtractFilePath(Path);
  8735. TListItem * Item = RemoteDirView->FindFileItem(UnixExtractFileName(Path));
  8736. if (Item != NULL)
  8737. {
  8738. RemoteDirView->ItemFocused = Item;
  8739. RemoteDirView->ItemFocused->MakeVisible(false);
  8740. RemoteDirView->SetFocus();
  8741. }
  8742. }
  8743. }
  8744. //---------------------------------------------------------------------------
  8745. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  8746. {
  8747. bool Result = !NonVisualDataModule->Busy;
  8748. if (Result)
  8749. {
  8750. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8751. }
  8752. return Result;
  8753. }
  8754. //---------------------------------------------------------------------------
  8755. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  8756. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8757. {
  8758. if (CanOperateOnFoundFiles(ATerminal))
  8759. {
  8760. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  8761. FOnFileOperationFinished = OnFileOperationFinished;
  8762. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  8763. }
  8764. }
  8765. //---------------------------------------------------------------------------
  8766. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  8767. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8768. {
  8769. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  8770. }
  8771. //---------------------------------------------------------------------------
  8772. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  8773. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8774. {
  8775. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  8776. }
  8777. //---------------------------------------------------------------------------
  8778. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  8779. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8780. {
  8781. if (CanOperateOnFoundFiles(ATerminal))
  8782. {
  8783. for (int Index = 0; Index < FileList->Count; Index++)
  8784. {
  8785. UnicodeString FileName = FileList->Strings[Index];
  8786. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  8787. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  8788. OnFileOperationFinished(FileName, true);
  8789. }
  8790. }
  8791. }
  8792. //---------------------------------------------------------------------------
  8793. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  8794. {
  8795. FFileFindTerminal = Terminal;
  8796. ShowFileFindDialog(
  8797. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  8798. DoDownloadFoundFiles, DoEditFoundFiles);
  8799. }
  8800. //---------------------------------------------------------------------------
  8801. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  8802. {
  8803. FTaskbarList = TaskbarList;
  8804. }
  8805. //---------------------------------------------------------------------------
  8806. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  8807. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  8808. {
  8809. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8810. if (Index >= 0)
  8811. {
  8812. if (Button == mbLeft)
  8813. {
  8814. // "Mouse down" is raised only after tab is switched.
  8815. // If switching tab (switching session) takes long enough for user
  8816. // to actually release the button, "mouse down" is still raised,
  8817. // but we do not get "mouse up" event, so dragging is not cancelled,
  8818. // prevent that by not beginning dragging in the first place.
  8819. if (IsKeyPressed(VK_LBUTTON))
  8820. {
  8821. // when user clicks the "+", we get mouse down only after the session
  8822. // is closed, when new session tab is already on X:Y, so dragging
  8823. // starts, prevent that
  8824. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  8825. {
  8826. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8827. if (Terminal != NULL)
  8828. {
  8829. SessionsPageControl->BeginDrag(false);
  8830. }
  8831. }
  8832. }
  8833. }
  8834. else if (Button == mbMiddle)
  8835. {
  8836. // ignore middle-click for "New session tab"
  8837. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8838. if (Terminal != NULL)
  8839. {
  8840. SessionsPageControl->ActivePageIndex = Index;
  8841. // Switch to session tab (has to be session tab, due to previous check)
  8842. if (DebugAlwaysTrue(SessionTabSwitched()))
  8843. {
  8844. CloseSession();
  8845. }
  8846. }
  8847. }
  8848. }
  8849. }
  8850. //---------------------------------------------------------------------------
  8851. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  8852. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  8853. {
  8854. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8855. // do not allow dropping on the "+" tab
  8856. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8857. if ((TargetTerminal != NULL) &&
  8858. (SessionsPageControl->ActivePage->PageIndex != Index))
  8859. {
  8860. Configuration->Usage->Inc(L"SessionTabMoves");
  8861. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  8862. // but we want to actually prevent that to avoid flicker
  8863. SessionsPageControl->ActivePage->PageIndex = Index;
  8864. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  8865. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  8866. }
  8867. }
  8868. //---------------------------------------------------------------------------
  8869. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  8870. TObject * Sender, TObject * Source, int X, int Y,
  8871. TDragState /*State*/, bool & Accept)
  8872. {
  8873. Accept = (Sender == Source);
  8874. if (Accept)
  8875. {
  8876. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8877. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8878. // do not allow dragging to the "+" tab
  8879. Accept = (Terminal != NULL);
  8880. }
  8881. }
  8882. //---------------------------------------------------------------------------
  8883. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(int /*KeyState*/,
  8884. const TPoint & Point, int & Effect)
  8885. {
  8886. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  8887. if (Index < 0)
  8888. {
  8889. Effect = DROPEFFECT_None;
  8890. }
  8891. else
  8892. {
  8893. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8894. // do not allow dropping on the "+" tab
  8895. if (TargetTerminal == NULL)
  8896. {
  8897. Effect = DROPEFFECT_None;
  8898. }
  8899. }
  8900. }
  8901. //---------------------------------------------------------------------------
  8902. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  8903. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  8904. {
  8905. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  8906. // do not allow dropping on the "+" tab
  8907. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8908. if (TargetTerminal != NULL)
  8909. {
  8910. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  8911. if (!IsFileControl(DropSourceControl, osRemote))
  8912. {
  8913. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  8914. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  8915. // Why don't we use Terminal->CurrentDirectory directly?
  8916. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory, false);
  8917. }
  8918. }
  8919. }
  8920. //---------------------------------------------------------------------------
  8921. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  8922. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  8923. {
  8924. // Downloads are handled in RemoteFileControlDDEnd
  8925. if (!IsFileControl(DropSourceControl, osRemote))
  8926. {
  8927. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  8928. Terminal->CurrentDirectory,
  8929. // force queue
  8930. true);
  8931. }
  8932. }
  8933. //---------------------------------------------------------------------------
  8934. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  8935. {
  8936. FShowing = false;
  8937. // Do not save empty workspace
  8938. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  8939. {
  8940. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  8941. UnicodeString Name = WorkspaceName();
  8942. DoSaveWorkspace(
  8943. Name, DataList.get(),
  8944. !Configuration->DisablePasswordStoring &&
  8945. WinConfiguration->AutoSaveWorkspacePasswords, false);
  8946. WinConfiguration->LastStoredSession = Name;
  8947. }
  8948. }
  8949. //---------------------------------------------------------------------------
  8950. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  8951. {
  8952. TManagedTerminal * ManagedTerminal =
  8953. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  8954. if (ManagedTerminal != NULL)
  8955. {
  8956. ManagedTerminal->DirectoryLoaded = Now();
  8957. }
  8958. }
  8959. //---------------------------------------------------------------------------
  8960. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  8961. TListItem * /*Item*/, bool /*Selected*/)
  8962. {
  8963. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8964. switch (DirView->LastSelectMethod)
  8965. {
  8966. case smKeyboard:
  8967. Configuration->Usage->Inc(L"KeyboardSelections");
  8968. break;
  8969. case smMouse:
  8970. Configuration->Usage->Inc(L"MouseSelections");
  8971. break;
  8972. }
  8973. }
  8974. //---------------------------------------------------------------------------
  8975. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  8976. {
  8977. if (Color != 0)
  8978. {
  8979. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  8980. }
  8981. else
  8982. {
  8983. return FSessionTabImageIndex;
  8984. }
  8985. }
  8986. //---------------------------------------------------------------------------
  8987. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  8988. {
  8989. TPngImageCollectionItem * Item =
  8990. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  8991. FSessionColors->AddPng(Item->PngImage);
  8992. return FSessionColors->Count - 1;
  8993. }
  8994. //---------------------------------------------------------------------------
  8995. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  8996. {
  8997. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  8998. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  8999. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  9000. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  9001. }
  9002. //---------------------------------------------------------------------------
  9003. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9004. {
  9005. for (int Index = 0; Index < Control->ControlCount; Index++)
  9006. {
  9007. TControl * AControl = Control->Controls[Index];
  9008. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9009. if (WinControl != NULL)
  9010. {
  9011. CollectItemsWithTextDisplayMode(WinControl);
  9012. }
  9013. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9014. if (Toolbar != NULL)
  9015. {
  9016. // we care for top-level items only
  9017. TTBCustomItem * Items = Toolbar->Items;
  9018. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9019. {
  9020. TTBCustomItem * Item = Items->Items[ItemIndex];
  9021. if (((Item->DisplayMode == nbdmImageAndText) ||
  9022. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9023. EligibleForImageDisplayMode(Item))
  9024. {
  9025. FItemsWithTextDisplayMode.insert(Item);
  9026. }
  9027. }
  9028. }
  9029. }
  9030. }
  9031. //---------------------------------------------------------------------------
  9032. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9033. {
  9034. return true;
  9035. }
  9036. //---------------------------------------------------------------------------
  9037. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9038. {
  9039. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9040. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9041. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9042. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9043. bool Result = true;
  9044. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9045. {
  9046. TTBCustomItem * Item = *i;
  9047. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9048. if (Label != NULL)
  9049. {
  9050. // optimization
  9051. if (Label->Visible == SelectiveToolbarText)
  9052. {
  9053. Result = false;
  9054. }
  9055. else
  9056. {
  9057. Label->Visible = SelectiveToolbarText;
  9058. }
  9059. }
  9060. else
  9061. {
  9062. // optimization
  9063. if (Item->DisplayMode == DisplayMode)
  9064. {
  9065. Result = false;
  9066. }
  9067. else
  9068. {
  9069. Item->DisplayMode = DisplayMode;
  9070. }
  9071. }
  9072. ++i;
  9073. }
  9074. if (Result)
  9075. {
  9076. UpdateNewSessionTab();
  9077. }
  9078. return Result;
  9079. }
  9080. //---------------------------------------------------------------------------
  9081. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9082. {
  9083. DebugFail();
  9084. }
  9085. //---------------------------------------------------------------------------
  9086. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9087. {
  9088. // Among other, a lock level is non zero, while directory is being loaded,
  9089. // even when it happens as a result of dirview navigation,
  9090. // i.e. when TNonVisualDataModule is NOT FBusy.
  9091. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9092. // Among other this prevents a panel auto update to occur while
  9093. // directory is changing.
  9094. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9095. }
  9096. //---------------------------------------------------------------------------
  9097. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  9098. {
  9099. // While the window is disabled, we still seem to process menu shortcuts at least,
  9100. // so stop it at least here.
  9101. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9102. return
  9103. (Allowed == aaUpdate) ||
  9104. !NonVisualDataModule->Busy;
  9105. }
  9106. //---------------------------------------------------------------------------
  9107. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9108. {
  9109. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9110. }
  9111. //---------------------------------------------------------------------------
  9112. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9113. {
  9114. // This is somewhat redundant to LockWindow() call from
  9115. // TTerminalManager::TerminalReadDirectoryProgress.
  9116. // But disabling window is known not to block keyboard shorcuts
  9117. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9118. if (Busy > 0)
  9119. {
  9120. if (NonVisualDataModule->Busy)
  9121. {
  9122. State = false;
  9123. }
  9124. else
  9125. {
  9126. NonVisualDataModule->StartBusy();
  9127. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9128. LockWindow();
  9129. }
  9130. }
  9131. else if (Busy < 0)
  9132. {
  9133. UnlockWindow();
  9134. NonVisualDataModule->EndBusy();
  9135. }
  9136. else
  9137. {
  9138. State = NonVisualDataModule->Busy;
  9139. }
  9140. }
  9141. //---------------------------------------------------------------------------
  9142. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9143. {
  9144. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9145. // no context menu for "New session tab"
  9146. if ((Index >= 0) && (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != NULL))
  9147. {
  9148. SessionsPageControl->ActivePageIndex = Index;
  9149. if (DebugAlwaysTrue(SessionTabSwitched()))
  9150. {
  9151. // copied from TControl.WMContextMenu
  9152. SendCancelMode(SessionsPageControl);
  9153. // explicit popup instead of using PopupMenu property
  9154. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9155. // while connecting yet not-connected session and hence
  9156. // allowing an access to commands over not-completelly connected session
  9157. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9158. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  9159. PopupMenu->PopupComponent = SessionsPageControl;
  9160. PopupMenu->Popup(Point.x, Point.y);
  9161. }
  9162. }
  9163. Handled = true;
  9164. }
  9165. //---------------------------------------------------------------------------
  9166. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9167. {
  9168. return (Terminal != NULL) && Terminal->Active && Terminal->IsCapable[fcChangePassword];
  9169. }
  9170. //---------------------------------------------------------------------------
  9171. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9172. {
  9173. Configuration->Usage->Inc(L"PasswordChanges");
  9174. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9175. ChangePasswordData->DisableAuthentationsExceptPassword();
  9176. ChangePasswordData->ChangePassword = true;
  9177. std::unique_ptr<TTerminal> ChangePasswordSession(
  9178. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9179. ChangePasswordSession->AutoReadDirectory = false;
  9180. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9181. {
  9182. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9183. }
  9184. else
  9185. {
  9186. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9187. }
  9188. }
  9189. //---------------------------------------------------------------------------
  9190. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9191. {
  9192. // No nice way to assert SSH2
  9193. return (Terminal != NULL) && Terminal->Active && (Terminal->FSProtocol == cfsSFTP);
  9194. }
  9195. //---------------------------------------------------------------------------
  9196. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9197. {
  9198. TOperationVisualizer OperationVisualizer;
  9199. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9200. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9201. }
  9202. //---------------------------------------------------------------------------
  9203. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9204. {
  9205. TForm::ChangeScale(M, D);
  9206. int APixelsPerInch = GetControlPixelsPerInch(this);
  9207. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9208. try
  9209. {
  9210. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9211. }
  9212. __finally
  9213. {
  9214. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9215. }
  9216. }
  9217. //---------------------------------------------------------------------------
  9218. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9219. {
  9220. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9221. }
  9222. //---------------------------------------------------------------------
  9223. class TPublicControl : public TControl
  9224. {
  9225. friend class TCustomScpExplorerForm;
  9226. };
  9227. //---------------------------------------------------------------------------
  9228. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9229. {
  9230. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9231. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9232. }
  9233. //---------------------------------------------------------------------------
  9234. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9235. {
  9236. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9237. OpenBookmark(Side, Bookmark);
  9238. }
  9239. //---------------------------------------------------------------------------
  9240. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9241. {
  9242. DoBookmarkClick(osLocal, Sender);
  9243. }
  9244. //---------------------------------------------------------------------------
  9245. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9246. {
  9247. DoBookmarkClick(osRemote, Sender);
  9248. }
  9249. //---------------------------------------------------------------------------
  9250. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9251. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9252. {
  9253. TNotifyEvent OnBookmarkClick = (Side == osLocal) ? &LocalBookmarkClick : &RemoteBookmarkClick;
  9254. if (!WinConfiguration->UseLocationProfiles)
  9255. {
  9256. std::unique_ptr<TStringList> Directories(new TStringList());
  9257. Directories->CaseSensitive = (Side == osRemote);
  9258. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9259. {
  9260. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9261. UnicodeString Directory = Bookmark->GetSideDirectory(Side);
  9262. if (!Directory.IsEmpty() && (Directories->IndexOf(Directory) < 0))
  9263. {
  9264. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9265. Item->Caption = EscapeHotkey(Directory);
  9266. Item->ShortCut = Bookmark->ShortCut;
  9267. Item->OnClick = OnBookmarkClick;
  9268. Item->Tag = reinterpret_cast<int>(Bookmark);
  9269. Directories->Add(Directory);
  9270. Menu->Add(Item.release());
  9271. }
  9272. }
  9273. }
  9274. else
  9275. {
  9276. std::unique_ptr<TStrings> Folders(CreateSortedStringList());
  9277. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9278. {
  9279. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9280. if (!Bookmark->Node.IsEmpty())
  9281. {
  9282. Folders->Add(Bookmark->Node);
  9283. }
  9284. }
  9285. for (int Index = 0; Index < Folders->Count; Index++)
  9286. {
  9287. std::unique_ptr<TTBCustomItem> Item(new TTBXSubmenuItem(Owner));
  9288. Item->Caption = Folders->Strings[Index];
  9289. Item->ImageIndex = NonVisualDataModule->RemoteChangePathAction->ImageIndex;
  9290. Folders->Objects[Index] = Item.get();
  9291. Menu->Add(Item.release());
  9292. }
  9293. for (int Index = 0; Index < BookmarkList->Count; Index++)
  9294. {
  9295. TBookmark * Bookmark = BookmarkList->Bookmarks[Index];
  9296. TTBCustomItem * Parent;
  9297. if (!Bookmark->Node.IsEmpty())
  9298. {
  9299. DebugAssert(Folders->IndexOf(Bookmark->Node) >= 0);
  9300. Parent = dynamic_cast<TTBCustomItem *>(Folders->Objects[Folders->IndexOf(Bookmark->Node)]);
  9301. }
  9302. else
  9303. {
  9304. Parent = Menu;
  9305. }
  9306. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9307. Item->Caption = Bookmark->Name;
  9308. Item->ShortCut = Bookmark->ShortCut;
  9309. Item->OnClick = OnBookmarkClick;
  9310. Item->Tag = reinterpret_cast<int>(Bookmark);
  9311. if (((Bookmark->Name == Bookmark->Local) || (Bookmark->Name == Bookmark->Remote)) &&
  9312. (Bookmark->Local.IsEmpty() || Bookmark->Remote.IsEmpty()))
  9313. {
  9314. // No hint for location profiles that are actually mere "bookmarks"
  9315. }
  9316. else
  9317. {
  9318. UnicodeString Hint = FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 1), (Bookmark->Name));
  9319. UnicodeString LongHint;
  9320. if (!Bookmark->Local.IsEmpty())
  9321. {
  9322. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 2), (Bookmark->Local)), L"\n");
  9323. }
  9324. if (!Bookmark->Remote.IsEmpty())
  9325. {
  9326. AddToList(LongHint, FORMAT(LoadStrPart(LOCATION_PROFILE_HINT, 3), (Bookmark->Remote)), L"\n");
  9327. }
  9328. AddToList(Hint, LongHint, L"|");
  9329. Item->Hint = Hint;
  9330. }
  9331. Parent->Add(Item.release());
  9332. }
  9333. }
  9334. }
  9335. //---------------------------------------------------------------------------
  9336. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  9337. {
  9338. Menu->Clear();
  9339. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  9340. AddMenuSeparator(Menu);
  9341. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  9342. AddMenuSeparator(Menu);
  9343. std::unique_ptr<TTBCustomItem> Item(new TTBXItem(Owner));
  9344. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction : NonVisualDataModule->RemoteAddBookmarkAction;
  9345. Menu->Add(Item.release());
  9346. }
  9347. //---------------------------------------------------------------------------
  9348. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  9349. {
  9350. bool Result = true;
  9351. try
  9352. {
  9353. if (Side == osRemote)
  9354. {
  9355. Terminal->ExceptionOnFail = true;
  9356. }
  9357. try
  9358. {
  9359. DirView(Side)->Path = Path;
  9360. }
  9361. __finally
  9362. {
  9363. if (Side == osRemote)
  9364. {
  9365. Terminal->ExceptionOnFail = false;
  9366. }
  9367. }
  9368. }
  9369. catch (Exception & E)
  9370. {
  9371. if ((Side != osRemote) || Terminal->Active)
  9372. {
  9373. ShowExtendedExceptionEx(Terminal, &E);
  9374. Result = false;
  9375. }
  9376. else
  9377. {
  9378. throw;
  9379. }
  9380. }
  9381. return Result;
  9382. }
  9383. //---------------------------------------------------------------------------
  9384. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  9385. {
  9386. // Make sure there some feedback even when loading tine local folders
  9387. TInstantOperationVisualizer Visualizer;
  9388. DirView(Side)->ReloadDirectory();
  9389. }
  9390. //---------------------------------------------------------------------------
  9391. void __fastcall TCustomScpExplorerForm::SessionsPageControlCloseButtonClick(TPageControl * /*Sender*/, int Index)
  9392. {
  9393. if (Index == SessionsPageControl->TabIndex)
  9394. {
  9395. CloseSession();
  9396. }
  9397. else
  9398. {
  9399. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  9400. TTerminal * Terminal = GetSessionTabTerminal(TabSheet);
  9401. TTerminalManager * Manager = TTerminalManager::Instance();
  9402. if (!Terminal->Active)
  9403. {
  9404. Manager->FreeTerminal(Terminal);
  9405. }
  9406. else
  9407. {
  9408. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  9409. if (CanCloseQueue(Queue))
  9410. {
  9411. Manager->FreeTerminal(Terminal);
  9412. }
  9413. }
  9414. }
  9415. }
  9416. //---------------------------------------------------------------------------
  9417. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side)
  9418. {
  9419. if (DebugAlwaysTrue(GetSide(Side) == osRemote))
  9420. {
  9421. TInstantOperationVisualizer Visualizer;
  9422. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  9423. // before we create a new data object
  9424. ClipboardClear();
  9425. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  9426. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  9427. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  9428. FClipboardFakeMonitors.reset(StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated));
  9429. if (FClipboardDragDropFilesEx.get() == NULL)
  9430. {
  9431. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  9432. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  9433. }
  9434. FClipboardDragDropFilesEx->FileList->Clear();
  9435. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  9436. FClipboardDragDropFilesEx->CopyToClipboard();
  9437. FClipboardTerminal = FTerminal;
  9438. // Need full paths, as cwd can be different once files are pasted
  9439. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(false, true)));
  9440. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  9441. {
  9442. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  9443. }
  9444. }
  9445. }
  9446. //---------------------------------------------------------------------------
  9447. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  9448. {
  9449. if (OpenClipboard(0))
  9450. {
  9451. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  9452. CloseClipboard();
  9453. }
  9454. }
  9455. //---------------------------------------------------------------------------
  9456. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  9457. {
  9458. return !FClipboardFakeDirectory.IsEmpty();
  9459. }
  9460. //---------------------------------------------------------------------------
  9461. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  9462. {
  9463. RemoveDir(ApiPath(FClipboardFakeDirectory));
  9464. FClipboardFakeDirectory = UnicodeString();
  9465. FClipboardTerminal = NULL;
  9466. // Also called in Idle()
  9467. FClipboardFakeMonitors.reset(NULL);
  9468. }
  9469. //---------------------------------------------------------------------------
  9470. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  9471. {
  9472. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  9473. {
  9474. ClipboardStop();
  9475. }
  9476. }
  9477. //---------------------------------------------------------------------------
  9478. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  9479. {
  9480. TTerminalManager * Manager = TTerminalManager::Instance();
  9481. Manager->ActiveTerminal = FClipboardTerminal;
  9482. TTransferOperationParam Params;
  9483. Params.TargetDirectory = TargetDirectory;
  9484. Params.DragDrop = DragDrop;
  9485. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  9486. }
  9487. //---------------------------------------------------------------------------
  9488. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  9489. {
  9490. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  9491. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  9492. {
  9493. RemoveDir(ApiPath(FileName));
  9494. if (!NonVisualDataModule->Busy)
  9495. {
  9496. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  9497. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  9498. }
  9499. }
  9500. }
  9501. //---------------------------------------------------------------------------