CustomScpExplorer.cpp 331 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136
  1. //---------------------------------------------------------------------------
  2. #define NO_WIN32_LEAN_AND_MEAN
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include <Common.h>
  6. #include "CustomScpExplorer.h"
  7. #include <Bookmarks.h>
  8. #include <Interface.h>
  9. #include <Exceptions.h>
  10. #include <CoreMain.h>
  11. #include <FileSystems.h>
  12. #include <TextsCore.h>
  13. #include <TextsWin.h>
  14. #include <HelpWin.h>
  15. #include <VCLCommon.h>
  16. #include <Progress.h>
  17. #include <SynchronizeProgress.h>
  18. #include <DragExt.h>
  19. #include <WinApi.h>
  20. #include "GUITools.h"
  21. #include "NonVisual.h"
  22. #include "Glyphs.h"
  23. #include "Tools.h"
  24. #include "WinConfiguration.h"
  25. #include "TerminalManager.h"
  26. #include "EditorManager.h"
  27. #include "ProgParams.h"
  28. #include "Setup.h"
  29. #include <Consts.hpp>
  30. #include <DateUtils.hpp>
  31. #include <TB2Common.hpp>
  32. #include <DirectoryMonitor.hpp>
  33. //---------------------------------------------------------------------------
  34. #pragma package(smart_init)
  35. #pragma link "CustomDirView"
  36. #pragma link "CustomUnixDirView"
  37. #pragma link "IEListView"
  38. #pragma link "NortonLikeListView"
  39. #pragma link "UnixDirView"
  40. #pragma link "CustomDriveView"
  41. #pragma link "UnixDriveView"
  42. #pragma link "CustomDriveView"
  43. #pragma link "TB2Dock"
  44. #pragma link "TBXStatusBars"
  45. #pragma link "TB2Item"
  46. #pragma link "TB2Toolbar"
  47. #pragma link "ThemePageControl"
  48. #pragma link "PathLabel"
  49. #ifndef NO_RESOURCES
  50. #pragma resource "*.dfm"
  51. #endif
  52. //---------------------------------------------------------------------------
  53. #define SAVE_SELECTION(DIRVIEW) \
  54. UnicodeString FocusFile = L""; \
  55. UnicodeString LastFocusedFile = L""; \
  56. if (DIRVIEW->ItemFocused) LastFocusedFile = DIRVIEW->ItemFocused->Caption; \
  57. { TListItem * ClosestUnselected = DIRVIEW->ClosestUnselected(DIRVIEW->ItemFocused); \
  58. if (ClosestUnselected) FocusFile = ClosestUnselected->Caption; }
  59. #define RESTORE_SELECTION(DIRVIEW) \
  60. if (!LastFocusedFile.IsEmpty() && \
  61. (!DIRVIEW->ItemFocused || (DIRVIEW->ItemFocused->Caption != LastFocusedFile))) \
  62. { \
  63. TListItem *ItemToSelect = DIRVIEW->FindFileItem(FocusFile); \
  64. if (ItemToSelect) \
  65. { \
  66. DIRVIEW->ItemFocused = ItemToSelect; \
  67. DIRVIEW->ItemFocused->MakeVisible(False); \
  68. } \
  69. }
  70. //---------------------------------------------------------------------------
  71. #define WM_COMPONENT_HIDE (WM_WINSCP_USER + 4)
  72. static const int SessionPanelCount = 4;
  73. //---------------------------------------------------------------------------
  74. class TMutexGuard
  75. {
  76. public:
  77. TMutexGuard(HANDLE AMutex, int Message = MUTEX_RELEASE_TIMEOUT,
  78. int Timeout = 5000)
  79. {
  80. FMutex = NULL;
  81. unsigned long WaitResult = WaitForSingleObject(AMutex, Timeout);
  82. if (WaitResult == WAIT_TIMEOUT)
  83. {
  84. throw Exception(LoadStr(MUTEX_RELEASE_TIMEOUT));
  85. }
  86. else
  87. {
  88. FMutex = AMutex;
  89. }
  90. }
  91. ~TMutexGuard()
  92. {
  93. if (FMutex != NULL)
  94. {
  95. ReleaseMutex(FMutex);
  96. }
  97. }
  98. private:
  99. HANDLE FMutex;
  100. };
  101. //---------------------------------------------------------------------------
  102. class TWindowLock
  103. {
  104. public:
  105. TWindowLock(TCustomScpExplorerForm * Form) :
  106. FForm(Form)
  107. {
  108. FForm->LockWindow();
  109. }
  110. ~TWindowLock()
  111. {
  112. FForm->UnlockWindow();
  113. }
  114. private:
  115. TCustomScpExplorerForm * FForm;
  116. };
  117. //---------------------------------------------------------------------------
  118. class TAutoBatch
  119. {
  120. public:
  121. TAutoBatch(TCustomScpExplorerForm * Form) :
  122. FForm(Form)
  123. {
  124. FForm->BatchStart(FBatchStorage);
  125. }
  126. ~TAutoBatch()
  127. {
  128. FForm->BatchEnd(FBatchStorage);
  129. }
  130. private:
  131. TCustomScpExplorerForm * FForm;
  132. void * FBatchStorage;
  133. };
  134. //---------------------------------------------------------------------------
  135. struct TTransferOperationParam
  136. {
  137. TTransferOperationParam();
  138. UnicodeString TargetDirectory;
  139. bool Temp;
  140. bool DragDrop;
  141. int Options;
  142. TAutoSwitch Queue;
  143. };
  144. //---------------------------------------------------------------------------
  145. TTransferOperationParam::TTransferOperationParam()
  146. {
  147. Temp = false;
  148. DragDrop = false;
  149. Options = 0;
  150. Queue = asAuto;
  151. }
  152. //---------------------------------------------------------------------------
  153. class TTransferPresetNoteData : public TObject
  154. {
  155. public:
  156. UnicodeString Message;
  157. };
  158. //---------------------------------------------------------------------------
  159. class TTerminalNoteData : public TObject
  160. {
  161. public:
  162. TTerminal * Terminal;
  163. };
  164. //---------------------------------------------------------------------------
  165. __fastcall TCustomScpExplorerForm::TCustomScpExplorerForm(TComponent* Owner):
  166. FFormRestored(false),
  167. TForm(Owner)
  168. {
  169. FCurrentSide = osRemote;
  170. FEverShown = false;
  171. FDocks = new TList();
  172. RestoreParams();
  173. ConfigurationChanged();
  174. RemoteDirView->Invalidate();
  175. DebugAssert(NonVisualDataModule && !NonVisualDataModule->ScpExplorer);
  176. NonVisualDataModule->ScpExplorer = this;
  177. FAutoOperation = false;
  178. FOnFileOperationFinished = NULL;
  179. FForceExecution = false;
  180. FIgnoreNextDialogChar = 0;
  181. FErrorList = NULL;
  182. FSynchronizeProgressForm = NULL;
  183. FProgressForm = NULL;
  184. FRefreshLocalDirectory = false;
  185. FRefreshRemoteDirectory = false;
  186. FDDExtMapFile = NULL;
  187. // CreateMutexW keeps failing with ERROR_NOACCESS
  188. FDDExtMutex = CreateMutexA(NULL, false, AnsiString(DRAG_EXT_MUTEX).c_str());
  189. DebugAssert(FDDExtMutex != NULL);
  190. FDDTargetControl = NULL;
  191. FDelayedDeletionTimer = NULL;
  192. FDelayedDeletionList = new TStringList();
  193. FDDFileList = NULL;
  194. FPendingTempSpaceWarn = false;
  195. FCapturedLog = NULL;
  196. FDragDropOperation = false;
  197. memset(&FHistoryMenu, 0, sizeof(FHistoryMenu));
  198. FAllowTransferPresetAutoSelect = true;
  199. FCopyParamDefault = L"";
  200. FSynchronizeController = NULL;
  201. FPendingQueueActionItem = NULL;
  202. FLockLevel = 0;
  203. FLockSuspendLevel = 0;
  204. FDisabledOnLockSuspend = false;
  205. FAlternativeDelete = false;
  206. FTrayIcon = new ::TTrayIcon(0);
  207. FTrayIcon->OnClick = TrayIconClick;
  208. FMaxQueueLength = 0;
  209. FLastContextPopupScreenPoint = TPoint(-1, -1);
  210. FTransferResumeList = NULL;
  211. FMoveToQueue = false;
  212. FStandaloneEditing = false;
  213. FOnFeedSynchronizeError = NULL;
  214. FNeedSession = false;
  215. FDoNotIdleCurrentTerminal = 0;
  216. FEditorManager = new TEditorManager();
  217. FEditorManager->OnFileChange = ExecutedFileChanged;
  218. FEditorManager->OnFileReload = ExecutedFileReload;
  219. FEditorManager->OnFileEarlyClosed = ExecutedFileEarlyClosed;
  220. FEditorManager->OnFileUploadComplete = ExecutedFileUploadComplete;
  221. FLocalEditors = new TList();
  222. FQueueStatus = NULL;
  223. FQueueStatusSection = new TCriticalSection();
  224. FQueueStatusInvalidated = false;
  225. FQueueItemInvalidated = false;
  226. FQueueActedItem = NULL;
  227. FQueueController = new TQueueController(QueueView3);
  228. FUserActionTimer = new TTimer(this);
  229. FUserActionTimer->Enabled = false;
  230. FUserActionTimer->Interval = 10;
  231. FUserActionTimer->OnTimer = UserActionTimer;
  232. FNotes = new TStringList();
  233. FNoteTimer = new TTimer(this);
  234. FNoteTimer->Enabled = false;
  235. FNoteTimer->OnTimer = NoteTimer;
  236. FOnNoteClick = NULL;
  237. FOle32Library = LoadLibrary(L"Ole32.dll");
  238. FDragMoveCursor = FOle32Library != NULL ?
  239. LoadCursor(FOle32Library, MAKEINTRESOURCE(2)) : NULL;
  240. UseSystemSettings(this);
  241. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  242. static_cast<TObject*>(GetComponent(fcTransferList)));
  243. DebugAssert(TransferList != NULL);
  244. FTransferListHoverIndex = -1;
  245. TransferList->OnChange = TransferListChange;
  246. TransferList->OnDrawItem = TransferListDrawItem;
  247. SetSubmenu(dynamic_cast<TTBXCustomItem *>(static_cast<TObject *>(GetComponent(fcColorMenu))));
  248. SetSubmenu(NonVisualDataModule->ColorMenuItem);
  249. UseDesktopFont(SessionsPageControl);
  250. UpdateSessionsPageControlHeight();
  251. UseDesktopFont(RemoteDirView);
  252. UseDesktopFont(RemoteDriveView);
  253. UseDesktopFont(QueueView3);
  254. UseDesktopFont(QueueLabel);
  255. UseDesktopFont(RemoteStatusBar);
  256. reinterpret_cast<TLabel*>(QueueSplitter)->OnDblClick = QueueSplitterDblClick;
  257. QueueSplitter->ShowHint = true;
  258. RemotePanelSplitter->ShowHint = true;
  259. UpdateImages();
  260. FCustomCommandMenu = CreateTBXPopupMenu(this);
  261. FCustomCommandLocalFileList = NULL;
  262. FCustomCommandRemoteFileList = NULL;
  263. FSessionColors = new TPngImageList(this);
  264. FSessionColors->ColorDepth = cd32Bit;
  265. AddFixedSessionImages();
  266. SessionsPageControl->Images = FSessionColors;
  267. QueueLabel->FocusControl = QueueView3;
  268. UpdateQueueLabel();
  269. FRemoteDirViewWasFocused = true;
  270. CreateHiddenWindow();
  271. StartUpdates();
  272. }
  273. //---------------------------------------------------------------------------
  274. __fastcall TCustomScpExplorerForm::~TCustomScpExplorerForm()
  275. {
  276. // this has to be one of the very first things to do
  277. StopUpdateThread();
  278. delete FCustomCommandLocalFileList;
  279. delete FCustomCommandRemoteFileList;
  280. delete FCustomCommandMenu;
  281. delete FTrayIcon;
  282. FTrayIcon = NULL;
  283. FEditorManager->CloseInternalEditors(ForceCloseInternalEditor);
  284. delete FEditorManager;
  285. ForceCloseLocalEditors();
  286. delete FLocalEditors;
  287. HideFileFindDialog();
  288. if (FDelayedDeletionTimer)
  289. {
  290. DoDelayedDeletion(NULL);
  291. SAFE_DESTROY(FDelayedDeletionTimer);
  292. }
  293. SAFE_DESTROY(FDelayedDeletionList);
  294. // sometimes we do not get DDEnd so the list is not released
  295. SAFE_DESTROY(FDDFileList);
  296. DebugAssert(FSynchronizeController == NULL);
  297. CloseHandle(FDDExtMutex);
  298. FDDExtMutex = NULL;
  299. FreeLibrary(FOle32Library);
  300. FOle32Library = NULL;
  301. FDragMoveCursor = NULL;
  302. DebugAssert(!FErrorList);
  303. if (FEverShown)
  304. {
  305. // when window is never shown (like when running command-line operation),
  306. // particularly window site is not restored correctly (BoundsRect value set
  307. // in RestoreForm gets lost during handle allocation), so we do not want
  308. // it to be stored
  309. StoreParams();
  310. }
  311. Terminal = NULL;
  312. Queue = NULL;
  313. DebugAssert(NonVisualDataModule && (NonVisualDataModule->ScpExplorer == this));
  314. NonVisualDataModule->ScpExplorer = NULL;
  315. delete FQueueController;
  316. FQueueController = NULL;
  317. delete FQueueStatusSection;
  318. FQueueStatusSection = NULL;
  319. delete FQueueStatus;
  320. FQueueStatus = NULL;
  321. delete FUserActionTimer;
  322. FUserActionTimer = NULL;
  323. delete FNoteTimer;
  324. delete FNotes;
  325. delete FNoteData;
  326. SAFE_DESTROY(FDocks);
  327. SessionsPageControl->Images = NULL;
  328. // only after clearing Terminal (after DoTerminalListChanged => ... => AddSessionColor is called)
  329. SAFE_DESTROY(FSessionColors);
  330. SAFE_DESTROY(FSessionsDragDropFilesEx);
  331. SAFE_DESTROY(FHistoryMenu[0][0]);
  332. SAFE_DESTROY(FHistoryMenu[0][1]);
  333. SAFE_DESTROY(FHistoryMenu[1][0]);
  334. SAFE_DESTROY(FHistoryMenu[1][1]);
  335. if (FHiddenWindow != NULL)
  336. {
  337. DestroyWindow(FHiddenWindow);
  338. FHiddenWindow = NULL;
  339. }
  340. }
  341. //---------------------------------------------------------------------------
  342. void __fastcall TCustomScpExplorerForm::RefreshPanel(const UnicodeString & Session, const UnicodeString & Path)
  343. {
  344. std::unique_ptr<TSessionData> Data;
  345. if (!Session.IsEmpty())
  346. {
  347. bool DefaultsOnly;
  348. Data.reset(StoredSessions->ParseUrl(Session, NULL, DefaultsOnly));
  349. }
  350. TTerminalManager * Manager = TTerminalManager::Instance();
  351. for (int Index = 0; Index < Manager->Count; Index++)
  352. {
  353. TTerminal * Terminal = Manager->Terminals[Index];
  354. if (Session.IsEmpty() ||
  355. Terminal->SessionData->IsSameSite(Data.get()))
  356. {
  357. if (Path.IsEmpty())
  358. {
  359. Terminal->ClearCaches();
  360. }
  361. else
  362. {
  363. Terminal->DirectoryModified(Path, true);
  364. }
  365. }
  366. }
  367. // We should flag a pending refresh for the background terminals or busy foreground terminals
  368. if ((Terminal != NULL) && Terminal->Active &&
  369. CanCommandLineFromAnotherInstance() &&
  370. (Session.IsEmpty() || Terminal->SessionData->IsSameSite(Data.get())) &&
  371. (Path.IsEmpty() || UnixIsChildPath(Path, Terminal->CurrentDirectory)))
  372. {
  373. Terminal->ReloadDirectory();
  374. }
  375. }
  376. //---------------------------------------------------------------------------
  377. void __fastcall TCustomScpExplorerForm::WMCopyData(TMessage & Message)
  378. {
  379. PCOPYDATASTRUCT CopyData = reinterpret_cast<PCOPYDATASTRUCT>(Message.LParam);
  380. size_t MessageSize = sizeof(TCopyDataMessage);
  381. bool Result = DebugAlwaysTrue(CopyData->cbData == MessageSize);
  382. if (Result)
  383. {
  384. const TCopyDataMessage & Message = *reinterpret_cast<const TCopyDataMessage *>(CopyData->lpData);
  385. Result = (Message.Version == TCopyDataMessage::Version1);
  386. if (Result)
  387. {
  388. switch (Message.Command)
  389. {
  390. case TCopyDataMessage::CommandCanCommandLine:
  391. Result = CanCommandLineFromAnotherInstance();
  392. break;
  393. case TCopyDataMessage::CommandCommandLine:
  394. {
  395. UnicodeString CommandLine(Message.CommandLine);
  396. Result = CommandLineFromAnotherInstance(CommandLine);
  397. }
  398. break;
  399. case TCopyDataMessage::RefreshPanel:
  400. RefreshPanel(Message.Refresh.Session, Message.Refresh.Path);
  401. break;
  402. case TCopyDataMessage::MainWindowCheck:
  403. Result = true;
  404. break;
  405. default:
  406. Result = false;
  407. break;
  408. }
  409. }
  410. }
  411. Message.Result = Result ? 1 : 0;
  412. }
  413. //---------------------------------------------------------------------------
  414. void __fastcall TCustomScpExplorerForm::CreateHiddenWindow()
  415. {
  416. WNDCLASS WindowClass = {0};
  417. WindowClass.lpfnWndProc = DefWindowProc;
  418. WindowClass.hInstance = HInstance;
  419. WindowClass.lpszClassName = HIDDEN_WINDOW_NAME;
  420. FHiddenWindow = NULL;
  421. if (RegisterClass(&WindowClass))
  422. {
  423. FHiddenWindow = CreateWindow(HIDDEN_WINDOW_NAME, L"",
  424. WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, NULL);
  425. }
  426. }
  427. //---------------------------------------------------------------------------
  428. bool __fastcall TCustomScpExplorerForm::CanConsole()
  429. {
  430. return (Terminal != NULL) && (Terminal->IsCapable[fcAnyCommand] || Terminal->IsCapable[fcSecondaryShell]);
  431. }
  432. //---------------------------------------------------------------------------
  433. bool __fastcall TCustomScpExplorerForm::CanCommandLineFromAnotherInstance()
  434. {
  435. bool Result = Visible && !NonVisualDataModule->Busy;
  436. return Result;
  437. }
  438. //---------------------------------------------------------------------------
  439. bool __fastcall TCustomScpExplorerForm::CommandLineFromAnotherInstance(
  440. const UnicodeString & CommandLine)
  441. {
  442. TProgramParams Params(CommandLine);
  443. bool Result = CanCommandLineFromAnotherInstance() && DebugAlwaysTrue(Params.ParamCount > 0);
  444. if (Result)
  445. {
  446. NonVisualDataModule->StartBusy();
  447. try
  448. {
  449. // this action is initiated from another process,
  450. // so it's likely that our window is not visible,
  451. // and user won't see what is going on
  452. Application->BringToFront();
  453. // reload sessions as we may be asked to open a session
  454. // just stored by another instance
  455. StoredSessions->Reload();
  456. UnicodeString SessionName = Params.Param[1];
  457. std::unique_ptr<TObjectList> DataList(new TObjectList());
  458. UnicodeString DownloadFile; // unused
  459. try
  460. {
  461. GetLoginData(SessionName, &Params, DataList.get(), DownloadFile, true, this);
  462. }
  463. catch (EAbort &)
  464. {
  465. // Happens when opening session in PuTTY and in other situations,
  466. // in which we do not want to fail WM_COPYDATA, as that would cause master instance
  467. // to process the command-line on its own (or to pass it to yet another instance)
  468. }
  469. if (DataList->Count > 0)
  470. {
  471. TTerminalManager * Manager = TTerminalManager::Instance();
  472. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  473. }
  474. }
  475. __finally
  476. {
  477. NonVisualDataModule->EndBusy();
  478. }
  479. }
  480. return Result;
  481. }
  482. //---------------------------------------------------------------------------
  483. void __fastcall TCustomScpExplorerForm::SetTerminal(TTerminal * value)
  484. {
  485. if (FTerminal != value)
  486. {
  487. TerminalChanging();
  488. DoSetTerminal(value, false);
  489. }
  490. }
  491. //---------------------------------------------------------------------------
  492. void __fastcall TCustomScpExplorerForm::DoSetTerminal(TTerminal * value, bool Replace)
  493. {
  494. FTerminal = value;
  495. bool PrevAllowTransferPresetAutoSelect = FAllowTransferPresetAutoSelect;
  496. FAllowTransferPresetAutoSelect = false;
  497. try
  498. {
  499. TerminalChanged(Replace);
  500. }
  501. __finally
  502. {
  503. FAllowTransferPresetAutoSelect = PrevAllowTransferPresetAutoSelect;
  504. }
  505. if (Terminal != NULL)
  506. {
  507. TransferPresetAutoSelect();
  508. }
  509. }
  510. //---------------------------------------------------------------------------
  511. void __fastcall TCustomScpExplorerForm::ReplaceTerminal(TTerminal * value)
  512. {
  513. DoSetTerminal(value, true);
  514. }
  515. //---------------------------------------------------------------------------
  516. void __fastcall TCustomScpExplorerForm::TerminalChanging()
  517. {
  518. if (FTerminal != NULL)
  519. {
  520. UpdateTerminal(Terminal);
  521. }
  522. }
  523. //---------------------------------------------------------------------------
  524. void __fastcall TCustomScpExplorerForm::TerminalChanged(bool Replaced)
  525. {
  526. if (Replaced)
  527. {
  528. RemoteDirView->ReplaceTerminal(Terminal);
  529. }
  530. else
  531. {
  532. RemoteDirView->Terminal = Terminal;
  533. }
  534. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  535. TManagedTerminal * ManagedTerminal = NULL;
  536. if (Terminal != NULL)
  537. {
  538. if (Terminal->Active)
  539. {
  540. Terminal->RefreshDirectory();
  541. }
  542. ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  543. DebugAssert(ManagedTerminal != NULL);
  544. if (WinConfiguration->PreservePanelState)
  545. {
  546. if (ManagedTerminal->RemoteExplorerState != NULL)
  547. {
  548. RemoteDirView->RestoreState(ManagedTerminal->RemoteExplorerState);
  549. }
  550. else
  551. {
  552. RemoteDirView->ClearState();
  553. }
  554. }
  555. if (!Replaced)
  556. {
  557. InitStatusBar();
  558. }
  559. }
  560. DoTerminalListChanged();
  561. if (ManagedTerminal != NULL)
  562. {
  563. // this has to be set only after the tab is switched from DoTerminalListChanged,
  564. // otherwise we are changing color of wrong tab
  565. SessionColor = (TColor)ManagedTerminal->StateData->Color;
  566. }
  567. UpdateTransferList();
  568. // Update panels Enable state before refreshing the labels
  569. UpdateControls();
  570. }
  571. //---------------------------------------------------------------------------
  572. void __fastcall TCustomScpExplorerForm::SetQueue(TTerminalQueue * value)
  573. {
  574. if (Queue != value)
  575. {
  576. if (FQueue != NULL)
  577. {
  578. FQueue->OnListUpdate = NULL;
  579. FQueue->OnQueueItemUpdate = NULL;
  580. }
  581. FQueue = value;
  582. if (FQueue != NULL)
  583. {
  584. DebugAssert(FQueue->OnListUpdate == NULL);
  585. FQueue->OnListUpdate = QueueListUpdate;
  586. DebugAssert(FQueue->OnQueueItemUpdate == NULL);
  587. FQueue->OnQueueItemUpdate = QueueItemUpdate;
  588. }
  589. QueueChanged();
  590. }
  591. }
  592. //---------------------------------------------------------------------------
  593. void __fastcall TCustomScpExplorerForm::QueueView3Deletion(TObject * /*Sender*/,
  594. TListItem * Item)
  595. {
  596. if (FQueueActedItem == Item)
  597. {
  598. FQueueActedItem = NULL;
  599. if ((QueueView3->PopupMenu != NULL) &&
  600. (QueueView3->PopupMenu->PopupComponent == QueueView3))
  601. {
  602. // rather "trick", suggested by Jordan on jrsoftware.toolbar2000
  603. ReleaseCapture();
  604. }
  605. }
  606. if (Item->Data == FPendingQueueActionItem)
  607. {
  608. FPendingQueueActionItem = NULL;
  609. }
  610. }
  611. //---------------------------------------------------------------------------
  612. void __fastcall TCustomScpExplorerForm::UpdateQueueStatus(bool QueueChanging)
  613. {
  614. {
  615. TGuard Guard(FQueueStatusSection);
  616. FQueueStatusInvalidated = false;
  617. if (FQueue != NULL)
  618. {
  619. FQueueStatus = FQueue->CreateStatus(FQueueStatus);
  620. }
  621. }
  622. if ((FQueueStatus != NULL) && (FQueueStatus->Count > FMaxQueueLength))
  623. {
  624. FMaxQueueLength = FQueueStatus->Count;
  625. Configuration->Usage->SetMax(L"MaxQueueLength", FMaxQueueLength);
  626. }
  627. FQueueController->UpdateQueueStatus(FQueueStatus);
  628. SetQueueProgress();
  629. UpdateQueueView();
  630. bool IsEmpty = (FQueueStatus == NULL) || (FQueueStatus->Count == 0);
  631. if (IsEmpty && (Terminal != NULL))
  632. {
  633. TOnceDoneOperation OnceDoneOperation =
  634. NonVisualDataModule->CurrentQueueOnceEmptyOperation();
  635. NonVisualDataModule->ResetQueueOnceEmptyOperation();
  636. if ((FQueue != NULL) && !WinConfiguration->EnableQueueByDefault && !QueueChanging)
  637. {
  638. FQueue->Enabled = false;
  639. }
  640. if ((OnceDoneOperation != odoIdle) && !NonVisualDataModule->Busy)
  641. {
  642. Terminal->CloseOnCompletion(OnceDoneOperation, LoadStr(CLOSED_ON_QUEUE_EMPTY));
  643. }
  644. }
  645. }
  646. //---------------------------------------------------------------------------
  647. UnicodeString __fastcall TCustomScpExplorerForm::GetQueueProgressTitle()
  648. {
  649. UnicodeString Result;
  650. if (FQueueStatus != NULL)
  651. {
  652. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  653. {
  654. TFileOperationProgressType * ProgressData =
  655. FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  656. if ((ProgressData != NULL) && ProgressData->InProgress)
  657. {
  658. Result = TProgressForm::ProgressStr(NULL, ProgressData);
  659. }
  660. }
  661. else if (FQueueStatus->ActiveAndPendingPrimaryCount > 1)
  662. {
  663. Result = FMTLOAD(PROGRESS_IN_QUEUE, (FQueueStatus->ActiveAndPendingPrimaryCount));
  664. }
  665. }
  666. return Result;
  667. }
  668. //---------------------------------------------------------------------------
  669. void __fastcall TCustomScpExplorerForm::UpdateQueueView()
  670. {
  671. ComponentVisible[fcQueueView] =
  672. (WinConfiguration->QueueView.Show == qvShow) ||
  673. ((WinConfiguration->QueueView.Show == qvHideWhenEmpty) &&
  674. (FQueueStatus != NULL) && (FQueueStatus->Count > 0));
  675. }
  676. //---------------------------------------------------------------------------
  677. void __fastcall TCustomScpExplorerForm::QueueChanged()
  678. {
  679. if (FQueueStatus != NULL)
  680. {
  681. delete FQueueStatus;
  682. FQueueStatus = NULL;
  683. }
  684. UpdateQueueStatus(true);
  685. }
  686. //---------------------------------------------------------------------------
  687. void __fastcall TCustomScpExplorerForm::QueueListUpdate(TTerminalQueue * Queue)
  688. {
  689. if (FQueue == Queue)
  690. {
  691. FQueueStatusInvalidated = true;
  692. }
  693. }
  694. //---------------------------------------------------------------------------
  695. void __fastcall TCustomScpExplorerForm::QueueItemUpdate(TTerminalQueue * Queue,
  696. TQueueItem * Item)
  697. {
  698. if (FQueue == Queue)
  699. {
  700. TGuard Guard(FQueueStatusSection);
  701. // this may be running in parallel with QueueChanged
  702. if (FQueueStatus != NULL)
  703. {
  704. TQueueItemProxy * QueueItem = FQueueStatus->FindByQueueItem(Item);
  705. if ((Item->Status == TQueueItem::qsDone) && (Terminal != NULL))
  706. {
  707. FRefreshLocalDirectory = (QueueItem == NULL) ||
  708. (!QueueItem->Info->ModifiedLocal.IsEmpty());
  709. FRefreshRemoteDirectory = (QueueItem == NULL) ||
  710. (!QueueItem->Info->ModifiedRemote.IsEmpty());
  711. }
  712. if (QueueItem != NULL)
  713. {
  714. QueueItem->UserData = (void*)true;
  715. FQueueItemInvalidated = true;
  716. }
  717. }
  718. }
  719. }
  720. //---------------------------------------------------------------------------
  721. bool __fastcall TCustomScpExplorerForm::IsQueueAutoPopup()
  722. {
  723. // during standalone editing, we have no way to see/control queue,
  724. // so we have to always popup prompts automatically
  725. return FStandaloneEditing || GUIConfiguration->QueueAutoPopup;
  726. }
  727. //---------------------------------------------------------------------------
  728. void __fastcall TCustomScpExplorerForm::RefreshQueueItems()
  729. {
  730. if (FQueueStatus != NULL)
  731. {
  732. bool QueueAutoPopup = IsQueueAutoPopup();
  733. bool NeedRefresh = FQueueController->NeedRefresh();
  734. bool Refresh = FQueueItemInvalidated || NeedRefresh;
  735. FQueueItemInvalidated = false;
  736. int Limit = Refresh ? FQueueStatus->Count : FQueueStatus->DoneAndActiveCount;
  737. bool Updated = false;
  738. TQueueItemProxy * QueueItem;
  739. bool WasUserAction;
  740. for (int Index = 0; Index < Limit; Index++)
  741. {
  742. bool Update = false;
  743. QueueItem = FQueueStatus->Items[Index];
  744. WasUserAction = TQueueItem::IsUserActionStatus(QueueItem->Status);
  745. if (!NonVisualDataModule->Busy && QueueAutoPopup && WasUserAction &&
  746. (FPendingQueueActionItem == NULL))
  747. {
  748. FPendingQueueActionItem = QueueItem;
  749. FUserActionTimer->Enabled = true;
  750. }
  751. if ((bool)QueueItem->UserData)
  752. {
  753. QueueItem->UserData = (void*)false;
  754. QueueItem->Update();
  755. Updated = true;
  756. Update = true;
  757. }
  758. else if (FQueueController->QueueItemNeedsFrequentRefresh(QueueItem) || NeedRefresh)
  759. {
  760. Update = true;
  761. }
  762. if (Update)
  763. {
  764. FQueueController->RefreshQueueItem(QueueItem);
  765. }
  766. }
  767. if (Updated)
  768. {
  769. NonVisualDataModule->UpdateNonVisibleActions();
  770. SetQueueProgress();
  771. }
  772. }
  773. }
  774. //---------------------------------------------------------------------------
  775. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressState(TBPFLAG Flags)
  776. {
  777. // Could now use Application->MainFormHandle, now that we implement Application->OnGetMainFormHandle
  778. FTaskbarList->SetProgressState(GetMainForm()->Handle, Flags);
  779. }
  780. //---------------------------------------------------------------------------
  781. void __fastcall TCustomScpExplorerForm::SetTaskbarListProgressValue(TFileOperationProgressType * ProgressData)
  782. {
  783. if (ProgressData->Operation != foCalculateSize)
  784. {
  785. int OverallProgress;
  786. // FProgressForm is null when this is called from SetQueueProgress
  787. if ((FProgressForm != NULL) && (FProgressForm->SynchronizeProgress != NULL))
  788. {
  789. OverallProgress = FProgressForm->SynchronizeProgress->Progress(ProgressData);
  790. }
  791. else
  792. {
  793. OverallProgress = ProgressData->OverallProgress();
  794. }
  795. // implies TBPF_NORMAL
  796. FTaskbarList->SetProgressValue(GetMainForm()->Handle, OverallProgress, 100);
  797. }
  798. else
  799. {
  800. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  801. }
  802. }
  803. //---------------------------------------------------------------------------
  804. void __fastcall TCustomScpExplorerForm::SetQueueProgress()
  805. {
  806. TTerminalManager::Instance()->QueueStatusUpdated();
  807. if ((FTaskbarList != NULL) && (FProgressForm == NULL))
  808. {
  809. if ((FQueueStatus != NULL) && (FQueueStatus->ActiveAndPendingPrimaryCount > 0))
  810. {
  811. if (FQueueStatus->IsOnlyOneActiveAndNoPending())
  812. {
  813. TFileOperationProgressType * ProgressData = NULL;
  814. if (FQueueStatus->Items[FQueueStatus->DoneCount] != NULL)
  815. {
  816. ProgressData = FQueueStatus->Items[FQueueStatus->DoneCount]->ProgressData;
  817. }
  818. if ((ProgressData != NULL) &&
  819. ProgressData->InProgress)
  820. {
  821. SetTaskbarListProgressValue(ProgressData);
  822. }
  823. else
  824. {
  825. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  826. }
  827. }
  828. else
  829. {
  830. SetTaskbarListProgressState(TBPF_INDETERMINATE);
  831. }
  832. }
  833. else
  834. {
  835. SetTaskbarListProgressState(TBPF_NOPROGRESS);
  836. }
  837. }
  838. UpdateQueueLabel();
  839. }
  840. //---------------------------------------------------------------------------
  841. void __fastcall TCustomScpExplorerForm::UpdateQueueLabel()
  842. {
  843. UnicodeString Caption = LoadStr(QUEUE_CAPTION);
  844. if (FQueueStatus != NULL)
  845. {
  846. int ActiveAndPendingPrimaryCount = FQueueStatus->ActiveAndPendingPrimaryCount;
  847. if (ActiveAndPendingPrimaryCount > 0)
  848. {
  849. Caption = FORMAT("%s (%d)", (Caption, ActiveAndPendingPrimaryCount));
  850. }
  851. }
  852. QueueLabel->Caption = Caption;
  853. }
  854. //---------------------------------------------------------------------------
  855. void __fastcall TCustomScpExplorerForm::UpdateTransferList()
  856. {
  857. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  858. static_cast<TComponent*>(GetComponent(fcTransferList)));
  859. TTBXDropDownItem * TransferDropDown = dynamic_cast<TTBXDropDownItem*>(
  860. static_cast<TComponent*>(GetComponent(fcTransferDropDown)));
  861. TransferList->Strings->BeginUpdate();
  862. try
  863. {
  864. TransferList->Strings->Assign(GUIConfiguration->CopyParamList->NameList);
  865. TransferList->Strings->Insert(0, StripHotkey(LoadStr(COPY_PARAM_DEFAULT)));
  866. TransferList->ItemIndex = GUIConfiguration->CopyParamIndex + 1;
  867. if (FTransferDropDownHint.IsEmpty())
  868. {
  869. FTransferDropDownHint = TransferDropDown->Hint;
  870. }
  871. // this way we get name for "default" settings (COPY_PARAM_DEFAULT)
  872. UnicodeString Name = TransferList->Strings->Strings[TransferList->ItemIndex];
  873. TransferDropDown->Text = StripHotkey(Name);
  874. TransferDropDown->Hint = FORMAT(L"%s|%s:\n%s",
  875. (FTransferDropDownHint, StripHotkey(Name),
  876. GUIConfiguration->CurrentCopyParam.GetInfoStr(L"; ",
  877. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General))));
  878. // update the label, otherwise when it is updated only on the first draw
  879. // of the list, it is drawn "bold" for some reason
  880. FTransferListHoverIndex = TransferList->ItemIndex;
  881. UpdateTransferLabel();
  882. }
  883. __finally
  884. {
  885. TransferList->Strings->EndUpdate();
  886. }
  887. }
  888. //---------------------------------------------------------------------------
  889. void __fastcall TCustomScpExplorerForm::UpdateCustomCommandsToolbar()
  890. {
  891. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar *>(GetComponent(fcCustomCommandsBand));
  892. DebugAssert(Toolbar != NULL);
  893. NonVisualDataModule->UpdateCustomCommandsToolbar(Toolbar);
  894. }
  895. //---------------------------------------------------------------------------
  896. void __fastcall TCustomScpExplorerForm::UpdateActions()
  897. {
  898. TForm::UpdateActions();
  899. if (ComponentVisible[fcCustomCommandsBand])
  900. {
  901. UpdateCustomCommandsToolbar();
  902. }
  903. }
  904. //---------------------------------------------------------------------------
  905. void __fastcall TCustomScpExplorerForm::UpdateSessionsPageControlHeight()
  906. {
  907. SessionsPageControl->Height = SessionsPageControl->GetTabsHeight();
  908. }
  909. //---------------------------------------------------------------------------
  910. void __fastcall TCustomScpExplorerForm::ConfigurationChanged()
  911. {
  912. DebugAssert(Configuration && RemoteDirView);
  913. RemoteDirView->DimmHiddenFiles = WinConfiguration->DimmHiddenFiles;
  914. RemoteDirView->ShowHiddenFiles = WinConfiguration->ShowHiddenFiles;
  915. RemoteDirView->FormatSizeBytes = WinConfiguration->FormatSizeBytes;
  916. RemoteDirView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  917. RemoteDirView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  918. if (RemoteDirView->RowSelect != WinConfiguration->FullRowSelect)
  919. {
  920. RemoteDirView->RowSelect = WinConfiguration->FullRowSelect;
  921. // selection is not redrawn automatically when RowSelect changes
  922. RemoteDirView->Invalidate();
  923. }
  924. RemoteDriveView->DimmHiddenDirs = WinConfiguration->DimmHiddenFiles;
  925. RemoteDriveView->ShowHiddenDirs = WinConfiguration->ShowHiddenFiles;
  926. RemoteDriveView->ShowInaccesibleDirectories = WinConfiguration->ShowInaccesibleDirectories;
  927. RemoteDriveView->NaturalOrderNumericalSorting = WinConfiguration->NaturalOrderNumericalSorting;
  928. UpdateSessionsPageControlHeight();
  929. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  930. UpdateToolbarDisplayMode();
  931. UpdateTransferList();
  932. if (Terminal != NULL)
  933. {
  934. TransferPresetAutoSelect();
  935. }
  936. UpdateQueueView();
  937. UpdateControls();
  938. // this can be called even before constuctor finishes.
  939. if (FEditorManager != NULL)
  940. {
  941. FEditorManager->ProcessFiles(FileConfigurationChanged, NULL);
  942. }
  943. // this can be called even before constuctor finishes.
  944. if (FLocalEditors != NULL)
  945. {
  946. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  947. {
  948. ReconfigureEditorForm(static_cast<TForm *>(FLocalEditors->Items[Index]));
  949. }
  950. }
  951. if (ComponentVisible[fcCustomCommandsBand])
  952. {
  953. UpdateCustomCommandsToolbar();
  954. }
  955. // show only when keeping queue items forever,
  956. // otherwise, its enough to have in in the context menu
  957. QueueDeleteAllDoneQueueToolbarItem->Visible =
  958. WinConfiguration->QueueKeepDoneItems && (WinConfiguration->QueueKeepDoneItemsFor < 0);
  959. }
  960. //---------------------------------------------------------------------------
  961. void __fastcall TCustomScpExplorerForm::FileConfigurationChanged(
  962. const UnicodeString FileName, TEditedFileData * /*Data*/, TObject * Token,
  963. void * /*Arg*/)
  964. {
  965. if (Token != NULL)
  966. {
  967. TForm * Editor = dynamic_cast<TForm*>(Token);
  968. DebugAssert(Editor != NULL);
  969. ReconfigureEditorForm(Editor);
  970. }
  971. }
  972. //---------------------------------------------------------------------------
  973. void __fastcall TCustomScpExplorerForm::EnableDDTransferConfirmation(TObject * /*Sender*/)
  974. {
  975. WinConfiguration->DDTransferConfirmation = asOn;
  976. }
  977. //---------------------------------------------------------------------------
  978. void __fastcall TCustomScpExplorerForm::CopyParamDialogAfter(
  979. TTransferDirection /*Direction*/, bool /*Temp*/, const UnicodeString & /*TargetDirectory*/)
  980. {
  981. // noop
  982. }
  983. //---------------------------------------------------------------------------
  984. bool __fastcall TCustomScpExplorerForm::CopyParamDialog(
  985. TTransferDirection Direction, TTransferType Type, bool Temp,
  986. TStrings * FileList, UnicodeString & TargetDirectory, TGUICopyParamType & CopyParam,
  987. bool Confirm, bool DragDrop, int Options)
  988. {
  989. bool Result = true;
  990. DebugAssert(Terminal && Terminal->Active);
  991. // these parameters are known in advance
  992. int Params =
  993. FLAGMASK(Type == ttMove, cpDelete);
  994. bool ToTemp = (Temp && (Direction == tdToLocal));
  995. if (Result && Confirm && WinConfiguration->ConfirmTransferring)
  996. {
  997. bool DisableNewerOnly =
  998. (!Terminal->IsCapable[fcNewerOnlyUpload] && (Direction == tdToRemote)) ||
  999. ToTemp;
  1000. Options |=
  1001. FLAGMASK(ToTemp, coTemp) |
  1002. FLAGMASK(!Terminal->IsCapable[fsBackgroundTransfers], coDisableQueue) |
  1003. coDoNotShowAgain;
  1004. TUsableCopyParamAttrs UsableCopyParamAttrs = Terminal->UsableCopyParamAttrs(Params);
  1005. int CopyParamAttrs = (Direction == tdToRemote ?
  1006. UsableCopyParamAttrs.Upload : UsableCopyParamAttrs.Download) |
  1007. FLAGMASK(DisableNewerOnly, cpaNoNewerOnly);
  1008. int OutputOptions =
  1009. FLAGMASK(DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto),
  1010. cooDoNotShowAgain);
  1011. std::unique_ptr<TSessionData> SessionData(SessionDataForCode());
  1012. FlashOnBackground(); // Particularly when called from ClipboardFakeCreated
  1013. Result = DoCopyDialog(Direction == tdToRemote, Type == ttMove,
  1014. FileList, TargetDirectory, &CopyParam, Options, CopyParamAttrs, SessionData.get(), &OutputOptions);
  1015. if (Result)
  1016. {
  1017. if (FLAGSET(OutputOptions, cooDoNotShowAgain))
  1018. {
  1019. if (DragDrop)
  1020. {
  1021. if (WinConfiguration->DDTransferConfirmation == asAuto)
  1022. {
  1023. PopupTrayBalloon(NULL, LoadStr(DD_TRANSFER_CONFIRM_OFF2), qtInformation,
  1024. NULL, 0, EnableDDTransferConfirmation, NULL);
  1025. }
  1026. WinConfiguration->DDTransferConfirmation = asOff;
  1027. }
  1028. else
  1029. {
  1030. WinConfiguration->ConfirmTransferring = false;
  1031. }
  1032. }
  1033. else
  1034. {
  1035. // User explicitly unchecked "do not show again",
  1036. // so show him the dialog the next time
  1037. if (DragDrop && (WinConfiguration->DDTransferConfirmation == asAuto))
  1038. {
  1039. WinConfiguration->DDTransferConfirmation = asOn;
  1040. }
  1041. }
  1042. CopyParamDialogAfter(Direction, Temp, TargetDirectory);
  1043. }
  1044. }
  1045. if (Result && CopyParam.Queue && !ToTemp && Terminal->IsCapable[fsBackgroundTransfers])
  1046. {
  1047. Configuration->Usage->Inc(L"TransfersOnBackground");
  1048. // these parameter are known only after transfer dialog
  1049. Params |=
  1050. (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  1051. AddQueueItem(Queue, Direction, FileList, TargetDirectory, CopyParam, Params);
  1052. Result = false;
  1053. ClearTransferSourceSelection(Direction);
  1054. }
  1055. return Result;
  1056. }
  1057. //---------------------------------------------------------------------------
  1058. void __fastcall TCustomScpExplorerForm::ClearTransferSourceSelection(TTransferDirection Direction)
  1059. {
  1060. if (FOnFileOperationFinished != NULL)
  1061. {
  1062. FOnFileOperationFinished(UnicodeString(), true);
  1063. }
  1064. else
  1065. {
  1066. TOperationSide Side = ((Direction == tdToRemote) ? osLocal : osRemote);
  1067. if (HasDirView[Side])
  1068. {
  1069. DirView(Side)->SelectAll(smNone);
  1070. }
  1071. }
  1072. }
  1073. //---------------------------------------------------------------------------
  1074. void __fastcall TCustomScpExplorerForm::AddQueueItem(
  1075. TTerminalQueue * Queue, TTransferDirection Direction, TStrings * FileList,
  1076. const UnicodeString TargetDirectory, const TGUICopyParamType & CopyParam,
  1077. int Params)
  1078. {
  1079. DebugAssert(Queue != NULL);
  1080. bool SingleFile = false;
  1081. if (FileList->Count == 1)
  1082. {
  1083. if (Direction == tdToRemote)
  1084. {
  1085. UnicodeString FileName = FileList->Strings[0];
  1086. SingleFile = FileExists(ApiPath(FileName));
  1087. }
  1088. else
  1089. {
  1090. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[0]);
  1091. SingleFile = !File->IsDirectory;
  1092. }
  1093. }
  1094. TQueueItem * QueueItem;
  1095. if (Direction == tdToRemote)
  1096. {
  1097. QueueItem = new TUploadQueueItem(Terminal, FileList, TargetDirectory,
  1098. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1099. }
  1100. else
  1101. {
  1102. QueueItem = new TDownloadQueueItem(Terminal, FileList, TargetDirectory,
  1103. &CopyParam, Params, SingleFile, CopyParam.QueueParallel);
  1104. }
  1105. AddQueueItem(Queue, QueueItem, Terminal);
  1106. }
  1107. //---------------------------------------------------------------------------
  1108. void __fastcall TCustomScpExplorerForm::AddQueueItem(TTerminalQueue * Queue, TQueueItem * QueueItem, TTerminal * Terminal)
  1109. {
  1110. if (Queue->IsEmpty)
  1111. {
  1112. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(Terminal));
  1113. ManagedTerminal->QueueOperationStart = Now();
  1114. }
  1115. Queue->AddItem(QueueItem);
  1116. }
  1117. //---------------------------------------------------------------------------
  1118. void __fastcall TCustomScpExplorerForm::RestoreFormParams()
  1119. {
  1120. }
  1121. //---------------------------------------------------------------------------
  1122. void __fastcall TCustomScpExplorerForm::RestoreParams()
  1123. {
  1124. DebugAssert(FDocks != NULL);
  1125. for (int Index = 0; Index < ComponentCount; Index++)
  1126. {
  1127. TTBDock * Dock = dynamic_cast<TTBDock *>(Components[Index]);
  1128. if ((Dock != NULL) && (Dock->Tag == 0))
  1129. {
  1130. FDocks->Add(Dock);
  1131. }
  1132. }
  1133. CollectItemsWithTextDisplayMode(this);
  1134. ConfigurationChanged();
  1135. QueuePanel->Height =
  1136. LoadDimension(WinConfiguration->QueueView.Height, WinConfiguration->QueueView.HeightPixelsPerInch, this);
  1137. LoadListViewStr(QueueView3, WinConfiguration->QueueView.Layout);
  1138. QueueDock->Visible = WinConfiguration->QueueView.ToolBar;
  1139. QueueLabel->Visible = WinConfiguration->QueueView.Label;
  1140. }
  1141. //---------------------------------------------------------------------------
  1142. void __fastcall TCustomScpExplorerForm::StoreParams()
  1143. {
  1144. WinConfiguration->QueueView.Height = QueuePanel->Height;
  1145. WinConfiguration->QueueView.HeightPixelsPerInch = GetControlPixelsPerInch(this);
  1146. WinConfiguration->QueueView.Layout = GetListViewStr(QueueView3);
  1147. WinConfiguration->QueueView.ToolBar = QueueDock->Visible;
  1148. WinConfiguration->QueueView.Label = QueueLabel->Visible;
  1149. }
  1150. //---------------------------------------------------------------------------
  1151. void __fastcall TCustomScpExplorerForm::CreateParams(TCreateParams & Params)
  1152. {
  1153. if (!FFormRestored)
  1154. {
  1155. FFormRestored = true;
  1156. RestoreFormParams();
  1157. }
  1158. TForm::CreateParams(Params);
  1159. }
  1160. //---------------------------------------------------------------------------
  1161. void __fastcall TCustomScpExplorerForm::SetDockAllowDrag(bool value)
  1162. {
  1163. DebugAssert(FDocks != NULL);
  1164. for (int Index = 0; Index < FDocks->Count; Index++)
  1165. {
  1166. static_cast<TTBDock*>(FDocks->Items[Index])->AllowDrag = value;
  1167. }
  1168. }
  1169. //---------------------------------------------------------------------------
  1170. void __fastcall TCustomScpExplorerForm::LoadToolbarsLayoutStr(UnicodeString LayoutStr, UnicodeString ButtonsStr)
  1171. {
  1172. SetDockAllowDrag(true);
  1173. while (!ButtonsStr.IsEmpty())
  1174. {
  1175. UnicodeString S = CutToChar(ButtonsStr, L';', true);
  1176. UnicodeString Name = CutToChar(S, L'=', true);
  1177. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1178. {
  1179. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1180. if ((Toolbar != NULL) && SameText(Name, GetToolbarKey(Toolbar->Name)))
  1181. {
  1182. while (!S.IsEmpty())
  1183. {
  1184. UnicodeString S2 = CutToChar(S, L',', true);
  1185. UnicodeString Name = CutToChar(S2, L':', true);
  1186. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1187. {
  1188. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1189. if (SameText(GetToolbarItemName(Item), Name))
  1190. {
  1191. // forward compatibility
  1192. UnicodeString S3 = CutToChar(S2, L':', true);
  1193. bool Visible = StrToIntDef(S3, 0);
  1194. Item->Visible = Visible;
  1195. }
  1196. }
  1197. }
  1198. }
  1199. }
  1200. }
  1201. ::LoadToolbarsLayoutStr(this, LayoutStr);
  1202. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1203. }
  1204. //---------------------------------------------------------------------------
  1205. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsLayoutStr()
  1206. {
  1207. UnicodeString Result;
  1208. SetDockAllowDrag(true);
  1209. Result = ::GetToolbarsLayoutStr(this);
  1210. SetDockAllowDrag(!WinConfiguration->LockToolbars);
  1211. return Result;
  1212. }
  1213. //---------------------------------------------------------------------------
  1214. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarItemName(TTBCustomItem * Item)
  1215. {
  1216. UnicodeString Result;
  1217. if (Item->Action != NULL)
  1218. {
  1219. Result = Item->Action->Name;
  1220. Result = RemoveSuffix(Result, L"Action");
  1221. }
  1222. else
  1223. {
  1224. Result = Item->Name;
  1225. Result = RemoveSuffix(Result, L"SubmenuItem");
  1226. Result = RemoveSuffix(Result, L"Item");
  1227. }
  1228. return Result;
  1229. }
  1230. //---------------------------------------------------------------------------
  1231. UnicodeString __fastcall TCustomScpExplorerForm::GetToolbarsButtonsStr()
  1232. {
  1233. UnicodeString Result;
  1234. for (int ToolbarIndex = 0; ToolbarIndex < ComponentCount; ToolbarIndex++)
  1235. {
  1236. UnicodeString ToolbarStr;
  1237. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Components[ToolbarIndex]);
  1238. if ((Toolbar != NULL) && (Toolbar != QueueToolbar))
  1239. {
  1240. for (int ItemIndex = 0; ItemIndex < Toolbar->Items->Count; ItemIndex++)
  1241. {
  1242. TTBCustomItem * Item = Toolbar->Items->Items[ItemIndex];
  1243. // Currently all buttons are visible by default, so we can safely remember all hidden buttons.
  1244. // Once we introduce any buttons that aer hidden by default, we would have to remember their initial state
  1245. // and save the changes here only.
  1246. if (!Item->Visible)
  1247. {
  1248. UnicodeString Name = GetToolbarItemName(Item);
  1249. DebugAssert(Name.Pos(L"TBX") == 0);
  1250. AddToList(ToolbarStr, FORMAT(L"%s:0", (Name)), L",");
  1251. }
  1252. }
  1253. }
  1254. if (!ToolbarStr.IsEmpty())
  1255. {
  1256. AddToList(Result, FORMAT(L"%s=%s", (GetToolbarKey(Toolbar->Name), ToolbarStr)), L";");
  1257. }
  1258. }
  1259. return Result;
  1260. }
  1261. //---------------------------------------------------------------------------
  1262. void __fastcall TCustomScpExplorerForm::CreateProgressForm(TSynchronizeProgress * SynchronizeProgress)
  1263. {
  1264. DebugAssert(FProgressForm == NULL);
  1265. FProgressForm =
  1266. new TProgressForm(Application, (FTransferResumeList != NULL), Terminal->IsCapable[fsSkipTransfer], SynchronizeProgress);
  1267. FProgressForm->DeleteLocalToRecycleBin =
  1268. (WinConfiguration->DeleteToRecycleBin != FAlternativeDelete);
  1269. FProgressForm->DeleteRemoteToRecycleBin =
  1270. (Terminal->SessionData->DeleteToRecycleBin != FAlternativeDelete) &&
  1271. !Terminal->SessionData->RecycleBinPath.IsEmpty();
  1272. // As progress window has delayed show now, we need to lock ourselves, (at least) until then
  1273. LockWindow();
  1274. if (FTaskbarList != NULL)
  1275. {
  1276. // Actually, do not know what hides the progress once the operation finishes
  1277. SetTaskbarListProgressState(TBPF_NORMAL);
  1278. }
  1279. }
  1280. //---------------------------------------------------------------------------
  1281. void __fastcall TCustomScpExplorerForm::DestroyProgressForm()
  1282. {
  1283. UnlockWindow();
  1284. SAFE_DESTROY(FProgressForm);
  1285. SetQueueProgress();
  1286. }
  1287. //---------------------------------------------------------------------------
  1288. void __fastcall TCustomScpExplorerForm::FileOperationProgress(
  1289. TFileOperationProgressType & ProgressData)
  1290. {
  1291. // Download to temporary local directory
  1292. if (FPendingTempSpaceWarn && ProgressData.InProgress && ProgressData.TotalSizeSet)
  1293. {
  1294. bool Continue = true;
  1295. FPendingTempSpaceWarn = false;
  1296. DoWarnLackOfTempSpace(ProgressData.Directory, ProgressData.TotalSize, Continue);
  1297. if (!Continue)
  1298. {
  1299. Abort();
  1300. }
  1301. }
  1302. // operation is being executed and we still didn't show up progress form
  1303. if (ProgressData.InProgress && !FProgressForm)
  1304. {
  1305. CreateProgressForm(NULL);
  1306. // When main window is hidden, synchronisation form does not exist (we do not get here during synchronization - only comparing),
  1307. // we suppose "/upload" or URL download mode
  1308. if (!Visible && (ProgressData.Operation != foCalculateSize) &&
  1309. (ProgressData.Operation != foCalculateChecksum) &&
  1310. // Hack to prevent renames from DoSynchronizeMove (synchronize checklist) closing the connection
  1311. (ProgressData.Operation != foRemoteMove) &&
  1312. (FSynchronizeProgressForm == NULL) &&
  1313. !FStandaloneEditing)
  1314. {
  1315. FProgressForm->OnceDoneOperation = odoDisconnect;
  1316. }
  1317. }
  1318. // operation is finished (or terminated), so we hide progress form
  1319. else if (!ProgressData.InProgress && (FProgressForm != NULL) && (FProgressForm->SynchronizeProgress == NULL))
  1320. {
  1321. DestroyProgressForm();
  1322. if (ProgressData.Operation == foCalculateSize)
  1323. {
  1324. // When calculating size before transfer, the abort caused by
  1325. // cancel flag set due to "MoveToQueue" below,
  1326. // is not propagated back to us in ExecuteFileOperation,
  1327. // as it is silently swallowed.
  1328. // So we explicitly (re)throw it here.
  1329. if (FMoveToQueue)
  1330. {
  1331. Abort();
  1332. }
  1333. }
  1334. else
  1335. {
  1336. if ((ProgressData.Cancel == csContinue) &&
  1337. !FAutoOperation)
  1338. {
  1339. OperationComplete(ProgressData.StartTime);
  1340. }
  1341. }
  1342. }
  1343. if (FProgressForm &&
  1344. // Can happen only during synchronization, when ending one of the operations
  1345. ProgressData.InProgress)
  1346. {
  1347. FProgressForm->SetProgressData(ProgressData);
  1348. if (FTaskbarList != NULL)
  1349. {
  1350. DebugAssert(ProgressData.InProgress);
  1351. SetTaskbarListProgressValue(&ProgressData);
  1352. }
  1353. if (FProgressForm->Cancel > csContinue)
  1354. {
  1355. ProgressData.SetCancelAtLeast(FProgressForm->Cancel);
  1356. if (FProgressForm->Cancel == csCancelFile)
  1357. {
  1358. FProgressForm->ClearCancel();
  1359. }
  1360. // cancel cancels even the move
  1361. FMoveToQueue = false;
  1362. }
  1363. else if (FProgressForm->MoveToQueue)
  1364. {
  1365. FMoveToQueue = true;
  1366. ProgressData.SetCancelAtLeast(csCancel);
  1367. }
  1368. if ((FTransferResumeList != NULL) &&
  1369. ProgressData.InProgress &&
  1370. ((ProgressData.Operation == foCopy) || (ProgressData.Operation == foMove)) &&
  1371. !ProgressData.FullFileName.IsEmpty())
  1372. {
  1373. if ((FTransferResumeList->Count == 0) ||
  1374. (FTransferResumeList->Strings[FTransferResumeList->Count - 1] != ProgressData.FullFileName))
  1375. {
  1376. // note that we do not recognize directories from files here
  1377. FTransferResumeList->Add(ProgressData.FullFileName);
  1378. }
  1379. }
  1380. }
  1381. }
  1382. //---------------------------------------------------------------------------
  1383. void __fastcall TCustomScpExplorerForm::OperationComplete(
  1384. const TDateTime & StartTime)
  1385. {
  1386. if (GUIConfiguration->BeepOnFinish &&
  1387. (Now() - StartTime > GUIConfiguration->BeepOnFinishAfter))
  1388. {
  1389. UnicodeString BeepSound = GUIConfiguration->BeepSound;
  1390. DWORD Sound;
  1391. if (!ExtractFileExt(BeepSound).IsEmpty())
  1392. {
  1393. Sound = SND_FILENAME;
  1394. }
  1395. else
  1396. {
  1397. Sound = SND_ALIAS;
  1398. }
  1399. PlaySound(BeepSound.c_str(), NULL, Sound | SND_ASYNC);
  1400. }
  1401. }
  1402. //---------------------------------------------------------------------------
  1403. void __fastcall TCustomScpExplorerForm::OperationProgress(
  1404. TFileOperationProgressType & ProgressData)
  1405. {
  1406. // For example, when calling TFileOperationProgressType::Suspend in TTerminal::QueryReopen,
  1407. // we cannot recurse back to TTerminal::Idle as that may detect another error, calling "suspend" again.
  1408. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1409. FileOperationProgress(ProgressData);
  1410. }
  1411. //---------------------------------------------------------------------------
  1412. UnicodeString __fastcall TCustomScpExplorerForm::GetProgressTitle()
  1413. {
  1414. UnicodeString Result;
  1415. if (FProgressForm != NULL)
  1416. {
  1417. Result = FProgressForm->ProgressStr();
  1418. }
  1419. return Result;
  1420. }
  1421. //---------------------------------------------------------------------------
  1422. bool __fastcall TCustomScpExplorerForm::PanelOperation(TOperationSide /*Side*/,
  1423. bool DragDrop)
  1424. {
  1425. return (!DragDrop && (DropSourceControl == NULL)) ||
  1426. (DropSourceControl == RemoteDirView);
  1427. }
  1428. //---------------------------------------------------------------------------
  1429. void __fastcall TCustomScpExplorerForm::DoOperationFinished(
  1430. TFileOperation Operation, TOperationSide Side,
  1431. bool /*Temp*/, const UnicodeString & FileName, bool Success,
  1432. TOnceDoneOperation & OnceDoneOperation)
  1433. {
  1434. if (!FAutoOperation)
  1435. {
  1436. // no selection on "/upload", form servers only as event handler
  1437. // (it is not displayed)
  1438. if (PanelOperation(Side, FDragDropOperation) &&
  1439. Visible && (Operation != foCalculateSize) &&
  1440. (Operation != foGetProperties) && (Operation != foCalculateChecksum))
  1441. {
  1442. if (FOnFileOperationFinished != NULL)
  1443. {
  1444. FOnFileOperationFinished(FileName, Success);
  1445. }
  1446. else
  1447. {
  1448. TCustomDirView * DView = DirView(Side);
  1449. UnicodeString FileNameOnly = ExtractFileName(FileName, (Side == osRemote));
  1450. TListItem *Item = DView->FindFileItem(FileNameOnly);
  1451. // this can happen when local drive is unplugged in the middle of the operation
  1452. if (Item != NULL)
  1453. {
  1454. if (Success) Item->Selected = false;
  1455. DView->MakeProgressVisible(Item);
  1456. }
  1457. }
  1458. }
  1459. if ((Operation == foCopy) || (Operation == foMove))
  1460. {
  1461. if (Side == osLocal)
  1462. {
  1463. Configuration->Usage->Inc(L"UploadedFiles");
  1464. }
  1465. else
  1466. {
  1467. Configuration->Usage->Inc(L"DownloadedFiles");
  1468. }
  1469. }
  1470. }
  1471. if (Success && (FSynchronizeController != NULL))
  1472. {
  1473. if (Operation == foCopy)
  1474. {
  1475. DebugAssert(Side == osLocal);
  1476. FSynchronizeController->LogOperation(soUpload, FileName);
  1477. }
  1478. else if (Operation == foDelete)
  1479. {
  1480. DebugAssert(Side == osRemote);
  1481. FSynchronizeController->LogOperation(soDelete, FileName);
  1482. }
  1483. }
  1484. if (FProgressForm)
  1485. {
  1486. OnceDoneOperation = FProgressForm->OnceDoneOperation;
  1487. }
  1488. }
  1489. //---------------------------------------------------------------------------
  1490. void __fastcall TCustomScpExplorerForm::OperationFinished(
  1491. TFileOperation Operation, TOperationSide Side,
  1492. bool Temp, const UnicodeString & FileName, Boolean Success,
  1493. TOnceDoneOperation & OnceDoneOperation)
  1494. {
  1495. TAutoNestingCounter Counter(FDoNotIdleCurrentTerminal);
  1496. DoOperationFinished(Operation, Side, Temp, FileName, Success,
  1497. OnceDoneOperation);
  1498. }
  1499. //---------------------------------------------------------------------------
  1500. TCustomDirView * __fastcall TCustomScpExplorerForm::DirView(TOperationSide Side)
  1501. {
  1502. DebugAssert(GetSide(Side) == osRemote);
  1503. DebugUsedParam(Side);
  1504. return RemoteDirView;
  1505. }
  1506. //---------------------------------------------------------------------------
  1507. bool __fastcall TCustomScpExplorerForm::DirViewEnabled(TOperationSide Side)
  1508. {
  1509. DebugAssert(GetSide(Side) == osRemote);
  1510. DebugUsedParam(Side);
  1511. return (Terminal != NULL);
  1512. }
  1513. //---------------------------------------------------------------------------
  1514. bool __fastcall TCustomScpExplorerForm::GetEnableFocusedOperation(
  1515. TOperationSide Side, int FilesOnly)
  1516. {
  1517. return DirView(Side)->AnyFileSelected(true, (FilesOnly != 0), true);
  1518. }
  1519. //---------------------------------------------------------------------------
  1520. bool __fastcall TCustomScpExplorerForm::GetEnableSelectedOperation(
  1521. TOperationSide Side, int FilesOnly)
  1522. {
  1523. return DirView(Side)->AnyFileSelected(false, (FilesOnly != 0), true);
  1524. }
  1525. //---------------------------------------------------------------------------
  1526. struct THistoryItemData
  1527. {
  1528. short int Side;
  1529. short int Index;
  1530. };
  1531. //---------------------------------------------------------------------------
  1532. void __fastcall TCustomScpExplorerForm::HistoryGo(TOperationSide Side, int Index)
  1533. {
  1534. DirView(Side)->HistoryGo(Index);
  1535. }
  1536. //---------------------------------------------------------------------------
  1537. void __fastcall TCustomScpExplorerForm::HistoryItemClick(System::TObject* Sender)
  1538. {
  1539. TTBCustomItem * Item = dynamic_cast<TTBCustomItem *>(Sender);
  1540. THistoryItemData Data = *reinterpret_cast<THistoryItemData*>(&(Item->Tag));
  1541. HistoryGo((TOperationSide)Data.Side, Data.Index);
  1542. }
  1543. //---------------------------------------------------------------------------
  1544. void __fastcall TCustomScpExplorerForm::UpdateHistoryMenu(TOperationSide Side,
  1545. bool Back)
  1546. {
  1547. if (FHistoryMenu[Side == osLocal][Back] != NULL)
  1548. {
  1549. TCustomDirView * DView = DirView(Side);
  1550. TTBXPopupMenu * Menu = FHistoryMenu[Side == osLocal][Back];
  1551. int ICount = Back ? DView->BackCount : DView->ForwardCount;
  1552. if (ICount > 10)
  1553. {
  1554. ICount = 10;
  1555. }
  1556. Menu->Items->Clear();
  1557. THistoryItemData Data;
  1558. Data.Side = Side;
  1559. for (short int i = 1; i <= ICount; i++)
  1560. {
  1561. TTBCustomItem * Item = new TTBXItem(Menu);
  1562. Data.Index = static_cast<short int>(i * (Back ? -1 : 1));
  1563. Item->Caption = MinimizeName(DView->HistoryPath[Data.Index], 50, (Side == osRemote));
  1564. Item->Hint = DView->HistoryPath[Data.Index];
  1565. DebugAssert(sizeof(int) == sizeof(THistoryItemData));
  1566. Item->Tag = *reinterpret_cast<int*>(&Data);
  1567. Item->OnClick = HistoryItemClick;
  1568. Menu->Items->Add(Item);
  1569. }
  1570. }
  1571. }
  1572. //---------------------------------------------------------------------------
  1573. TTBXPopupMenu * __fastcall TCustomScpExplorerForm::HistoryMenu(
  1574. TOperationSide Side, bool Back)
  1575. {
  1576. if (FHistoryMenu[Side == osLocal][Back] == NULL)
  1577. {
  1578. // workaround
  1579. // In Pascal the size of TTBXPopupMenu is 132, in C++ 136,
  1580. // operator new allocates memory in Pascal code, but calls inline
  1581. // contructor in C++, leading in problems, the function does
  1582. // both in Pascal code
  1583. FHistoryMenu[Side == osLocal][Back] = CreateTBXPopupMenu(this);
  1584. UpdateHistoryMenu(Side, Back);
  1585. }
  1586. return FHistoryMenu[Side == osLocal][Back];
  1587. }
  1588. //---------------------------------------------------------------------------
  1589. void __fastcall TCustomScpExplorerForm::DirViewHistoryChange(
  1590. TCustomDirView *Sender)
  1591. {
  1592. TOperationSide Side = (Sender == DirView(osRemote) ? osRemote : osLocal);
  1593. UpdateHistoryMenu(Side, true);
  1594. UpdateHistoryMenu(Side, false);
  1595. }
  1596. //---------------------------------------------------------------------------
  1597. bool __fastcall TCustomScpExplorerForm::CustomCommandRemoteAllowed()
  1598. {
  1599. // remote custom commands can be executed only if the server supports shell commands
  1600. // or have secondary shell
  1601. return (FTerminal != NULL) && (FTerminal->IsCapable[fcSecondaryShell] || FTerminal->IsCapable[fcShellAnyCommand]);
  1602. }
  1603. //---------------------------------------------------------------------------
  1604. int __fastcall TCustomScpExplorerForm::CustomCommandState(
  1605. const TCustomCommandType & Command, bool OnFocused, TCustomCommandListType ListType)
  1606. {
  1607. int Result;
  1608. TFileCustomCommand RemoteCustomCommand;
  1609. TLocalCustomCommand LocalCustomCommand;
  1610. TFileCustomCommand * NonInteractiveCustomCommand =
  1611. FLAGCLEAR(Command.Params, ccLocal) ? &RemoteCustomCommand : &LocalCustomCommand;
  1612. TInteractiveCustomCommand InteractiveCustomCommand(NonInteractiveCustomCommand);
  1613. UnicodeString Cmd = InteractiveCustomCommand.Complete(Command.Command, false);
  1614. if (FLAGCLEAR(Command.Params, ccLocal))
  1615. {
  1616. int AllowedState = CustomCommandRemoteAllowed() ? 1 : 0;
  1617. // custom command that does not operate with files can be executed anytime ...
  1618. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1619. {
  1620. if ((ListType == ccltAll) || (ListType == ccltNonFile))
  1621. {
  1622. // ... but do not show such command in remote file menu (TODO)
  1623. Result = AllowedState;
  1624. }
  1625. else
  1626. {
  1627. Result = -1;
  1628. }
  1629. }
  1630. else
  1631. {
  1632. if ((ListType == ccltAll) || (ListType == ccltFile))
  1633. {
  1634. Result = ((FCurrentSide == osRemote) && DirView(osRemote)->AnyFileSelected(OnFocused, false, true)) ? AllowedState : 0;
  1635. }
  1636. else
  1637. {
  1638. Result = -1;
  1639. }
  1640. }
  1641. }
  1642. else
  1643. {
  1644. // custom command that does not operate with files can be executed anytime
  1645. if (!NonInteractiveCustomCommand->IsFileCommand(Cmd))
  1646. {
  1647. Result = ((ListType == ccltAll) || (ListType == ccltNonFile)) ? 1 : -1;
  1648. }
  1649. else if (LocalCustomCommand.HasLocalFileName(Cmd))
  1650. {
  1651. if ((ListType == ccltAll) || (ListType == ccltFile))
  1652. {
  1653. // special case is "diff"-style command that can be executed over any side,
  1654. // if we have both sides
  1655. Result =
  1656. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1657. // directly (instead of EnableSelectedOperation) to pass
  1658. // false to FocusedFileOnlyWhenFocused when panel is inactive.
  1659. ((HasDirView[osLocal] && DirView(osLocal)->AnyFileSelected(false, false, (FCurrentSide == osLocal))) &&
  1660. DirView(osRemote)->AnyFileSelected(false, false, (FCurrentSide == osRemote))) ? 1 : 0;
  1661. }
  1662. else if (ListType == ccltBoth)
  1663. {
  1664. Result = 1;
  1665. }
  1666. else
  1667. {
  1668. Result = -1;
  1669. }
  1670. }
  1671. else
  1672. {
  1673. if ((ListType == ccltAll) || (ListType == ccltFile))
  1674. {
  1675. Result = DirView(FCurrentSide)->AnyFileSelected(OnFocused, false, true) ? 1 : 0;
  1676. }
  1677. else
  1678. {
  1679. Result = -1;
  1680. }
  1681. }
  1682. }
  1683. return Result;
  1684. }
  1685. //---------------------------------------------------------------------------
  1686. void __fastcall TCustomScpExplorerForm::RemoteCustomCommand(
  1687. TStrings * FileList, const TCustomCommandType & ACommand,
  1688. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  1689. {
  1690. if (EnsureCommandSessionFallback(fcShellAnyCommand))
  1691. {
  1692. TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
  1693. TWinInteractiveCustomCommand InteractiveCustomCommand(
  1694. &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
  1695. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  1696. Configuration->Usage->Inc(L"RemoteCustomCommandRuns2");
  1697. bool Capture =
  1698. FLAGSET(ACommand.Params, ccShowResults) ||
  1699. FLAGSET(ACommand.Params, ccCopyResults) ||
  1700. FLAGSET(ACommand.Params, ccShowResultsInMsgBox);
  1701. TCaptureOutputEvent OutputEvent = NULL;
  1702. DebugAssert(FCapturedLog == NULL);
  1703. if (Capture)
  1704. {
  1705. FCapturedLog = new TStringList();
  1706. OutputEvent = TerminalCaptureLog;
  1707. }
  1708. try
  1709. {
  1710. if (!RemoteCustomCommand.IsFileCommand(Command))
  1711. {
  1712. Terminal->AnyCommand(RemoteCustomCommand.Complete(Command, true),
  1713. OutputEvent);
  1714. }
  1715. else
  1716. {
  1717. Terminal->CustomCommandOnFiles(Command, ACommand.Params, FileList, OutputEvent);
  1718. }
  1719. if ((FCapturedLog != NULL) && (FCapturedLog->Count > 0))
  1720. {
  1721. if (FLAGSET(ACommand.Params, ccCopyResults))
  1722. {
  1723. CopyToClipboard(FCapturedLog);
  1724. }
  1725. if (FLAGSET(ACommand.Params, ccShowResults))
  1726. {
  1727. DoConsoleDialog(Terminal, L"", FCapturedLog);
  1728. }
  1729. else if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox))
  1730. {
  1731. MessageDialog(FCapturedLog->Text, qtInformation, qaOK);
  1732. }
  1733. }
  1734. }
  1735. __finally
  1736. {
  1737. SAFE_DESTROY(FCapturedLog);
  1738. }
  1739. }
  1740. }
  1741. //---------------------------------------------------------------------------
  1742. void __fastcall TCustomScpExplorerForm::LocalCustomCommandPure(
  1743. TStrings * FileList, const TCustomCommandType & ACommand, const UnicodeString & Command, TStrings * ALocalFileList,
  1744. const TCustomCommandData & Data, bool LocalFileCommand, bool FileListCommand, UnicodeString * POutput)
  1745. {
  1746. TStrings * LocalFileList = NULL;
  1747. TStrings * RemoteFileList = NULL;
  1748. TStrings * RemoteFileListFull = NULL;
  1749. try
  1750. {
  1751. if (LocalFileCommand)
  1752. {
  1753. if (ALocalFileList == NULL)
  1754. {
  1755. DebugAssert(HasDirView[osLocal]);
  1756. // Cannot have focus on both panels, so we have to call AnyFileSelected
  1757. // directly (instead of EnableSelectedOperation) to pass
  1758. // false to FocusedFileOnlyWhenFocused
  1759. DebugAssert(DirView(osLocal)->AnyFileSelected(false, false, false));
  1760. LocalFileList = DirView(osLocal)->CreateFileList(false, true, NULL);
  1761. }
  1762. else
  1763. {
  1764. LocalFileList = ALocalFileList;
  1765. }
  1766. if (FileListCommand)
  1767. {
  1768. if (LocalFileList->Count != 1)
  1769. {
  1770. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH1));
  1771. }
  1772. }
  1773. else
  1774. {
  1775. if ((LocalFileList->Count != 1) &&
  1776. (FileList->Count != 1) &&
  1777. (LocalFileList->Count != FileList->Count))
  1778. {
  1779. throw Exception(LoadStr(CUSTOM_COMMAND_SELECTED_UNMATCH));
  1780. }
  1781. }
  1782. }
  1783. UnicodeString RootTempDir;
  1784. UnicodeString TempDir;
  1785. bool RemoteFiles = FLAGSET(ACommand.Params, ccRemoteFiles);
  1786. if (!RemoteFiles)
  1787. {
  1788. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, true);
  1789. }
  1790. try
  1791. {
  1792. TDateTimes RemoteFileTimes;
  1793. if (RemoteFiles)
  1794. {
  1795. RemoteFileList = FileList;
  1796. }
  1797. else
  1798. {
  1799. RemoteFileList = new TStringList();
  1800. TMakeLocalFileListParams MakeFileListParam;
  1801. MakeFileListParam.FileList = RemoteFileList;
  1802. MakeFileListParam.FileTimes = &RemoteFileTimes;
  1803. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  1804. MakeFileListParam.Recursive =
  1805. FLAGSET(ACommand.Params, ccRecursive) && !FileListCommand;
  1806. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1807. if (!MakeFileListParam.Recursive)
  1808. {
  1809. RemoteFileTimes.clear();
  1810. RemoteFileListFull = new TStringList();
  1811. MakeFileListParam.FileList = RemoteFileListFull;
  1812. MakeFileListParam.Recursive = true;
  1813. MakeFileListParam.IncludeDirs = false;
  1814. ProcessLocalDirectory(TempDir, Terminal->MakeLocalFileList, &MakeFileListParam);
  1815. }
  1816. else
  1817. {
  1818. RemoteFileListFull = RemoteFileList;
  1819. }
  1820. }
  1821. bool NonBlocking = FileListCommand && RemoteFiles && !POutput;
  1822. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  1823. if (!NonBlocking)
  1824. {
  1825. int Count;
  1826. if (FileListCommand)
  1827. {
  1828. Count = 1;
  1829. }
  1830. else if (LocalFileCommand)
  1831. {
  1832. Count = std::max(LocalFileList->Count, RemoteFileList->Count);
  1833. }
  1834. else
  1835. {
  1836. Count = RemoteFileList->Count;
  1837. }
  1838. Progress.Start(foCustomCommand, osRemote, Count);
  1839. DebugAssert(FProgressForm != NULL);
  1840. FProgressForm->ReadOnly = true;
  1841. }
  1842. try
  1843. {
  1844. if (FileListCommand)
  1845. {
  1846. UnicodeString LocalFile;
  1847. // MakeFileList does not delimit filenames
  1848. UnicodeString FileList = MakeFileList(RemoteFileList);
  1849. if (LocalFileCommand)
  1850. {
  1851. DebugAssert(LocalFileList->Count == 1);
  1852. LocalFile = LocalFileList->Strings[0];
  1853. }
  1854. TLocalCustomCommand CustomCommand(Data,
  1855. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
  1856. UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
  1857. if (NonBlocking)
  1858. {
  1859. DebugAssert(!POutput);
  1860. ExecuteShellChecked(ShellCommand);
  1861. }
  1862. else
  1863. {
  1864. ExecuteProcessCheckedAndWait(ShellCommand, HelpKeyword, POutput);
  1865. }
  1866. }
  1867. else if (LocalFileCommand)
  1868. {
  1869. if (LocalFileList->Count == 1)
  1870. {
  1871. UnicodeString LocalFile = LocalFileList->Strings[0];
  1872. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1873. {
  1874. UnicodeString FileName = RemoteFileList->Strings[Index];
  1875. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1876. Progress.SetFile(FileNameForProgress);
  1877. TLocalCustomCommand CustomCommand(Data,
  1878. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
  1879. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1880. TOnceDoneOperation OnceDoneOperation;
  1881. // Do not unselect anything, as it won't work with two panels anyway
  1882. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1883. }
  1884. }
  1885. else if (RemoteFileList->Count == 1)
  1886. {
  1887. UnicodeString FileName = RemoteFileList->Strings[0];
  1888. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1889. {
  1890. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1891. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1892. Progress.SetFile(LocalFileNameOnly);
  1893. TLocalCustomCommand CustomCommand(
  1894. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1895. FileName, LocalFileName, L"");
  1896. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1897. TOnceDoneOperation OnceDoneOperation;
  1898. // Do not unselect anything, as it won't work with two panels anyway
  1899. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1900. }
  1901. }
  1902. else
  1903. {
  1904. if (LocalFileList->Count != RemoteFileList->Count)
  1905. {
  1906. throw Exception(LoadStr(CUSTOM_COMMAND_PAIRS_DOWNLOAD_FAILED));
  1907. }
  1908. for (int Index = 0; Index < LocalFileList->Count; Index++)
  1909. {
  1910. UnicodeString FileName = RemoteFileList->Strings[Index];
  1911. UnicodeString LocalFileName = LocalFileList->Strings[Index];
  1912. UnicodeString LocalFileNameOnly = ExtractFileName(LocalFileName);
  1913. Progress.SetFile(LocalFileNameOnly); // sic
  1914. TLocalCustomCommand CustomCommand(
  1915. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1916. FileName, LocalFileName, L"");
  1917. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1918. TOnceDoneOperation OnceDoneOperation;
  1919. // Do not unselect anything, as it won't work with two panels anyway
  1920. Progress.Finish(UnicodeString(), true, OnceDoneOperation);
  1921. }
  1922. }
  1923. }
  1924. else
  1925. {
  1926. for (int Index = 0; Index < RemoteFileList->Count; Index++)
  1927. {
  1928. UnicodeString FileName = RemoteFileList->Strings[Index];
  1929. UnicodeString FileNameForProgress = RemoteFiles ? FileName : ExtractFileName(FileName);
  1930. Progress.SetFile(FileNameForProgress);
  1931. TLocalCustomCommand CustomCommand(Data,
  1932. Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  1933. FileName, L"", L"");
  1934. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  1935. TOnceDoneOperation OnceDoneOperation;
  1936. Progress.Finish(FileNameForProgress, true, OnceDoneOperation);
  1937. }
  1938. }
  1939. }
  1940. __finally
  1941. {
  1942. if (!NonBlocking)
  1943. {
  1944. Progress.Stop();
  1945. }
  1946. }
  1947. DebugAssert(!FAutoOperation);
  1948. if (!RemoteFiles)
  1949. {
  1950. TempDir = IncludeTrailingBackslash(TempDir);
  1951. for (int Index = 0; Index < RemoteFileListFull->Count; Index++)
  1952. {
  1953. UnicodeString FileName = RemoteFileListFull->Strings[Index];
  1954. if (DebugAlwaysTrue(SameText(TempDir, FileName.SubString(1, TempDir.Length()))) &&
  1955. // Skip directories, process only nested files.
  1956. // The check is redundant as FileAge fails for directories anyway.
  1957. !DirectoryExists(FileName))
  1958. {
  1959. UnicodeString RemoteDir =
  1960. UnixExtractFileDir(
  1961. UnixIncludeTrailingBackslash(FTerminal->CurrentDirectory) +
  1962. ToUnixPath(FileName.SubString(TempDir.Length() + 1, FileName.Length() - TempDir.Length())));
  1963. TDateTime NewTime;
  1964. if (FileAge(FileName, NewTime) &&
  1965. (NewTime != RemoteFileTimes[Index]))
  1966. {
  1967. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  1968. TemporaryFileCopyParam(CopyParam);
  1969. CopyParam.FileMask = L"";
  1970. FAutoOperation = true;
  1971. std::unique_ptr<TStrings> TemporaryFilesList(new TStringList());
  1972. TemporaryFilesList->Add(FileName);
  1973. FTerminal->CopyToRemote(TemporaryFilesList.get(), RemoteDir, &CopyParam, cpTemporary, NULL);
  1974. }
  1975. }
  1976. }
  1977. }
  1978. }
  1979. __finally
  1980. {
  1981. FAutoOperation = false;
  1982. if (!RootTempDir.IsEmpty() && DebugAlwaysTrue(!RemoteFiles))
  1983. {
  1984. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  1985. }
  1986. }
  1987. }
  1988. __finally
  1989. {
  1990. if (RemoteFileList != FileList)
  1991. {
  1992. delete RemoteFileList;
  1993. }
  1994. if (LocalFileList != ALocalFileList)
  1995. {
  1996. delete LocalFileList;
  1997. }
  1998. if (RemoteFileListFull != RemoteFileList)
  1999. {
  2000. delete RemoteFileListFull;
  2001. }
  2002. }
  2003. }
  2004. //---------------------------------------------------------------------------
  2005. void __fastcall TCustomScpExplorerForm::LocalCustomCommandWithLocalFiles(
  2006. const TCustomCommandType & ACommand, const UnicodeString & Command, const TCustomCommandData & Data,
  2007. bool FileListCommand, UnicodeString * POutput)
  2008. {
  2009. std::unique_ptr<TStrings> SelectedFileList(DirView(osLocal)->CreateFileList(false, true, NULL));
  2010. std::unique_ptr<TStrings> LocalFileList(new TStringList());
  2011. for (int Index = 0; Index < SelectedFileList->Count; Index++)
  2012. {
  2013. UnicodeString FileName = SelectedFileList->Strings[Index];
  2014. if (DirectoryExists(FileName))
  2015. {
  2016. if (FLAGSET(ACommand.Params, ccApplyToDirectories))
  2017. {
  2018. LocalFileList->Add(FileName);
  2019. }
  2020. if (FLAGSET(ACommand.Params, ccRecursive))
  2021. {
  2022. TMakeLocalFileListParams MakeFileListParam;
  2023. MakeFileListParam.FileList = LocalFileList.get();
  2024. MakeFileListParam.FileTimes = NULL;
  2025. MakeFileListParam.IncludeDirs = FLAGSET(ACommand.Params, ccApplyToDirectories);
  2026. MakeFileListParam.Recursive = true;
  2027. ProcessLocalDirectory(FileName, Terminal->MakeLocalFileList, &MakeFileListParam);
  2028. }
  2029. }
  2030. else
  2031. {
  2032. LocalFileList->Add(FileName);
  2033. }
  2034. }
  2035. if (FileListCommand)
  2036. {
  2037. UnicodeString FileList = MakeFileList(LocalFileList.get());
  2038. TLocalCustomCommand CustomCommand(
  2039. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2040. L"", L"", FileList);
  2041. ExecuteProcessChecked(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2042. }
  2043. else
  2044. {
  2045. TFileOperationProgressType Progress(&OperationProgress, &OperationFinished);
  2046. Progress.Start(foCustomCommand, osLocal, LocalFileList->Count);
  2047. DebugAssert(FProgressForm != NULL);
  2048. FProgressForm->ReadOnly = true;
  2049. try
  2050. {
  2051. for (int Index = 0; Index < LocalFileList->Count; Index++)
  2052. {
  2053. UnicodeString FileName = LocalFileList->Strings[Index];
  2054. Progress.SetFile(FileName);
  2055. TLocalCustomCommand CustomCommand(
  2056. Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
  2057. FileName, L"", L"");
  2058. ExecuteProcessCheckedAndWait(CustomCommand.Complete(Command, true), HelpKeyword, POutput);
  2059. TOnceDoneOperation OnceDoneOperation;
  2060. Progress.Finish(FileName, true, OnceDoneOperation);
  2061. }
  2062. }
  2063. __finally
  2064. {
  2065. Progress.Stop();
  2066. }
  2067. }
  2068. }
  2069. //---------------------------------------------------------------------------
  2070. void __fastcall TCustomScpExplorerForm::LocalCustomCommand(TStrings * FileList,
  2071. const TCustomCommandType & ACommand, TStrings * ALocalFileList,
  2072. const TCustomCommandData & Data, const UnicodeString & CommandCommand)
  2073. {
  2074. TLocalCustomCommand LocalCustomCommand(Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory());
  2075. TWinInteractiveCustomCommand InteractiveCustomCommand(
  2076. &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
  2077. UnicodeString Command = InteractiveCustomCommand.Complete(CommandCommand, false);
  2078. bool FileListCommand = LocalCustomCommand.IsFileListCommand(Command);
  2079. bool LocalFileCommand = LocalCustomCommand.HasLocalFileName(Command);
  2080. Configuration->Usage->Inc(L"LocalCustomCommandRuns2");
  2081. std::unique_ptr<UnicodeString> POutput;
  2082. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) ||
  2083. FLAGSET(ACommand.Params, ccCopyResults))
  2084. {
  2085. POutput.reset(new UnicodeString());
  2086. }
  2087. if (!LocalCustomCommand.IsFileCommand(Command))
  2088. {
  2089. ExecuteProcessChecked(LocalCustomCommand.Complete(Command, true), HelpKeyword, POutput.get());
  2090. }
  2091. // remote files?
  2092. else if ((FCurrentSide == osRemote) || LocalFileCommand)
  2093. {
  2094. LocalCustomCommandPure(FileList, ACommand, Command, ALocalFileList, Data, LocalFileCommand, FileListCommand, POutput.get());
  2095. }
  2096. // local files
  2097. else
  2098. {
  2099. LocalCustomCommandWithLocalFiles(ACommand, Command, Data, FileListCommand, POutput.get());
  2100. }
  2101. if (POutput.get() != NULL)
  2102. {
  2103. // If the output is single-line, we do not want the trailing CRLF, as the CopyToClipboard(TStrings *) does
  2104. int P = POutput->Pos(sLineBreak);
  2105. if (P == POutput->Length() - static_cast<int>(strlen(sLineBreak)) + 1)
  2106. {
  2107. POutput->SetLength(P - 1);
  2108. }
  2109. // Copy even empty output to clipboard
  2110. if (FLAGSET(ACommand.Params, ccCopyResults))
  2111. {
  2112. CopyToClipboard(*POutput);
  2113. }
  2114. // But do not show an empty message box.
  2115. // This way the ShowResultsInMsgBox can be used to suppress a console window of commands with no output
  2116. if (FLAGSET(ACommand.Params, ccShowResultsInMsgBox) &&
  2117. !POutput->IsEmpty())
  2118. {
  2119. TClipboardHandler ClipboardHandler;
  2120. ClipboardHandler.Text = *POutput;
  2121. TMessageParams Params;
  2122. TQueryButtonAlias Aliases[1];
  2123. Aliases[0].Button = qaRetry;
  2124. Aliases[0].Alias = LoadStr(EDIT_COPY);
  2125. Aliases[0].OnSubmit = &ClipboardHandler.Copy;
  2126. Params.Aliases = Aliases;
  2127. Params.AliasesCount = LENOF(Aliases);
  2128. MessageDialog(*POutput, qtInformation, qaOK | qaRetry, HelpKeyword, &Params);
  2129. }
  2130. }
  2131. }
  2132. //---------------------------------------------------------------------------
  2133. void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
  2134. const TCustomCommandType & ACommand, TStrings * ALocalFileList)
  2135. {
  2136. TCustomCommandData Data(Terminal);
  2137. UnicodeString Site = Terminal->SessionData->SessionKey;
  2138. UnicodeString HelpKeyword = ACommand.HomePage;
  2139. std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
  2140. if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
  2141. {
  2142. std::unique_ptr<TCustomCommand> CustomCommandForOptions;
  2143. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2144. {
  2145. CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, Terminal->CurrentDirectory));
  2146. }
  2147. else
  2148. {
  2149. CustomCommandForOptions.reset(new TLocalCustomCommand(Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory()));
  2150. }
  2151. if (!DoCustomCommandOptionsDialog(
  2152. &ACommand, CustomCommandOptions.get(), NULL, TCustomCommandType::ofRun, CustomCommandForOptions.get(), Site, NULL))
  2153. {
  2154. Abort();
  2155. }
  2156. }
  2157. UnicodeString CommandCommand = ACommand.GetCommandWithExpandedOptions(CustomCommandOptions.get(), Site);
  2158. if (FLAGCLEAR(ACommand.Params, ccLocal))
  2159. {
  2160. RemoteCustomCommand(FileList, ACommand, Data, CommandCommand);
  2161. }
  2162. else
  2163. {
  2164. LocalCustomCommand(FileList, ACommand, ALocalFileList, Data, CommandCommand);
  2165. }
  2166. }
  2167. //---------------------------------------------------------------------------
  2168. void __fastcall TCustomScpExplorerForm::BothCustomCommand(
  2169. const TCustomCommandType & Command)
  2170. {
  2171. DebugAssert(FCustomCommandLocalFileList != NULL);
  2172. DebugAssert(FCustomCommandRemoteFileList != NULL);
  2173. DebugAssert(FCustomCommandLocalFileList->Count == FCustomCommandRemoteFileList->Count);
  2174. TStrings * LocalFileList = new TStringList();
  2175. TStrings * RemoteFileList = new TStringList();
  2176. try
  2177. {
  2178. for (int Index = 0; Index < FCustomCommandLocalFileList->Count; Index++)
  2179. {
  2180. LocalFileList->Clear();
  2181. LocalFileList->AddObject(
  2182. FCustomCommandLocalFileList->Strings[Index],
  2183. FCustomCommandLocalFileList->Objects[Index]);
  2184. RemoteFileList->Clear();
  2185. RemoteFileList->AddObject(
  2186. FCustomCommandRemoteFileList->Strings[Index],
  2187. FCustomCommandRemoteFileList->Objects[Index]);
  2188. CustomCommand(RemoteFileList, Command, LocalFileList);
  2189. }
  2190. }
  2191. __finally
  2192. {
  2193. delete LocalFileList;
  2194. delete RemoteFileList;
  2195. }
  2196. }
  2197. //---------------------------------------------------------------------------
  2198. void __fastcall TCustomScpExplorerForm::CustomCommandMenu(
  2199. TAction * Action, TStrings * LocalFileList, TStrings * RemoteFileList)
  2200. {
  2201. delete FCustomCommandLocalFileList;
  2202. delete FCustomCommandRemoteFileList;
  2203. // takeover ownership,
  2204. // the lists must survive the MenuPopup as OnClick occurs only after it exits
  2205. FCustomCommandLocalFileList = LocalFileList;
  2206. FCustomCommandRemoteFileList = RemoteFileList;
  2207. TButton * Button = dynamic_cast<TButton *>(Action->ActionComponent);
  2208. if (Button != NULL)
  2209. {
  2210. FCustomCommandMenu->Items->Clear();
  2211. NonVisualDataModule->CreateCustomCommandsMenu(FCustomCommandMenu->Items, false, false, ccltBoth);
  2212. MenuPopup(FCustomCommandMenu, Button);
  2213. }
  2214. else
  2215. {
  2216. NonVisualDataModule->CreateCustomCommandsMenu(Action, false, ccltBoth);
  2217. }
  2218. }
  2219. //---------------------------------------------------------------------------
  2220. void __fastcall TCustomScpExplorerForm::TerminalCaptureLog(
  2221. const UnicodeString & AddedLine, TCaptureOutputType OutputType)
  2222. {
  2223. DebugAssert(FCapturedLog != NULL);
  2224. if ((OutputType == cotOutput) || (OutputType == cotError))
  2225. {
  2226. FCapturedLog->Add(AddedLine);
  2227. }
  2228. }
  2229. //---------------------------------------------------------------------------
  2230. bool __fastcall TCustomScpExplorerForm::IsFileControl(TObject * Control,
  2231. TOperationSide Side)
  2232. {
  2233. return (Side == osRemote) &&
  2234. ((Control == RemoteDirView) || (Control == RemoteDriveView));
  2235. }
  2236. //---------------------------------------------------------------------------
  2237. void __fastcall TCustomScpExplorerForm::DirViewContextPopupDefaultItem(
  2238. TOperationSide Side, TTBXCustomItem * Item, TDoubleClickAction DoubleClickAction)
  2239. {
  2240. TTBItemOptions O;
  2241. O = Item->Options;
  2242. TCustomDirView * DView = DirView(Side);
  2243. if ((DView->ItemFocused != NULL) &&
  2244. (WinConfiguration->DoubleClickAction == DoubleClickAction) &&
  2245. // when resolving links is disabled, default action is to enter the directory,
  2246. // no matter what DoubleClickAction is configured to
  2247. ((Side != osRemote) || Terminal->ResolvingSymlinks || Terminal->IsEncryptingFiles()) &&
  2248. // Can only Edit files, but can Open/Copy even directories
  2249. ((DoubleClickAction != dcaEdit) ||
  2250. !DView->ItemIsDirectory(DView->ItemFocused)))
  2251. {
  2252. Item->Options = O << tboDefault;
  2253. }
  2254. else
  2255. {
  2256. Item->Options = O >> tboDefault;
  2257. }
  2258. }
  2259. //---------------------------------------------------------------------------
  2260. void __fastcall TCustomScpExplorerForm::DirViewContextPopup(
  2261. TOperationSide Side, Byte PopupComponent, const TPoint &MousePos)
  2262. {
  2263. TCustomDirView * DView = DirView(Side);
  2264. TListItem * Item = DView->ItemFocused;
  2265. if ((DView->GetItemAt(MousePos.x, MousePos.y) == Item) &&
  2266. EnableFocusedOperation[Side])
  2267. {
  2268. TPoint ClientPoint;
  2269. ClientPoint = ((MousePos.x < 0) && (MousePos.y < 0)) ?
  2270. TPoint(0, 0) : MousePos;
  2271. FLastContextPopupScreenPoint = DView->ClientToScreen(ClientPoint);
  2272. reinterpret_cast<TPopupMenu*>(GetComponent(PopupComponent))->Popup(
  2273. FLastContextPopupScreenPoint.x, FLastContextPopupScreenPoint.y);
  2274. }
  2275. }
  2276. //---------------------------------------------------------------------------
  2277. void __fastcall TCustomScpExplorerForm::RemoteDirViewContextPopup(
  2278. TObject * /*Sender*/, const TPoint &MousePos, bool &Handled)
  2279. {
  2280. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteOpenMenuItem, dcaOpen);
  2281. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteEditMenuItem, dcaEdit);
  2282. DirViewContextPopupDefaultItem(osRemote, NonVisualDataModule->RemoteCopyMenuItem, dcaCopy);
  2283. DirViewContextPopup(osRemote, fcRemotePopup, MousePos);
  2284. Handled = true;
  2285. }
  2286. //---------------------------------------------------------------------------
  2287. void __fastcall TCustomScpExplorerForm::ReloadLocalDirectory(const UnicodeString Directory)
  2288. {
  2289. }
  2290. //---------------------------------------------------------------------------
  2291. void __fastcall TCustomScpExplorerForm::BatchStart(void *& /*Storage*/)
  2292. {
  2293. DebugAssert(FErrorList == NULL);
  2294. if (WinConfiguration->ContinueOnError)
  2295. {
  2296. FErrorList = new TStringList();
  2297. Configuration->Usage->Inc(L"ContinuationsOnError");
  2298. }
  2299. NonVisualDataModule->StartBusy();
  2300. }
  2301. //---------------------------------------------------------------------------
  2302. void __fastcall TCustomScpExplorerForm::BatchEnd(void * /*Storage*/)
  2303. {
  2304. NonVisualDataModule->EndBusy();
  2305. if (FErrorList)
  2306. {
  2307. HandleErrorList(FErrorList);
  2308. }
  2309. }
  2310. //---------------------------------------------------------------------------
  2311. void __fastcall TCustomScpExplorerForm::UpdateCopyParamCounters(
  2312. const TCopyParamType & CopyParam)
  2313. {
  2314. if (!CopyParam.IncludeFileMask.Masks.IsEmpty())
  2315. {
  2316. Configuration->Usage->Inc(L"FileMaskUses");
  2317. }
  2318. if (IsEffectiveFileNameMask(CopyParam.FileMask))
  2319. {
  2320. Configuration->Usage->Inc(L"OperationMaskUses");
  2321. }
  2322. }
  2323. //---------------------------------------------------------------------------
  2324. bool __fastcall TCustomScpExplorerForm::ExecuteCopyMoveFileOperation(
  2325. TFileOperation Operation, TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * AParam)
  2326. {
  2327. TTransferDirection Direction = (Side == osLocal ? tdToRemote : tdToLocal);
  2328. TTransferType Type = (Operation == foCopy ? ttCopy : ttMove);
  2329. TTransferOperationParam DefaultParam;
  2330. TTransferOperationParam & Param = (AParam != NULL) ? *static_cast<TTransferOperationParam *>(AParam) : DefaultParam;
  2331. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2332. switch (Param.Queue)
  2333. {
  2334. case asOn:
  2335. CopyParam.Queue = true;
  2336. break;
  2337. case asOff:
  2338. CopyParam.Queue = false;
  2339. break;
  2340. case asAuto:
  2341. default:
  2342. // keep default
  2343. break;
  2344. }
  2345. bool Result =
  2346. CopyParamDialog(Direction, Type, Param.Temp, FileList, Param.TargetDirectory, CopyParam, !NoConfirmation, Param.DragDrop, Param.Options);
  2347. if (Result)
  2348. {
  2349. DebugAssert(Terminal);
  2350. bool SelectionRestored = false;
  2351. TCustomDirView * DView = NULL;
  2352. if (HasDirView[Side])
  2353. {
  2354. DView = DirView(Side);
  2355. DView->SaveSelection();
  2356. DView->SaveSelectedNames();
  2357. }
  2358. UpdateCopyParamCounters(CopyParam);
  2359. std::unique_ptr<TStringList> TransferResumeList(new TStringList());
  2360. DebugAssert(FTransferResumeList == NULL);
  2361. FTransferResumeList = Terminal->IsCapable[fcMoveToQueue] ? TransferResumeList.get() : NULL;
  2362. FMoveToQueue = false;
  2363. int Params = FLAGMASK(Operation == foMove, cpDelete);
  2364. try
  2365. {
  2366. TStrings * PermanentFileList;
  2367. std::unique_ptr<TStrings> PermanentFileListOwner;
  2368. try
  2369. {
  2370. if (Side == osLocal)
  2371. {
  2372. PermanentFileList = FileList;
  2373. Params |= FLAGMASK(Param.Temp, cpTemporary);
  2374. Terminal->CopyToRemote(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2375. if (Operation == foMove)
  2376. {
  2377. ReloadLocalDirectory();
  2378. if (DView != NULL)
  2379. {
  2380. DView->RestoreSelection();
  2381. }
  2382. SelectionRestored = true;
  2383. }
  2384. }
  2385. else
  2386. {
  2387. // Clone the file list as it may refer to current directory files,
  2388. // which get destroyed, when the source directory is reloaded after foMove operation.
  2389. // We should actually clone the file list for whole ExecuteFileOperation to protect against reloads.
  2390. // But for a hotfix, we are not going to do such a big change.
  2391. PermanentFileListOwner.reset(TRemoteFileList::CloneStrings(FileList));
  2392. PermanentFileList = PermanentFileListOwner.get();
  2393. try
  2394. {
  2395. Terminal->CopyToLocal(FileList, Param.TargetDirectory, &CopyParam, Params, NULL);
  2396. }
  2397. __finally
  2398. {
  2399. if (Operation == foMove)
  2400. {
  2401. if (DView != NULL)
  2402. {
  2403. DView->RestoreSelection();
  2404. }
  2405. SelectionRestored = true;
  2406. }
  2407. ReloadLocalDirectory(Param.TargetDirectory);
  2408. }
  2409. }
  2410. }
  2411. catch (EAbort &)
  2412. {
  2413. if (FMoveToQueue)
  2414. {
  2415. Params |= (CopyParam.QueueNoConfirmation ? cpNoConfirmation : 0);
  2416. DebugAssert(CopyParam.TransferSkipList == NULL);
  2417. DebugAssert(CopyParam.TransferResumeFile.IsEmpty());
  2418. if (TransferResumeList->Count > 0)
  2419. {
  2420. CopyParam.TransferResumeFile = TransferResumeList->Strings[TransferResumeList->Count - 1];
  2421. TransferResumeList->Delete(TransferResumeList->Count - 1);
  2422. }
  2423. CopyParam.TransferSkipList = TransferResumeList.release();
  2424. // not really needed, just to keep it consistent with TransferResumeList
  2425. FTransferResumeList = NULL;
  2426. FMoveToQueue = false;
  2427. Configuration->Usage->Inc("MovesToBackground");
  2428. AddQueueItem(Queue, Direction, PermanentFileList, Param.TargetDirectory, CopyParam, Params);
  2429. ClearTransferSourceSelection(Direction);
  2430. }
  2431. throw;
  2432. }
  2433. }
  2434. __finally
  2435. {
  2436. if (!SelectionRestored && (DView != NULL))
  2437. {
  2438. DView->DiscardSavedSelection();
  2439. }
  2440. FTransferResumeList = NULL;
  2441. }
  2442. }
  2443. return Result;
  2444. }
  2445. //---------------------------------------------------------------------------
  2446. bool __fastcall TCustomScpExplorerForm::ExecuteDeleteFileOperation(
  2447. TOperationSide Side, TStrings * FileList, void * Param)
  2448. {
  2449. DebugAssert(FileList->Count);
  2450. // We deliberately do not toggle alternative flag (Param), but use OR,
  2451. // because the Param is set only when command is invoked using Shift-Del/F8 keyboard
  2452. // shortcut of CurrentDeleteAlternativeAction
  2453. bool Alternative =
  2454. bool(Param) || UseAlternativeFunction();
  2455. bool Recycle;
  2456. if (Side == osLocal)
  2457. {
  2458. Recycle = (WinConfiguration->DeleteToRecycleBin != Alternative);
  2459. }
  2460. else
  2461. {
  2462. Recycle =
  2463. (Terminal->SessionData->DeleteToRecycleBin != Alternative) &&
  2464. !Terminal->SessionData->RecycleBinPath.IsEmpty() &&
  2465. !Terminal->IsRecycledFile(FileList->Strings[0]);
  2466. }
  2467. bool Result = !(Recycle ? WinConfiguration->ConfirmRecycling : WinConfiguration->ConfirmDeleting);
  2468. if (!Result)
  2469. {
  2470. UnicodeString Query;
  2471. if (FileList->Count == 1)
  2472. {
  2473. if (Side == osLocal)
  2474. {
  2475. Query = ExtractFileName(FileList->Strings[0]);
  2476. }
  2477. else
  2478. {
  2479. Query = UnixExtractFileName(FileList->Strings[0]);
  2480. }
  2481. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILE : CONFIRM_DELETE_FILE), (Query));
  2482. }
  2483. else
  2484. {
  2485. Query = FMTLOAD((Recycle ? CONFIRM_RECYCLE_FILES : CONFIRM_DELETE_FILES), (FileList->Count));
  2486. }
  2487. TMessageParams Params(mpNeverAskAgainCheck);
  2488. Params.ImageName = L"Delete file";
  2489. unsigned int Answer =
  2490. MessageDialog(MainInstructions(Query), qtConfirmation, qaOK | qaCancel, HELP_DELETE_FILE, &Params);
  2491. if (Answer == qaNeverAskAgain)
  2492. {
  2493. Result = true;
  2494. if (Recycle)
  2495. {
  2496. WinConfiguration->ConfirmRecycling = false;
  2497. }
  2498. else
  2499. {
  2500. WinConfiguration->ConfirmDeleting = false;
  2501. }
  2502. }
  2503. else
  2504. {
  2505. Result = (Answer == qaOK);
  2506. }
  2507. }
  2508. if (Result)
  2509. {
  2510. DeleteFiles(Side, FileList, FLAGMASK(Alternative, dfAlternative));
  2511. }
  2512. return Result;
  2513. }
  2514. //---------------------------------------------------------------------------
  2515. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2516. TOperationSide Side, TStrings * FileList, bool NoConfirmation, void * Param)
  2517. {
  2518. TAutoBatch AutoBatch(this);
  2519. bool Result;
  2520. if ((Operation == foCopy) || (Operation == foMove))
  2521. {
  2522. Result = ExecuteCopyMoveFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2523. }
  2524. else if (Operation == foRename)
  2525. {
  2526. DebugAssert(DirView(Side)->ItemFocused);
  2527. DirView(Side)->ItemFocused->EditCaption();
  2528. Result = true;
  2529. }
  2530. else if (Operation == foDelete)
  2531. {
  2532. Result = ExecuteDeleteFileOperation(Side, FileList, Param);
  2533. }
  2534. else if (Operation == foSetProperties)
  2535. {
  2536. RemoteDirView->SaveSelectedNames();
  2537. Result = SetProperties(Side, FileList);
  2538. }
  2539. else if (Operation == foCustomCommand)
  2540. {
  2541. DebugAssert(Param);
  2542. DebugAssert(Side == osRemote);
  2543. RemoteDirView->SaveSelectedNames();
  2544. const TCustomCommandType * Command = static_cast<const TCustomCommandType*>(Param);
  2545. CustomCommand(FileList, *Command, NULL);
  2546. Result = true;
  2547. }
  2548. else if ((Operation == foRemoteMove) || (Operation == foRemoteCopy))
  2549. {
  2550. DebugAssert(Side == osRemote);
  2551. Result = RemoteTransferFiles(FileList, NoConfirmation,
  2552. (Operation == foRemoteMove), reinterpret_cast<TTerminal *>(Param));
  2553. }
  2554. else if (Operation == foLock)
  2555. {
  2556. DebugAssert(Side == osRemote);
  2557. LockFiles(FileList, true);
  2558. Result = true;
  2559. }
  2560. else if (Operation == foUnlock)
  2561. {
  2562. DebugAssert(Side == osRemote);
  2563. LockFiles(FileList, false);
  2564. Result = true;
  2565. }
  2566. else
  2567. {
  2568. DebugFail();
  2569. }
  2570. return Result;
  2571. }
  2572. //---------------------------------------------------------------------------
  2573. TOperationSide __fastcall TCustomScpExplorerForm::GetSide(TOperationSide Side)
  2574. {
  2575. if (Side == osCurrent)
  2576. {
  2577. Side = FCurrentSide;
  2578. }
  2579. return Side;
  2580. }
  2581. //---------------------------------------------------------------------------
  2582. bool __fastcall TCustomScpExplorerForm::ExecuteFileOperation(TFileOperation Operation,
  2583. TOperationSide Side, bool OnFocused, bool NoConfirmation, void * Param)
  2584. {
  2585. Side = GetSide(Side);
  2586. bool Result;
  2587. TStrings * FileList = DirView(Side)->CreateFileList(OnFocused, (Side == osLocal), NULL);
  2588. try
  2589. {
  2590. Result = ExecuteFileOperation(Operation, Side, FileList, NoConfirmation, Param);
  2591. }
  2592. __finally
  2593. {
  2594. delete FileList;
  2595. }
  2596. return Result;
  2597. }
  2598. //---------------------------------------------------------------------------
  2599. void __fastcall TCustomScpExplorerForm::ExecuteFileOperationCommand(
  2600. TFileOperation Operation, TOperationSide Side, bool OnFocused,
  2601. bool NoConfirmation, void * Param)
  2602. {
  2603. if (ExecuteFileOperation(Operation, Side, OnFocused, NoConfirmation, Param))
  2604. {
  2605. if ((Operation == foCopy) || (Operation == foMove))
  2606. {
  2607. if (GetSide(Side) == osLocal)
  2608. {
  2609. Configuration->Usage->Inc(L"UploadsCommand");
  2610. }
  2611. else
  2612. {
  2613. Configuration->Usage->Inc(L"DownloadsCommand");
  2614. }
  2615. }
  2616. }
  2617. }
  2618. //---------------------------------------------------------------------------
  2619. void __fastcall TCustomScpExplorerForm::ExecuteCopyOperationCommand(
  2620. TOperationSide Side, bool OnFocused, unsigned int Flags)
  2621. {
  2622. TTransferOperationParam Param;
  2623. if ((WinConfiguration->Interface != ifCommander) ||
  2624. WinConfiguration->ScpCommander.ExplorerKeyboardShortcuts)
  2625. {
  2626. Flags &= ~cocShortCutHint;
  2627. }
  2628. TCustomDirView * DView = DirView(Side);
  2629. Param.Options =
  2630. FLAGMASK(FLAGSET(Flags, cocShortCutHint), coShortCutHint) |
  2631. FLAGMASK(SelectedAllFilesInDirView(DView), coAllFiles);
  2632. if (FLAGSET(Flags, cocQueue))
  2633. {
  2634. Param.Queue = asOn;
  2635. }
  2636. else if (FLAGSET(Flags, cocNonQueue))
  2637. {
  2638. Param.Queue = asOff;
  2639. }
  2640. ExecuteFileOperationCommand(foCopy, Side, OnFocused, false, &Param);
  2641. }
  2642. //---------------------------------------------------------------------------
  2643. void __fastcall TCustomScpExplorerForm::HandleErrorList(TStringList *& ErrorList)
  2644. {
  2645. try
  2646. {
  2647. if (ErrorList->Count)
  2648. {
  2649. UnicodeString Message = MainInstructions(FMTLOAD(ERROR_LIST_COUNT, (ErrorList->Count)));
  2650. if (MessageDialog(Message, qtError,
  2651. qaOK | qaCancel, HELP_NONE) == qaOK)
  2652. {
  2653. unsigned int Answer;
  2654. int Index = 0;
  2655. do
  2656. {
  2657. DebugAssert(Index >= 0 && Index < ErrorList->Count);
  2658. TQueryButtonAlias Aliases[2];
  2659. Aliases[0].Button = qaYes;
  2660. Aliases[0].Alias = LoadStr(PREV_BUTTON);
  2661. Aliases[1].Button = qaNo;
  2662. Aliases[1].Alias = LoadStr(NEXT_BUTTON);
  2663. TMessageParams Params;
  2664. Params.Aliases = Aliases;
  2665. Params.AliasesCount = LENOF(Aliases);
  2666. Answer = MoreMessageDialog(
  2667. FMTLOAD(ERROR_LIST_NUMBER, (Index+1, ErrorList->Count, ErrorList->Strings[Index])),
  2668. dynamic_cast<TStrings *>(ErrorList->Objects[Index]), qtError,
  2669. (Index ? qaYes : 0) | (Index < ErrorList->Count - 1 ? qaNo : 0) |
  2670. qaOK, HELP_NONE, &Params);
  2671. if (Answer == qaNo)
  2672. {
  2673. Index++;
  2674. }
  2675. if (Answer == qaYes)
  2676. {
  2677. Index--;
  2678. }
  2679. }
  2680. while (Answer != qaOK);
  2681. }
  2682. }
  2683. }
  2684. __finally
  2685. {
  2686. TStrings * List = ErrorList;
  2687. ErrorList = NULL;
  2688. for (int i = 0; i < List->Count; i++)
  2689. {
  2690. delete List->Objects[i];
  2691. }
  2692. delete List;
  2693. }
  2694. }
  2695. //---------------------------------------------------------------------------
  2696. void __fastcall TCustomScpExplorerForm::ExecuteRemoteFile(
  2697. const UnicodeString & FullFileName, TRemoteFile * File, TExecuteFileBy ExecuteFileBy)
  2698. {
  2699. // needed for checking filemasks, as there's no directory object
  2700. // associated with the file object
  2701. File->FullFileName = FullFileName;
  2702. TFileMasks::TParams MaskParams;
  2703. MaskParams.Size = File->Size;
  2704. MaskParams.Modification = File->Modification;
  2705. ExecuteFile(osRemote, ExecuteFileBy, NULL, FullFileName, File, MaskParams);
  2706. }
  2707. //---------------------------------------------------------------------------
  2708. void __fastcall TCustomScpExplorerForm::EditNew(TOperationSide Side)
  2709. {
  2710. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2711. Side = GetSide(Side);
  2712. TCustomDirView * DView = DirView(Side);
  2713. TListItem * FocusedItem = DView->ItemFocused;
  2714. UnicodeString Name;
  2715. if ((FocusedItem != NULL) && !DView->ItemIsParentDirectory(FocusedItem))
  2716. {
  2717. Name = DView->ItemFileName(FocusedItem);
  2718. }
  2719. else
  2720. {
  2721. Name = LoadStr(NEW_FILE);
  2722. }
  2723. UnicodeString Names = Name;
  2724. std::unique_ptr<TStrings> History(CloneStrings(CustomWinConfiguration->History[L"EditFile"]));
  2725. if (InputDialog(LoadStr(EDIT_FILE_CAPTION), LoadStr(EDIT_FILE_PROMPT), Names,
  2726. HELP_EDIT_NEW, History.get(), true))
  2727. {
  2728. while (!Names.IsEmpty())
  2729. {
  2730. Name = CutToChar(Names, FileMasksDelimiters[1], false);
  2731. CustomWinConfiguration->History[L"EditFile"] = History.get();
  2732. UnicodeString TargetFileName;
  2733. UnicodeString LocalFileName;
  2734. UnicodeString RootTempDir;
  2735. UnicodeString TempDir;
  2736. UnicodeString RemoteDirectory;
  2737. bool ExistingFile = false;
  2738. if (Side == osRemote)
  2739. {
  2740. Name = AbsolutePath(FTerminal->CurrentDirectory, Name);
  2741. TRemoteFile * File = NULL;
  2742. if (FTerminal->FileExists(Name, &File))
  2743. {
  2744. try
  2745. {
  2746. ExecuteRemoteFile(Name, File, efDefaultEditor);
  2747. ExistingFile = true;
  2748. }
  2749. __finally
  2750. {
  2751. delete File;
  2752. }
  2753. }
  2754. if (!ExistingFile)
  2755. {
  2756. RemoteDirectory = UnixExtractFilePath(Name);
  2757. TemporaryDirectoryForRemoteFiles(
  2758. RemoteDirectory, GUIConfiguration->CurrentCopyParam, TempDir, RootTempDir);
  2759. TargetFileName = UnixExtractFileName(Name);
  2760. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  2761. LocalFileName = TempDir +
  2762. // We probably do not want to trim the VMS version here
  2763. FTerminal->ChangeFileName(&CopyParam, TargetFileName, osRemote, false);
  2764. }
  2765. }
  2766. else
  2767. {
  2768. if (ExtractFilePath(Name).IsEmpty())
  2769. {
  2770. LocalFileName = IncludeTrailingBackslash(DView->PathName) + Name;
  2771. }
  2772. else
  2773. {
  2774. LocalFileName = ExpandFileName(Name);
  2775. }
  2776. TargetFileName = ExtractFileName(Name);
  2777. }
  2778. if (!ExistingFile)
  2779. {
  2780. if (!FileExists(ApiPath(LocalFileName)))
  2781. {
  2782. int File = FileCreate(ApiPath(LocalFileName));
  2783. if (File < 0)
  2784. {
  2785. if (!RootTempDir.IsEmpty())
  2786. {
  2787. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  2788. }
  2789. throw Exception(FMTLOAD(CREATE_FILE_ERROR, (LocalFileName)));
  2790. }
  2791. else
  2792. {
  2793. FileClose(File);
  2794. }
  2795. }
  2796. TExecuteFileBy ExecuteFileBy = efDefaultEditor;
  2797. const TEditorData * ExternalEditor = NULL;
  2798. TFileMasks::TParams MaskParams; // size not known
  2799. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, TargetFileName,
  2800. false, MaskParams);
  2801. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, TargetFileName,
  2802. ExternalEditor, RootTempDir, RemoteDirectory);
  2803. }
  2804. }
  2805. }
  2806. }
  2807. //---------------------------------------------------------------------------
  2808. bool __fastcall TCustomScpExplorerForm::RemoteExecuteForceText(
  2809. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor)
  2810. {
  2811. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2812. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2813. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2814. return
  2815. ((ExecuteFileBy == efInternalEditor)) ||
  2816. ((ExecuteFileBy == efExternalEditor) && ExternalEditor->ExternalEditorText);
  2817. }
  2818. //---------------------------------------------------------------------------
  2819. void __fastcall TCustomScpExplorerForm::CustomExecuteFile(TOperationSide Side,
  2820. TExecuteFileBy ExecuteFileBy, UnicodeString FileName, UnicodeString OriginalFileName,
  2821. const TEditorData * ExternalEditor, UnicodeString LocalRootDirectory,
  2822. UnicodeString RemoteDirectory)
  2823. {
  2824. DebugAssert(!WinConfiguration->DisableOpenEdit);
  2825. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  2826. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  2827. DebugAssert(ExecuteFileBy != efDefaultEditor);
  2828. Side = GetSide(Side);
  2829. std::unique_ptr<TEditedFileData> Data(new TEditedFileData);
  2830. if (Side == osRemote)
  2831. {
  2832. Data->Terminal = Terminal;
  2833. Data->Queue = Queue;
  2834. Data->SessionData = CloneCurrentSessionData();
  2835. Data->ForceText = RemoteExecuteForceText(ExecuteFileBy, ExternalEditor);
  2836. Data->RemoteDirectory = RemoteDirectory;
  2837. Data->SessionName = Terminal->SessionData->SessionName;
  2838. Data->LocalRootDirectory = LocalRootDirectory;
  2839. Data->OriginalFileName = OriginalFileName;
  2840. Data->Command = L""; // will be changed later for external editor
  2841. }
  2842. if (ExecuteFileBy == efInternalEditor)
  2843. {
  2844. if (Side == osRemote)
  2845. {
  2846. UnicodeString Caption = UnixIncludeTrailingBackslash(RemoteDirectory) + OriginalFileName +
  2847. L" - " + Terminal->SessionData->SessionName;
  2848. TForm * Editor;
  2849. try
  2850. {
  2851. TNotifyEvent OnFileChanged = NULL;
  2852. TNotifyEvent OnSaveAll = NULL;
  2853. TAnyModifiedEvent OnAnyModified = NULL;
  2854. // Edited files are uploaded in background queue, what we do not support with encrypted files
  2855. if (Terminal->IsCapable[fsBackgroundTransfers])
  2856. {
  2857. OnFileChanged = FEditorManager->FileChanged;
  2858. OnSaveAll = SaveAllInternalEditors;
  2859. OnAnyModified = AnyInternalEditorModified;
  2860. }
  2861. Editor = ShowEditorForm(FileName, this, OnFileChanged,
  2862. FEditorManager->FileReload, FEditorManager->FileClosed,
  2863. OnSaveAll, OnAnyModified,
  2864. Caption, FStandaloneEditing, SessionColor, Terminal->SessionData->InternalEditorEncoding);
  2865. }
  2866. catch(...)
  2867. {
  2868. if (!LocalRootDirectory.IsEmpty())
  2869. {
  2870. RecursiveDeleteFile(ExcludeTrailingBackslash(LocalRootDirectory), false);
  2871. }
  2872. throw;
  2873. }
  2874. FEditorManager->AddFileInternal(FileName, Data.release(), Editor);
  2875. }
  2876. else
  2877. {
  2878. DebugAssert(!FStandaloneEditing);
  2879. TForm * Editor =
  2880. ShowEditorForm(FileName, this, NULL, NULL, LocalEditorClosed,
  2881. SaveAllInternalEditors, AnyInternalEditorModified,
  2882. L"", false, SessionColor, -1);
  2883. FLocalEditors->Add(Editor);
  2884. }
  2885. }
  2886. else
  2887. {
  2888. HANDLE Process;
  2889. if (ExecuteFileBy == efExternalEditor)
  2890. {
  2891. UnicodeString Program, Params, Dir;
  2892. Data->Command = ExternalEditor->ExternalEditor;
  2893. ReformatFileNameCommand(Data->Command);
  2894. SplitCommand(Data->Command, Program, Params, Dir);
  2895. Params = ExpandFileNameCommand(Params, FileName);
  2896. Program = ExpandEnvironmentVariables(Program);
  2897. if (!ExecuteShell(Program, Params, Process))
  2898. {
  2899. throw EOSExtException(FMTLOAD(EDITOR_ERROR, (Program)));
  2900. }
  2901. }
  2902. else
  2903. {
  2904. DebugAssert(Side == osRemote);
  2905. if (!ExecuteShell(FileName, L"", Process))
  2906. {
  2907. throw EOSExtException(FMTLOAD(EXECUTE_FILE_ERROR, (FileName)));
  2908. }
  2909. }
  2910. if ((Side == osLocal) ||
  2911. ((ExecuteFileBy == efShell) &&
  2912. !WinConfiguration->Editor.SDIShellEditor) ||
  2913. ((ExecuteFileBy == efExternalEditor) &&
  2914. !ExternalEditor->SDIExternalEditor))
  2915. {
  2916. // no need for handle
  2917. if (Process != NULL)
  2918. {
  2919. DebugCheck(CloseHandle(Process));
  2920. }
  2921. Process = INVALID_HANDLE_VALUE;
  2922. }
  2923. else
  2924. {
  2925. if (Process == NULL)
  2926. {
  2927. throw ExtException(LoadStr(OPEN_FILE_NO_PROCESS2), L"", HELP_OPEN_FILE_NO_PROCESS);
  2928. }
  2929. }
  2930. if (Side == osRemote)
  2931. {
  2932. FEditorManager->AddFileExternal(FileName, Data.release(), Process);
  2933. }
  2934. }
  2935. }
  2936. //---------------------------------------------------------------------------
  2937. void __fastcall TCustomScpExplorerForm::SaveInternalEditor(
  2938. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2939. void * /*Arg*/)
  2940. {
  2941. if (Token != NULL)
  2942. {
  2943. EditorFormFileSave(static_cast<TForm *>(Token));
  2944. }
  2945. }
  2946. //---------------------------------------------------------------------------
  2947. void __fastcall TCustomScpExplorerForm::SaveAllInternalEditors(TObject * /*Sender*/)
  2948. {
  2949. for (int Index = 0; Index < FLocalEditors->Count; Index++)
  2950. {
  2951. EditorFormFileSave(static_cast<TForm *>(FLocalEditors->Items[Index]));
  2952. }
  2953. FEditorManager->ProcessFiles(SaveInternalEditor, NULL);
  2954. }
  2955. //---------------------------------------------------------------------------
  2956. void __fastcall TCustomScpExplorerForm::InternalEditorModified(
  2957. const UnicodeString /*FileName*/, TEditedFileData * /*Data*/, TObject * Token,
  2958. void * Arg)
  2959. {
  2960. if ((Token != NULL) &&
  2961. IsEditorFormModified(static_cast<TForm *>(Token)))
  2962. {
  2963. *static_cast<bool *>(Arg) = true;
  2964. }
  2965. }
  2966. //---------------------------------------------------------------------------
  2967. void __fastcall TCustomScpExplorerForm::AnyInternalEditorModified(
  2968. TObject * /*Sender*/, bool & Modified)
  2969. {
  2970. for (int Index = 0; !Modified && (Index < FLocalEditors->Count); Index++)
  2971. {
  2972. if (IsEditorFormModified(static_cast<TForm *>(FLocalEditors->Items[Index])))
  2973. {
  2974. Modified = true;
  2975. }
  2976. }
  2977. if (!Modified)
  2978. {
  2979. FEditorManager->ProcessFiles(InternalEditorModified, &Modified);
  2980. }
  2981. }
  2982. //---------------------------------------------------------------------------
  2983. void __fastcall TCustomScpExplorerForm::LocalEditorClosed(TObject * Sender, bool /*Forced*/)
  2984. {
  2985. DebugCheck(FLocalEditors->Extract(Sender) >= 0);
  2986. }
  2987. //---------------------------------------------------------------------------
  2988. void __fastcall TCustomScpExplorerForm::TemporaryDirectoryForRemoteFiles(
  2989. UnicodeString RemoteDirectory, TCopyParamType CopyParam,
  2990. UnicodeString & Result, UnicodeString & RootDirectory)
  2991. {
  2992. if (!WinConfiguration->TemporaryDirectoryDeterministic)
  2993. {
  2994. RootDirectory = IncludeTrailingBackslash(WinConfiguration->TemporaryDir());
  2995. Result = RootDirectory;
  2996. }
  2997. else
  2998. {
  2999. RootDirectory = L"";
  3000. Result = WinConfiguration->ExpandedTemporaryDirectory();
  3001. Result = IncludeTrailingBackslash(Result);
  3002. }
  3003. if (WinConfiguration->TemporaryDirectoryAppendSession)
  3004. {
  3005. Result = IncludeTrailingBackslash(Result + MakeValidFileName(Terminal->SessionData->SessionName));
  3006. }
  3007. if (WinConfiguration->TemporaryDirectoryAppendPath)
  3008. {
  3009. if (!RemoteDirectory.IsEmpty() && (RemoteDirectory[1] == L'/'))
  3010. {
  3011. RemoteDirectory.Delete(1, 1);
  3012. }
  3013. Result = IncludeTrailingBackslash(Result + CopyParam.ValidLocalPath(FromUnixPath(RemoteDirectory)));
  3014. }
  3015. if (!ForceDirectories(ApiPath(Result)))
  3016. {
  3017. throw EOSExtException(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  3018. }
  3019. }
  3020. //---------------------------------------------------------------------------
  3021. void __fastcall TCustomScpExplorerForm::TemporarilyDownloadFiles(
  3022. TStrings * FileList, bool ForceText, UnicodeString & RootTempDir, UnicodeString & TempDir,
  3023. bool AllFiles, bool GetTargetNames, bool AutoOperation)
  3024. {
  3025. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3026. if (ForceText)
  3027. {
  3028. CopyParam.TransferMode = tmAscii;
  3029. }
  3030. TemporaryFileCopyParam(CopyParam);
  3031. if (AllFiles)
  3032. {
  3033. CopyParam.IncludeFileMask = TFileMasks();
  3034. }
  3035. if (TempDir.IsEmpty())
  3036. {
  3037. TemporaryDirectoryForRemoteFiles(FTerminal->CurrentDirectory, CopyParam, TempDir, RootTempDir);
  3038. }
  3039. DebugAssert(!FAutoOperation);
  3040. FAutoOperation = AutoOperation;
  3041. Terminal->ExceptionOnFail = true;
  3042. try
  3043. {
  3044. try
  3045. {
  3046. // turn off confirmations, as for MDI editors we may possibly download
  3047. // the same file over
  3048. Terminal->CopyToLocal(
  3049. FileList, TempDir, &CopyParam, cpNoConfirmation | cpTemporary, NULL);
  3050. if (GetTargetNames)
  3051. {
  3052. for (int i = 0; i < FileList->Count; i++)
  3053. {
  3054. FileList->Strings[i] =
  3055. Terminal->ChangeFileName(&CopyParam, UnixExtractFileName(FileList->Strings[i]), osRemote, false);
  3056. }
  3057. }
  3058. }
  3059. catch(...)
  3060. {
  3061. if (!RootTempDir.IsEmpty())
  3062. {
  3063. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3064. }
  3065. throw;
  3066. }
  3067. }
  3068. __finally
  3069. {
  3070. FAutoOperation = false;
  3071. Terminal->ExceptionOnFail = false;
  3072. }
  3073. }
  3074. //---------------------------------------------------------------------------
  3075. void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
  3076. {
  3077. // Do not waste time checking for default editor list the next time,
  3078. // or testing if default editor is not notepad.
  3079. bool TryNextTime = false;
  3080. UnicodeString UsageState;
  3081. if (!WinConfiguration->EditorList->IsDefaultList())
  3082. {
  3083. UsageState = "H";
  3084. }
  3085. else
  3086. {
  3087. UnicodeString Executable;
  3088. UnicodeString ExecutableDescription;
  3089. if (DetectSystemExternalEditor(false, Executable, ExecutableDescription, UsageState, TryNextTime))
  3090. {
  3091. UnicodeString Message =
  3092. FMTLOAD(EDITOR_AUTO_CONFIG2, (ExecutableDescription, ExecutableDescription));
  3093. unsigned int Answer =
  3094. MessageDialog(Message, qtConfirmation, qaOK | qaCancel, HELP_EDITOR_AUTO_CONFIG);
  3095. if (Answer != qaOK)
  3096. {
  3097. UsageState = "R";
  3098. }
  3099. else
  3100. {
  3101. UsageState = "A";
  3102. TEditorData EditorData;
  3103. EditorData.Editor = edExternal;
  3104. EditorData.ExternalEditor = FormatCommand(Executable, L"");
  3105. EditorData.DecideExternalEditorText();
  3106. TEditorList EditorList;
  3107. EditorList = *WinConfiguration->EditorList;
  3108. EditorList.Insert(0, new TEditorPreferences(EditorData));
  3109. WinConfiguration->EditorList = &EditorList;
  3110. }
  3111. }
  3112. }
  3113. WinConfiguration->OfferedEditorAutoConfig = !TryNextTime;
  3114. WinConfiguration->Usage->Set(L"EditorAutoConfig", UsageState);
  3115. }
  3116. //---------------------------------------------------------------------------
  3117. void __fastcall TCustomScpExplorerForm::ExecuteFileNormalize(
  3118. TExecuteFileBy & ExecuteFileBy, const TEditorData *& ExternalEditor,
  3119. const UnicodeString & FileName, bool Local, const TFileMasks::TParams & MaskParams)
  3120. {
  3121. if (ExecuteFileBy == efDefaultEditor)
  3122. {
  3123. if (!WinConfiguration->OfferedEditorAutoConfig)
  3124. {
  3125. EditorAutoConfig();
  3126. }
  3127. const TEditorPreferences * Editor =
  3128. WinConfiguration->DefaultEditorForFile(FileName, Local, MaskParams);
  3129. if ((Editor == NULL) || (Editor->Data->Editor == edInternal))
  3130. {
  3131. ExecuteFileBy = efInternalEditor;
  3132. ExternalEditor = NULL;
  3133. }
  3134. else if (Editor->Data->Editor == edOpen)
  3135. {
  3136. ExecuteFileBy = efShell;
  3137. ExternalEditor = NULL;
  3138. }
  3139. else
  3140. {
  3141. ExecuteFileBy = efExternalEditor;
  3142. ExternalEditor = Editor->Data;
  3143. }
  3144. }
  3145. }
  3146. //---------------------------------------------------------------------------
  3147. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3148. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3149. UnicodeString FullFileName, TObject * Object, const TFileMasks::TParams & MaskParams)
  3150. {
  3151. UnicodeString OriginalFileName;
  3152. UnicodeString LocalRootDirectory;
  3153. UnicodeString RemoteDirectory;
  3154. ExecuteFileNormalize(ExecuteFileBy, ExternalEditor, FullFileName,
  3155. (Side == osLocal), MaskParams);
  3156. UnicodeString Counter;
  3157. UnicodeString LocalFileName;
  3158. bool Handled = false;
  3159. if (Side == osRemote)
  3160. {
  3161. // We need to trim VMS version here, so that we use name without version
  3162. // when uploading back to create a new version of the file
  3163. OriginalFileName = FTerminal->GetBaseFileName(UnixExtractFileName(FullFileName));
  3164. RemoteDirectory = UnixExtractFilePath(FullFileName);
  3165. TObject * Token = NULL;
  3166. UnicodeString LocalDirectory;
  3167. if (!FEditorManager->CanAddFile(RemoteDirectory, OriginalFileName,
  3168. Terminal->SessionData->SessionName, Token, LocalRootDirectory,
  3169. LocalDirectory))
  3170. {
  3171. if (Token != NULL)
  3172. {
  3173. TForm * Form = dynamic_cast<TForm *>(Token);
  3174. if (Form->WindowState == wsMinimized)
  3175. {
  3176. ShowWindow(Form->Handle, SW_RESTORE);
  3177. }
  3178. else
  3179. {
  3180. Form->SetFocus();
  3181. }
  3182. Handled = true;
  3183. }
  3184. else
  3185. {
  3186. throw Exception(FMTLOAD(ALREADY_EDITED_EXTERNALLY_OR_UPLOADED, (OriginalFileName)));
  3187. }
  3188. }
  3189. if (!Handled)
  3190. {
  3191. TStringList * FileList1 = new TStringList();
  3192. try
  3193. {
  3194. FileList1->AddObject(FullFileName, Object);
  3195. TemporarilyDownloadFiles(FileList1,
  3196. RemoteExecuteForceText(ExecuteFileBy, ExternalEditor),
  3197. LocalRootDirectory, LocalDirectory, true, true, true);
  3198. LocalFileName = LocalDirectory + FileList1->Strings[0];
  3199. }
  3200. __finally
  3201. {
  3202. delete FileList1;
  3203. }
  3204. switch (ExecuteFileBy)
  3205. {
  3206. case efShell:
  3207. Counter = "RemoteFilesExecuted";
  3208. break;
  3209. case efInternalEditor:
  3210. Counter = "RemoteFilesOpenedInInternalEditor";
  3211. break;
  3212. case efExternalEditor:
  3213. Counter = "RemoteFilesOpenedInExternalEditor";
  3214. break;
  3215. default:
  3216. DebugFail();
  3217. }
  3218. }
  3219. }
  3220. else
  3221. {
  3222. LocalFileName = FullFileName;
  3223. OriginalFileName = ExtractFileName(FullFileName);
  3224. switch (ExecuteFileBy)
  3225. {
  3226. case efShell:
  3227. Counter = "LocalFilesExecuted";
  3228. break;
  3229. case efInternalEditor:
  3230. Counter = "LocalFilesOpenedInInternalEditor";
  3231. break;
  3232. case efExternalEditor:
  3233. Counter = "LocalFilesOpenedInExternalEditor";
  3234. break;
  3235. default:
  3236. DebugFail();
  3237. }
  3238. }
  3239. if (!Handled)
  3240. {
  3241. Configuration->Usage->Inc(Counter);
  3242. CustomExecuteFile(Side, ExecuteFileBy, LocalFileName, OriginalFileName,
  3243. ExternalEditor, LocalRootDirectory, RemoteDirectory);
  3244. }
  3245. }
  3246. //---------------------------------------------------------------------------
  3247. void __fastcall TCustomScpExplorerForm::ExecuteFile(TOperationSide Side,
  3248. TExecuteFileBy ExecuteFileBy, const TEditorData * ExternalEditor,
  3249. bool AllSelected, bool OnFocused)
  3250. {
  3251. DebugAssert(!WinConfiguration->DisableOpenEdit);
  3252. DebugAssert((ExecuteFileBy == efExternalEditor) ==
  3253. ((ExternalEditor != NULL) && (ExternalEditor->Editor == edExternal)));
  3254. Side = GetSide(Side);
  3255. TCustomDirView * DView = DirView(Side);
  3256. TStrings * FileList = AllSelected ?
  3257. DView->CreateFileList(OnFocused, Side == osLocal) :
  3258. DView->CreateFocusedFileList(Side == osLocal);
  3259. try
  3260. {
  3261. DebugAssert(AllSelected || (FileList->Count == 1));
  3262. for (int i = 0; i < FileList->Count; i++)
  3263. {
  3264. UnicodeString ListFileName = FileList->Strings[i];
  3265. UnicodeString FileNameOnly = (Side == osRemote) ?
  3266. UnixExtractFileName(ListFileName) : ExtractFileName(ListFileName);
  3267. TListItem * Item = DView->FindFileItem(FileNameOnly);
  3268. if (!DView->ItemIsDirectory(Item))
  3269. {
  3270. UnicodeString FullFileName;
  3271. if (Side == osRemote)
  3272. {
  3273. FullFileName = RemoteDirView->Path + ListFileName;
  3274. }
  3275. else
  3276. {
  3277. FullFileName = ListFileName;
  3278. }
  3279. TFileMasks::TParams MaskParams;
  3280. MaskParams.Size = DView->ItemFileSize(Item);
  3281. TDateTimePrecision Precision;
  3282. MaskParams.Modification = DView->ItemFileTime(Item, Precision);
  3283. ExecuteFile(Side, ExecuteFileBy, ExternalEditor, FullFileName,
  3284. FileList->Objects[i], MaskParams);
  3285. }
  3286. }
  3287. }
  3288. __finally
  3289. {
  3290. delete FileList;
  3291. }
  3292. }
  3293. //---------------------------------------------------------------------------
  3294. void __fastcall TCustomScpExplorerForm::TemporaryFileCopyParam(TCopyParamType & CopyParam)
  3295. {
  3296. // do not forget to add additional options to TemporarilyDownloadFiles, and AS
  3297. CopyParam.FileNameCase = ncNoChange;
  3298. CopyParam.PreserveRights = false;
  3299. CopyParam.PreserveReadOnly = false;
  3300. CopyParam.ReplaceInvalidChars = true;
  3301. CopyParam.IncludeFileMask = TFileMasks();
  3302. CopyParam.NewerOnly = false;
  3303. CopyParam.FileMask = L"";
  3304. }
  3305. //---------------------------------------------------------------------------
  3306. void __fastcall TCustomScpExplorerForm::ExecutedFileChanged(const UnicodeString FileName,
  3307. TEditedFileData * Data, HANDLE UploadCompleteEvent)
  3308. {
  3309. TTerminalManager * Manager = TTerminalManager::Instance();
  3310. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3311. {
  3312. if (!NonVisualDataModule->Busy)
  3313. {
  3314. UnicodeString FileNameOnly = ExtractFileName(FileName);
  3315. UnicodeString EditInactiveSessionReopenAcceptedCounter = L"EditInactiveSessionReopenAccepted";
  3316. UnicodeString EditInactiveSessionReopenRejectedCounter = L"EditInactiveSessionReopenRejected";
  3317. if (Data->Terminal == NULL)
  3318. {
  3319. TTerminal * SameSiteTerminal = Manager->FindActiveTerminalForSite(Data->SessionData);
  3320. if (SameSiteTerminal != NULL)
  3321. {
  3322. UnicodeString Message =
  3323. FMTLOAD(EDIT_SESSION_REATTACH,
  3324. (FileNameOnly, Data->SessionName, FileNameOnly));
  3325. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3326. {
  3327. Data->Terminal = Terminal;
  3328. Data->Queue = Manager->FindQueueForTerminal(Terminal);
  3329. Data->SessionName = Terminal->SessionData->SessionName;
  3330. // We might also overwrite session data
  3331. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3332. }
  3333. else
  3334. {
  3335. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3336. Abort();
  3337. }
  3338. }
  3339. }
  3340. // foreground session should reconnect itself
  3341. else if (Terminal != Data->Terminal)
  3342. {
  3343. UnicodeString Message =
  3344. MainInstructions(
  3345. FMTLOAD(EDIT_SESSION_RECONNECT, (Data->SessionName, FileNameOnly)));
  3346. if (MessageDialog(Message, qtConfirmation, qaOK | qaCancel) == qaOK)
  3347. {
  3348. Manager->SetActiveTerminalWithAutoReconnect(Data->Terminal);
  3349. Configuration->Usage->Inc(EditInactiveSessionReopenAcceptedCounter);
  3350. }
  3351. else
  3352. {
  3353. Configuration->Usage->Inc(EditInactiveSessionReopenRejectedCounter);
  3354. Abort();
  3355. }
  3356. }
  3357. }
  3358. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3359. {
  3360. Configuration->Usage->Inc(L"EditInactiveSession");
  3361. // Prevent this when not idle (!NonVisualDataModule->Busy)?
  3362. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED2,
  3363. (ExtractFileName(FileName), Data->SessionName)));
  3364. }
  3365. }
  3366. TStrings * FileList = new TStringList();
  3367. try
  3368. {
  3369. FileList->Add(FileName);
  3370. // consider using the same settings (preset) as when the file was downloaded
  3371. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3372. TemporaryFileCopyParam(CopyParam);
  3373. if (Data->ForceText)
  3374. {
  3375. CopyParam.TransferMode = tmAscii;
  3376. }
  3377. // so i do not need to worry if masking algorithm works in all cases
  3378. // ("" means "copy file name", no masking is actually done)
  3379. if (ExtractFileName(FileName) == Data->OriginalFileName)
  3380. {
  3381. CopyParam.FileMask = L"";
  3382. }
  3383. else
  3384. {
  3385. CopyParam.FileMask = DelimitFileNameMask(Data->OriginalFileName);
  3386. }
  3387. DebugAssert(Data->Queue != NULL);
  3388. int Params = cpNoConfirmation | cpTemporary;
  3389. TQueueItem * QueueItem = new TUploadQueueItem(Data->Terminal, FileList,
  3390. Data->RemoteDirectory, &CopyParam, Params, true, false);
  3391. QueueItem->CompleteEvent = UploadCompleteEvent;
  3392. AddQueueItem(Data->Queue, QueueItem, Data->Terminal);
  3393. }
  3394. __finally
  3395. {
  3396. delete FileList;
  3397. }
  3398. }
  3399. //---------------------------------------------------------------------------
  3400. void __fastcall TCustomScpExplorerForm::ExecutedFileReload(
  3401. const UnicodeString FileName, const TEditedFileData * Data)
  3402. {
  3403. // Sanity check, we should not be busy otherwise user would not be able to click Reload button.
  3404. DebugAssert(!NonVisualDataModule->Busy);
  3405. if ((Data->Terminal == NULL) || !Data->Terminal->Active)
  3406. {
  3407. throw Exception(FMTLOAD(EDIT_SESSION_CLOSED_RELOAD,
  3408. (ExtractFileName(FileName), Data->SessionName)));
  3409. }
  3410. TTerminal * PrevTerminal = TTerminalManager::Instance()->ActiveTerminal;
  3411. TTerminalManager::Instance()->ActiveTerminal = Data->Terminal;
  3412. NonVisualDataModule->StartBusy();
  3413. try
  3414. {
  3415. std::unique_ptr<TRemoteFile> File;
  3416. UnicodeString RemoteFileName =
  3417. UnixIncludeTrailingBackslash(Data->RemoteDirectory) + Data->OriginalFileName;
  3418. FTerminal->ExceptionOnFail = true;
  3419. try
  3420. {
  3421. TRemoteFile * AFile = NULL;
  3422. FTerminal->ReadFile(RemoteFileName, AFile);
  3423. File.reset(AFile);
  3424. if (!File->HaveFullFileName)
  3425. {
  3426. File->FullFileName = RemoteFileName;
  3427. }
  3428. }
  3429. __finally
  3430. {
  3431. FTerminal->ExceptionOnFail = false;
  3432. }
  3433. std::unique_ptr<TStrings> FileList(new TStringList());
  3434. FileList->AddObject(RemoteFileName, File.get());
  3435. UnicodeString RootTempDir = Data->LocalRootDirectory;
  3436. UnicodeString TempDir = ExtractFilePath(FileName);
  3437. TemporarilyDownloadFiles(FileList.get(), Data->ForceText, RootTempDir,
  3438. TempDir, true, true, true);
  3439. // sanity check, the target file name should be still the same
  3440. DebugAssert(ExtractFileName(FileName) == FileList->Strings[0]);
  3441. }
  3442. __finally
  3443. {
  3444. NonVisualDataModule->EndBusy();
  3445. // it actually may not exist anymore...
  3446. TTerminalManager::Instance()->ActiveTerminal = PrevTerminal;
  3447. }
  3448. }
  3449. //---------------------------------------------------------------------------
  3450. void __fastcall TCustomScpExplorerForm::ExecutedFileEarlyClosed(
  3451. const TEditedFileData * Data, bool & KeepOpen)
  3452. {
  3453. // Command is set for external editors only (not for "shell" open).
  3454. if (!Data->Command.IsEmpty())
  3455. {
  3456. bool AnyFound = false;
  3457. bool AnyMDI = false;
  3458. bool AnyNonMDI = false;
  3459. bool AnyDetect = false;
  3460. TEditorList * EditorList = new TEditorList();
  3461. try
  3462. {
  3463. *EditorList = *WinConfiguration->EditorList;
  3464. for (int i = 0; i < EditorList->Count; i++)
  3465. {
  3466. const TEditorPreferences * Editor = EditorList->Editors[i];
  3467. if ((Editor->Data->Editor == edExternal) &&
  3468. (Editor->Data->ExternalEditor == Data->Command))
  3469. {
  3470. AnyFound = true;
  3471. if (Editor->Data->SDIExternalEditor)
  3472. {
  3473. AnyNonMDI = true;
  3474. if (Editor->Data->DetectMDIExternalEditor)
  3475. {
  3476. AnyDetect = true;
  3477. }
  3478. }
  3479. else
  3480. {
  3481. AnyMDI = true;
  3482. }
  3483. }
  3484. }
  3485. bool EnableMDI = false;
  3486. bool DisableDetect = false;
  3487. if (AnyMDI)
  3488. {
  3489. KeepOpen = true;
  3490. if (AnyNonMDI)
  3491. {
  3492. // there is at least one instance of the editor with MDI support enabled,
  3493. // and one with disabled, enable it for all instances
  3494. EnableMDI = true;
  3495. }
  3496. }
  3497. else if (AnyFound && !AnyDetect)
  3498. {
  3499. // at least once instance found but all have MDI autodetection disabled
  3500. // => close the file (default action)
  3501. }
  3502. else
  3503. {
  3504. // no instance of the editor has MDI support enabled
  3505. TMessageParams Params;
  3506. if (AnyFound)
  3507. {
  3508. // there is at least one instance of the editor
  3509. Params.Params |= mpNeverAskAgainCheck;
  3510. }
  3511. unsigned int Answer = MessageDialog(FMTLOAD(EDITOR_EARLY_CLOSED2, (Data->OriginalFileName)), qtWarning,
  3512. qaYes | qaNo, HELP_EDITOR_EARLY_CLOSED, &Params);
  3513. switch (Answer)
  3514. {
  3515. case qaNeverAskAgain:
  3516. DisableDetect = true;
  3517. break;
  3518. case qaNo:
  3519. EnableMDI = true;
  3520. KeepOpen = true;
  3521. break;
  3522. }
  3523. }
  3524. if (AnyFound && (EnableMDI || DisableDetect))
  3525. {
  3526. bool Changed = false;
  3527. for (int i = 0; i < EditorList->Count; i++)
  3528. {
  3529. const TEditorPreferences * Editor = EditorList->Editors[i];
  3530. if ((Editor->Data->Editor == edExternal) &&
  3531. (Editor->Data->ExternalEditor == Data->Command) &&
  3532. ((EnableMDI && Editor->Data->SDIExternalEditor) ||
  3533. (DisableDetect && Editor->Data->DetectMDIExternalEditor)))
  3534. {
  3535. Changed = true;
  3536. TEditorPreferences * UpdatedEditor = new TEditorPreferences(*Editor);
  3537. if (EnableMDI)
  3538. {
  3539. UpdatedEditor->GetData()->SDIExternalEditor = false;
  3540. }
  3541. if (DisableDetect)
  3542. {
  3543. UpdatedEditor->GetData()->DetectMDIExternalEditor = false;
  3544. }
  3545. EditorList->Change(i, UpdatedEditor);
  3546. }
  3547. }
  3548. if (Changed)
  3549. {
  3550. WinConfiguration->EditorList = EditorList;
  3551. }
  3552. }
  3553. }
  3554. __finally
  3555. {
  3556. delete EditorList;
  3557. }
  3558. }
  3559. else
  3560. {
  3561. // "open" case
  3562. MessageDialog(FMTLOAD(APP_EARLY_CLOSED, (Data->OriginalFileName)), qtWarning,
  3563. qaOK, HELP_APP_EARLY_CLOSED);
  3564. }
  3565. }
  3566. //---------------------------------------------------------------------------
  3567. void __fastcall TCustomScpExplorerForm::ExecutedFileUploadComplete(TObject * Sender)
  3568. {
  3569. EditorFormFileUploadComplete(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  3570. }
  3571. //---------------------------------------------------------------------------
  3572. void __fastcall TCustomScpExplorerForm::RemoteDirViewEnter(TObject * /*Sender*/)
  3573. {
  3574. SideEnter(osRemote);
  3575. }
  3576. //---------------------------------------------------------------------------
  3577. void __fastcall TCustomScpExplorerForm::RemoteDriveViewEnter(TObject * /*Sender*/)
  3578. {
  3579. MakeNextInTabOrder(RemoteDirView, RemoteDriveView);
  3580. SideEnter(osRemote);
  3581. }
  3582. //---------------------------------------------------------------------------
  3583. void __fastcall TCustomScpExplorerForm::SideEnter(TOperationSide Side)
  3584. {
  3585. FCurrentSide = Side;
  3586. if (Visible)
  3587. {
  3588. UpdateControls();
  3589. }
  3590. }
  3591. //---------------------------------------------------------------------------
  3592. void __fastcall TCustomScpExplorerForm::DeleteFiles(TOperationSide Side,
  3593. TStrings * FileList, bool Alternative)
  3594. {
  3595. TCustomDirView * DView = DirView(Side);
  3596. DView->SaveSelection();
  3597. DView->SaveSelectedNames();
  3598. DebugAssert(!FAlternativeDelete);
  3599. FAlternativeDelete = Alternative;
  3600. try
  3601. {
  3602. if (Side == osRemote)
  3603. {
  3604. DebugAssert(Terminal != NULL);
  3605. Terminal->DeleteFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3606. }
  3607. else
  3608. {
  3609. try
  3610. {
  3611. TTerminalManager::Instance()->LocalTerminal->DeleteLocalFiles(FileList, FLAGMASK(Alternative, dfAlternative));
  3612. }
  3613. __finally
  3614. {
  3615. ReloadLocalDirectory();
  3616. }
  3617. }
  3618. FAlternativeDelete = false;
  3619. }
  3620. catch(...)
  3621. {
  3622. FAlternativeDelete = false;
  3623. DView->DiscardSavedSelection();
  3624. throw;
  3625. }
  3626. DView->RestoreSelection();
  3627. }
  3628. //---------------------------------------------------------------------------
  3629. void __fastcall TCustomScpExplorerForm::LockFiles(TStrings * FileList, bool Lock)
  3630. {
  3631. DebugAssert(Terminal);
  3632. RemoteDirView->SaveSelection();
  3633. RemoteDirView->SaveSelectedNames();
  3634. try
  3635. {
  3636. if (Lock)
  3637. {
  3638. Terminal->LockFiles(FileList);
  3639. }
  3640. else
  3641. {
  3642. Terminal->UnlockFiles(FileList);
  3643. }
  3644. }
  3645. catch(...)
  3646. {
  3647. RemoteDirView->DiscardSavedSelection();
  3648. throw;
  3649. }
  3650. RemoteDirView->RestoreSelection();
  3651. }
  3652. //---------------------------------------------------------------------------
  3653. bool __fastcall TCustomScpExplorerForm::RemoteTransferDialog(TTerminal *& Session,
  3654. TStrings * FileList, UnicodeString & Target, UnicodeString & FileMask, bool & DirectCopy,
  3655. bool NoConfirmation, bool Move)
  3656. {
  3657. DebugAssert(Terminal != NULL);
  3658. // update Terminal->StateData->RemoteDirectory
  3659. UpdateTerminal(Terminal);
  3660. if (Session == NULL)
  3661. {
  3662. Session = TTerminalManager::Instance()->ActiveTerminal;
  3663. }
  3664. if (Session == TTerminalManager::Instance()->ActiveTerminal)
  3665. {
  3666. if (RemoteDriveView->DropTarget != NULL)
  3667. {
  3668. Target = RemoteDriveView->NodePathName(RemoteDriveView->DropTarget);
  3669. }
  3670. else if (RemoteDirView->DropTarget != NULL)
  3671. {
  3672. DebugAssert(RemoteDirView->ItemIsDirectory(RemoteDirView->DropTarget));
  3673. Target = RemoteDirView->ItemFullFileName(RemoteDirView->DropTarget);
  3674. }
  3675. else
  3676. {
  3677. Target = RemoteDirView->Path;
  3678. }
  3679. }
  3680. else
  3681. {
  3682. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(Session));
  3683. Target = ManagedTerminal->StateData->RemoteDirectory;
  3684. }
  3685. Target = UnixIncludeTrailingBackslash(Target);
  3686. if (FileList->Count == 1)
  3687. {
  3688. FileMask = DelimitFileNameMask(UnixExtractFileName(FileList->Strings[0]));
  3689. }
  3690. else
  3691. {
  3692. FileMask = AnyMask;
  3693. }
  3694. DirectCopy = FTerminal->IsCapable[fcRemoteCopy] || FTerminal->IsCapable[fcSecondaryShell];
  3695. bool Result = true;
  3696. if (!NoConfirmation)
  3697. {
  3698. bool Multi = (FileList->Count > 1);
  3699. if (Move)
  3700. {
  3701. Result = DoRemoteMoveDialog(Multi, Target, FileMask);
  3702. }
  3703. else
  3704. {
  3705. TStrings * Sessions = TTerminalManager::Instance()->TerminalList;
  3706. TStrings * Directories = new TStringList;
  3707. try
  3708. {
  3709. for (int Index = 0; Index < Sessions->Count; Index++)
  3710. {
  3711. TManagedTerminal * Terminal =
  3712. dynamic_cast<TManagedTerminal *>(Sessions->Objects[Index]);
  3713. Directories->Add(Terminal->StateData->RemoteDirectory);
  3714. }
  3715. TDirectRemoteCopy AllowDirectCopy;
  3716. if (FTerminal->IsCapable[fcRemoteCopy] || FTerminal->CommandSessionOpened)
  3717. {
  3718. DebugAssert(DirectCopy);
  3719. AllowDirectCopy = drcAllow;
  3720. }
  3721. else if (FTerminal->IsCapable[fcSecondaryShell])
  3722. {
  3723. DebugAssert(DirectCopy);
  3724. AllowDirectCopy = drcConfirmCommandSession;
  3725. }
  3726. else
  3727. {
  3728. DebugAssert(!DirectCopy);
  3729. AllowDirectCopy = drcDisallow;
  3730. }
  3731. void * ASession = Session;
  3732. Result = DoRemoteCopyDialog(Sessions, Directories, AllowDirectCopy,
  3733. Multi, ASession, Target, FileMask, DirectCopy, TTerminalManager::Instance()->ActiveTerminal);
  3734. Session = static_cast<TTerminal *>(ASession);
  3735. }
  3736. __finally
  3737. {
  3738. delete Directories;
  3739. }
  3740. }
  3741. }
  3742. return Result;
  3743. }
  3744. //---------------------------------------------------------------------------
  3745. bool __fastcall TCustomScpExplorerForm::RemoteTransferFiles(
  3746. TStrings * FileList, bool NoConfirmation, bool Move, TTerminal * Session)
  3747. {
  3748. bool DirectCopy;
  3749. UnicodeString Target, FileMask;
  3750. bool Result = RemoteTransferDialog(Session, FileList, Target, FileMask, DirectCopy, NoConfirmation, Move);
  3751. if (Result)
  3752. {
  3753. if (!Move && !DirectCopy)
  3754. {
  3755. Configuration->Usage->Inc("RemoteCopyTemp");
  3756. UnicodeString RootTempDir;
  3757. UnicodeString TempDir;
  3758. TemporarilyDownloadFiles(FileList, false, RootTempDir, TempDir, false, false, false);
  3759. TStrings * TemporaryFilesList = new TStringList();
  3760. try
  3761. {
  3762. TMakeLocalFileListParams MakeFileListParam;
  3763. MakeFileListParam.FileList = TemporaryFilesList;
  3764. MakeFileListParam.FileTimes = NULL;
  3765. MakeFileListParam.IncludeDirs = true;
  3766. MakeFileListParam.Recursive = false;
  3767. ProcessLocalDirectory(TempDir, FTerminal->MakeLocalFileList, &MakeFileListParam);
  3768. TTerminalManager::Instance()->ActiveTerminal = Session;
  3769. if (TemporaryFilesList->Count > 0)
  3770. {
  3771. TGUICopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  3772. CopyParam.FileMask = FileMask;
  3773. DebugAssert(!FAutoOperation);
  3774. FAutoOperation = true;
  3775. FTerminal->CopyToRemote(TemporaryFilesList, Target, &CopyParam, cpTemporary, NULL);
  3776. }
  3777. }
  3778. __finally
  3779. {
  3780. delete TemporaryFilesList;
  3781. FAutoOperation = false;
  3782. if (!RootTempDir.IsEmpty())
  3783. {
  3784. RecursiveDeleteFile(ExcludeTrailingBackslash(RootTempDir), false);
  3785. }
  3786. }
  3787. }
  3788. else
  3789. {
  3790. RemoteDirView->SaveSelection();
  3791. RemoteDirView->SaveSelectedNames();
  3792. try
  3793. {
  3794. if (Move)
  3795. {
  3796. Configuration->Usage->Inc("RemoteMove");
  3797. Terminal->MoveFiles(FileList, Target, FileMask);
  3798. }
  3799. else
  3800. {
  3801. Configuration->Usage->Inc("RemoteCopyDirect");
  3802. DebugAssert(DirectCopy);
  3803. DebugAssert(Session == FTerminal);
  3804. if (FTerminal->IsCapable[fcRemoteCopy] ||
  3805. FTerminal->CommandSessionOpened ||
  3806. CommandSessionFallback())
  3807. {
  3808. Terminal->CopyFiles(FileList, Target, FileMask);
  3809. }
  3810. }
  3811. }
  3812. catch(...)
  3813. {
  3814. RemoteDirView->DiscardSavedSelection();
  3815. throw;
  3816. }
  3817. RemoteDirView->RestoreSelection();
  3818. }
  3819. }
  3820. return Result;
  3821. }
  3822. //---------------------------------------------------------------------------
  3823. void __fastcall TCustomScpExplorerForm::CreateRemoteDirectory(
  3824. const UnicodeString & Path, TRemoteProperties & Properties)
  3825. {
  3826. Properties.Valid = Properties.Valid << vpEncrypt;
  3827. Properties.Encrypt = GUIConfiguration->CurrentCopyParam.EncryptNewFiles;
  3828. RemoteDirView->CreateDirectoryEx(Path, &Properties);
  3829. }
  3830. //---------------------------------------------------------------------------
  3831. void __fastcall TCustomScpExplorerForm::CreateDirectory(TOperationSide Side)
  3832. {
  3833. Side = GetSide(Side);
  3834. TRemoteProperties Properties = GUIConfiguration->NewDirectoryProperties;
  3835. TRemoteProperties * AProperties = (Side == osRemote ? &Properties : NULL);
  3836. UnicodeString Name = LoadStr(NEW_FOLDER);
  3837. int AllowedChanges =
  3838. FLAGMASK((Side == osRemote) && Terminal->IsCapable[fcModeChanging], cpMode);
  3839. bool SaveSettings = false;
  3840. if (DoCreateDirectoryDialog(Name, AProperties, AllowedChanges, SaveSettings))
  3841. {
  3842. TWindowLock Lock(this);
  3843. if (Side == osRemote)
  3844. {
  3845. if (SaveSettings)
  3846. {
  3847. GUIConfiguration->NewDirectoryProperties = Properties;
  3848. }
  3849. CreateRemoteDirectory(Name, Properties);
  3850. }
  3851. else
  3852. {
  3853. DirView(Side)->CreateDirectory(Name);
  3854. }
  3855. }
  3856. }
  3857. //---------------------------------------------------------------------------
  3858. void __fastcall TCustomScpExplorerForm::HomeDirectory(TOperationSide Side)
  3859. {
  3860. TWindowLock Lock(this);
  3861. DirView(Side)->ExecuteHomeDirectory();
  3862. }
  3863. //---------------------------------------------------------------------------
  3864. void __fastcall TCustomScpExplorerForm::OpenDirectory(TOperationSide Side)
  3865. {
  3866. DoOpenDirectoryDialog(odBrowse, Side);
  3867. }
  3868. //---------------------------------------------------------------------------
  3869. bool __fastcall TCustomScpExplorerForm::OpenBookmark(TOperationSide Side, TBookmark * Bookmark)
  3870. {
  3871. UnicodeString Path = Bookmark->GetSideDirectory(Side);
  3872. bool Result = !Path.IsEmpty();
  3873. if (Result)
  3874. {
  3875. // While we might get here when the session is closed (from location profiles),
  3876. // it's not a problem as the Path setter is noop then
  3877. DirView(Side)->Path = Path;
  3878. }
  3879. return Result;
  3880. }
  3881. //---------------------------------------------------------------------------
  3882. void __fastcall TCustomScpExplorerForm::CalculateSize(
  3883. TStrings * FileList, __int64 & Size, TCalculateSizeStats & Stats,
  3884. bool & Close)
  3885. {
  3886. // terminal can be already closed (e.g. dropped connection)
  3887. if (Terminal != NULL)
  3888. {
  3889. try
  3890. {
  3891. Terminal->CalculateFilesSize(FileList, Size, 0, NULL, true, Stats);
  3892. }
  3893. catch(...)
  3894. {
  3895. if (!Terminal->Active)
  3896. {
  3897. Close = true;
  3898. }
  3899. throw;
  3900. }
  3901. }
  3902. }
  3903. //---------------------------------------------------------------------------
  3904. void __fastcall TCustomScpExplorerForm::CalculateChecksum(const UnicodeString & Alg,
  3905. TStrings * FileList, TCalculatedChecksumEvent OnCalculatedChecksum,
  3906. bool & Close)
  3907. {
  3908. // terminal can be already closed (e.g. dropped connection)
  3909. if (Terminal != NULL)
  3910. {
  3911. Configuration->Usage->Inc(L"ChecksumCalculated");
  3912. try
  3913. {
  3914. Terminal->CalculateFilesChecksum(Alg, FileList, NULL, OnCalculatedChecksum);
  3915. }
  3916. catch(...)
  3917. {
  3918. if (!Terminal->Active)
  3919. {
  3920. Close = true;
  3921. }
  3922. throw;
  3923. }
  3924. }
  3925. }
  3926. //---------------------------------------------------------------------------
  3927. bool __fastcall TCustomScpExplorerForm::SetProperties(TOperationSide Side, TStrings * FileList)
  3928. {
  3929. bool Result;
  3930. if (Side == osRemote)
  3931. {
  3932. TRemoteTokenList * GroupList = NULL;
  3933. TRemoteTokenList * UserList = NULL;
  3934. try
  3935. {
  3936. TRemoteProperties CurrentProperties;
  3937. if (Terminal->LoadFilesProperties(FileList))
  3938. {
  3939. RemoteDirView->Invalidate();
  3940. }
  3941. bool CapableGroupChanging = Terminal->IsCapable[fcGroupChanging];
  3942. bool CapableOwnerChanging = Terminal->IsCapable[fcOwnerChanging];
  3943. if (CapableGroupChanging || CapableOwnerChanging)
  3944. {
  3945. if (CapableGroupChanging)
  3946. {
  3947. GroupList = Terminal->Groups->Duplicate();
  3948. }
  3949. if (CapableOwnerChanging)
  3950. {
  3951. UserList = Terminal->Users->Duplicate();
  3952. }
  3953. TRemoteDirectory * Files = Terminal->Files;
  3954. int Count = Files->Count;
  3955. if (Count > 100)
  3956. {
  3957. Count = 100;
  3958. }
  3959. for (int Index = 0; Index < Count; Index++)
  3960. {
  3961. TRemoteFile * File = Files->Files[Index];
  3962. if (CapableGroupChanging)
  3963. {
  3964. GroupList->AddUnique(File->Group);
  3965. }
  3966. if (CapableOwnerChanging)
  3967. {
  3968. UserList->AddUnique(File->Owner);
  3969. }
  3970. }
  3971. // if we haven't collected tokens for all files in current directory,
  3972. // make sure we collect them at least for all selected files.
  3973. // (note that so far the files in FileList has to be from current direcotry)
  3974. if (Count < Files->Count)
  3975. {
  3976. for (int Index = 0; Index < FileList->Count; Index++)
  3977. {
  3978. TRemoteFile * File = (TRemoteFile *)(FileList->Objects[Index]);
  3979. if (CapableGroupChanging)
  3980. {
  3981. GroupList->AddUnique(File->Group);
  3982. }
  3983. if (CapableOwnerChanging)
  3984. {
  3985. UserList->AddUnique(File->Owner);
  3986. }
  3987. }
  3988. }
  3989. }
  3990. CurrentProperties = TRemoteProperties::CommonProperties(FileList);
  3991. int Flags = 0;
  3992. if (Terminal->IsCapable[fcModeChanging]) Flags |= cpMode;
  3993. if (CapableOwnerChanging) Flags |= cpOwner;
  3994. if (CapableGroupChanging) Flags |= cpGroup;
  3995. TCalculateChecksumEvent CalculateChecksumEvent = NULL;
  3996. if (Terminal->IsCapable[fcCalculatingChecksum])
  3997. {
  3998. CalculateChecksumEvent = CalculateChecksum;
  3999. }
  4000. std::unique_ptr<TStrings> ChecksumAlgs(new TStringList());
  4001. Terminal->GetSupportedChecksumAlgs(ChecksumAlgs.get());
  4002. TRemoteProperties NewProperties = CurrentProperties;
  4003. Result =
  4004. DoPropertiesDialog(FileList, RemoteDirView->PathName,
  4005. GroupList, UserList, ChecksumAlgs.get(), &NewProperties, Flags,
  4006. Terminal->IsCapable[fcGroupOwnerChangingByID],
  4007. CalculateSize, CalculateChecksumEvent);
  4008. if (Result)
  4009. {
  4010. NewProperties = TRemoteProperties::ChangedProperties(CurrentProperties, NewProperties);
  4011. Terminal->ChangeFilesProperties(FileList, &NewProperties);
  4012. }
  4013. }
  4014. __finally
  4015. {
  4016. delete GroupList;
  4017. delete UserList;
  4018. }
  4019. }
  4020. else
  4021. {
  4022. DirView(Side)->DisplayPropertiesMenu();
  4023. Result = true;
  4024. }
  4025. return Result;
  4026. }
  4027. //---------------------------------------------------------------------------
  4028. void __fastcall TCustomScpExplorerForm::KeyProcessed(Word & Key, TShiftState Shift)
  4029. {
  4030. if (Shift * AllKeyShiftStates() == (TShiftState() << ssAlt))
  4031. {
  4032. FIgnoreNextDialogChar = Key;
  4033. }
  4034. Key = 0;
  4035. }
  4036. //---------------------------------------------------------------------------
  4037. void __fastcall TCustomScpExplorerForm::CheckCustomCommandShortCut(
  4038. TCustomCommandList * List, Word & Key, Classes::TShiftState Shift, TShortCut KeyShortCut)
  4039. {
  4040. const TCustomCommandType * Command = List->Find(KeyShortCut);
  4041. if (Command != NULL)
  4042. {
  4043. KeyProcessed(Key, Shift);
  4044. if (CustomCommandState(*Command, false, ccltAll) > 0)
  4045. {
  4046. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  4047. ExecuteFileOperationCommand(foCustomCommand, osRemote,
  4048. false, false, const_cast<TCustomCommandType *>(Command));
  4049. }
  4050. }
  4051. }
  4052. //---------------------------------------------------------------------------
  4053. void __fastcall TCustomScpExplorerForm::KeyDown(Word & Key, Classes::TShiftState Shift)
  4054. {
  4055. if (QueueView3->Focused() && (QueueView3->OnKeyDown != NULL))
  4056. {
  4057. QueueView3->OnKeyDown(QueueView3, Key, Shift);
  4058. }
  4059. if (!DirView(osCurrent)->IsEditing())
  4060. {
  4061. TShortCut KeyShortCut = ShortCut(Key, Shift);
  4062. for (int Index = 0; Index < NonVisualDataModule->ExplorerActions->ActionCount; Index++)
  4063. {
  4064. TAction * Action = (TAction *)NonVisualDataModule->ExplorerActions->Actions[Index];
  4065. if (((Action->ShortCut == KeyShortCut) ||
  4066. (Action->SecondaryShortCuts->IndexOfShortCut(KeyShortCut) >= 0)) &&
  4067. AllowedAction(Action, aaShortCut))
  4068. {
  4069. // Has to be called before the action as the dialog char is already in queue.
  4070. // So when the action consumes message queue, we already need to have the
  4071. // FIgnoreNextDialogChar set
  4072. KeyProcessed(Key, Shift);
  4073. // Reset reference to previous component (when menu/toolbar was clicked).
  4074. // Needed to detect that action was invoked by keyboard shortcut
  4075. // in TNonVisualDataModule::ExplorerActionsExecute
  4076. Action->ActionComponent = NULL;
  4077. Action->Execute();
  4078. return;
  4079. }
  4080. }
  4081. for (int i = 0; i < TTerminalManager::Instance()->Count; i++)
  4082. {
  4083. if (NonVisualDataModule->OpenSessionShortCut(i) == KeyShortCut)
  4084. {
  4085. KeyProcessed(Key, Shift);
  4086. TTerminalManager::Instance()->ActiveTerminalIndex = i;
  4087. return;
  4088. }
  4089. }
  4090. if (Key == VK_TAB && Shift.Contains(ssCtrl))
  4091. {
  4092. KeyProcessed(Key, Shift);
  4093. TTerminalManager::Instance()->CycleTerminals(!Shift.Contains(ssShift));
  4094. }
  4095. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  4096. if (IsCustomShortCut(CustomShortCut))
  4097. {
  4098. CheckCustomCommandShortCut(WinConfiguration->CustomCommandList, Key, Shift, CustomShortCut);
  4099. CheckCustomCommandShortCut(WinConfiguration->ExtensionList, Key, Shift, CustomShortCut);
  4100. if (WinConfiguration->SharedBookmarks != NULL)
  4101. {
  4102. TBookmark * Bookmark = WinConfiguration->SharedBookmarks->FindByShortCut(CustomShortCut);
  4103. if ((Bookmark != NULL) &&
  4104. OpenBookmark(FCurrentSide, Bookmark))
  4105. {
  4106. KeyProcessed(Key, Shift);
  4107. }
  4108. }
  4109. }
  4110. }
  4111. TForm::KeyDown(Key, Shift);
  4112. }
  4113. //---------------------------------------------------------------------------
  4114. void __fastcall TCustomScpExplorerForm::InitStatusBar()
  4115. {
  4116. DebugAssert(Terminal != NULL);
  4117. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4118. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  4119. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4120. int Offset = SessionStatusBar->Panels->Count - SessionPanelCount;
  4121. bool SecurityEnabled = !SessionInfo.SecurityProtocolName.IsEmpty();
  4122. SessionStatusBar->Panels->Items[Offset + 0]->Enabled = SecurityEnabled;
  4123. // expanded from ?: to avoid memory leaks
  4124. if (SecurityEnabled)
  4125. {
  4126. SessionStatusBar->Panels->Items[Offset + 0]->Hint =
  4127. FMTLOAD(STATUS_SECURE, (SessionInfo.SecurityProtocolName));
  4128. }
  4129. else
  4130. {
  4131. SessionStatusBar->Panels->Items[Offset + 0]->Hint = LoadStr(STATUS_INSECURE);
  4132. }
  4133. if (FileSystemInfo.ProtocolName.IsEmpty())
  4134. {
  4135. SessionStatusBar->Panels->Items[Offset + 1]->Caption = SessionInfo.ProtocolName;
  4136. }
  4137. else
  4138. {
  4139. SessionStatusBar->Panels->Items[Offset + 1]->Caption = FileSystemInfo.ProtocolName;
  4140. }
  4141. SessionStatusBar->Panels->Items[Offset + 1]->Hint = LoadStr(STATUS_PROTOCOL_HINT);
  4142. SessionStatusBar->Panels->Items[Offset + 2]->Enabled =
  4143. (!SessionInfo.CSCompression.IsEmpty() || !SessionInfo.SCCompression.IsEmpty());
  4144. UnicodeString CSCompressionStr = DefaultStr(SessionInfo.CSCompression, LoadStr(NO_STR));
  4145. UnicodeString SCCompressionStr = DefaultStr(SessionInfo.SCCompression, LoadStr(NO_STR));
  4146. UnicodeString CompressionStr = CSCompressionStr;
  4147. if (CSCompressionStr != SCCompressionStr)
  4148. {
  4149. CompressionStr += UnicodeString(L"/") + SCCompressionStr;
  4150. }
  4151. SessionStatusBar->Panels->Items[Offset + 2]->Hint = FMTLOAD(STATUS_COMPRESSION_HINT, (CompressionStr));
  4152. SessionStatusBar->Panels->Items[Offset + 3]->Hint = LoadStr(STATUS_DURATION_HINT);
  4153. UpdateStatusBar();
  4154. }
  4155. //---------------------------------------------------------------------------
  4156. void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
  4157. {
  4158. TTBXStatusBar * SessionStatusBar = (TTBXStatusBar *)GetComponent(fcStatusBar);
  4159. DebugAssert(SessionStatusBar != NULL);
  4160. if (!Terminal || !Terminal->Active || Terminal->Status < ssOpened)
  4161. {
  4162. // note: (Terminal->Status < sshReady) currently never happens here,
  4163. // so STATUS_CONNECTING is never used
  4164. SessionStatusBar->SimplePanel = true;
  4165. SessionStatusBar->SimpleText = LoadStr(
  4166. !Terminal || !Terminal->Active ? STATUS_NOT_CONNECTED : STATUS_CONNECTING);
  4167. }
  4168. else
  4169. {
  4170. DebugAssert(Terminal);
  4171. SessionStatusBar->SimplePanel = false;
  4172. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  4173. if (!FNote.IsEmpty())
  4174. {
  4175. SessionStatusBar->Panels->Items[0]->Caption = FNote;
  4176. }
  4177. else
  4178. {
  4179. UpdateStatusPanelText(SessionStatusBar->Panels->Items[0]);
  4180. }
  4181. SessionStatusBar->Panels->Items[0]->Hint = FNoteHints;
  4182. SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1]->Caption =
  4183. FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
  4184. }
  4185. }
  4186. //---------------------------------------------------------------------------
  4187. void __fastcall TCustomScpExplorerForm::UpdateStatusPanelText(TTBXStatusPanel * Panel)
  4188. {
  4189. Panel->Caption = L"";
  4190. }
  4191. //---------------------------------------------------------------------------
  4192. void __fastcall TCustomScpExplorerForm::Idle()
  4193. {
  4194. if (FShowing || FStandaloneEditing)
  4195. {
  4196. FEditorManager->Check();
  4197. // make sure that Idle is called before update queue, as it may invoke QueueEvent
  4198. // that needs to know if queue view is visible (and it may be closed after queue update)
  4199. TTerminalManager::Instance()->Idle(FDoNotIdleCurrentTerminal > 0);
  4200. }
  4201. if (FShowing)
  4202. {
  4203. if (!NonVisualDataModule->Busy &&
  4204. // Menu is opened or drag&drop is going on
  4205. (Mouse->Capture == NULL))
  4206. {
  4207. if (FRefreshRemoteDirectory)
  4208. {
  4209. if ((Terminal != NULL) && (Terminal->Status == ssOpened))
  4210. {
  4211. Terminal->RefreshDirectory();
  4212. }
  4213. FRefreshRemoteDirectory = false;
  4214. }
  4215. if (FRefreshLocalDirectory)
  4216. {
  4217. ReloadLocalDirectory();
  4218. FRefreshLocalDirectory = false;
  4219. }
  4220. if (WinConfiguration->RefreshRemotePanel)
  4221. {
  4222. TManagedTerminal * ManagedTerminal =
  4223. dynamic_cast<TManagedTerminal *>(Terminal);
  4224. if ((ManagedTerminal != NULL) && (Terminal->Status == ssOpened) &&
  4225. (Now() - ManagedTerminal->DirectoryLoaded >
  4226. WinConfiguration->RefreshRemotePanelInterval))
  4227. {
  4228. RemoteDirView->ReloadDirectory();
  4229. }
  4230. }
  4231. }
  4232. }
  4233. if (FShowing || FStandaloneEditing)
  4234. {
  4235. if (FQueueStatusInvalidated)
  4236. {
  4237. UpdateQueueStatus(false);
  4238. }
  4239. RefreshQueueItems();
  4240. }
  4241. if (FShowing)
  4242. {
  4243. UpdateStatusBar();
  4244. }
  4245. FIgnoreNextDialogChar = 0;
  4246. }
  4247. //---------------------------------------------------------------------------
  4248. void __fastcall TCustomScpExplorerForm::UserActionTimer(TObject * /*Sender*/)
  4249. {
  4250. try
  4251. {
  4252. FUserActionTimer->Enabled = false;
  4253. if (IsQueueAutoPopup() && (FPendingQueueActionItem != NULL))
  4254. {
  4255. if (TQueueItem::IsUserActionStatus(FPendingQueueActionItem->Status))
  4256. {
  4257. FPendingQueueActionItem->ProcessUserAction();
  4258. }
  4259. }
  4260. }
  4261. __finally
  4262. {
  4263. FPendingQueueActionItem = NULL;
  4264. }
  4265. }
  4266. //---------------------------------------------------------------------------
  4267. void __fastcall TCustomScpExplorerForm::ApplicationMinimize(TObject * /*Sender*/)
  4268. {
  4269. if (WinConfiguration->MinimizeToTray)
  4270. {
  4271. UpdateTrayIcon();
  4272. FTrayIcon->Visible = true;
  4273. if (Visible)
  4274. {
  4275. ShowWindow(Handle, SW_HIDE);
  4276. }
  4277. }
  4278. }
  4279. //---------------------------------------------------------------------------
  4280. void __fastcall TCustomScpExplorerForm::ApplicationRestore(TObject * /*Sender*/)
  4281. {
  4282. // WORKAROUND
  4283. // When restoring maximized window from minimization,
  4284. // rarely some controls do not align properly.
  4285. // Two instances seen (both for Commander):
  4286. // - When restoring, window is temporarily narrower (not maximized),
  4287. // causing toolbars on TopDock to wrap and dock to expand horizontally.
  4288. // Once maximized already, top dock shinks back, but the session PageControl,
  4289. // do not align up, leaving space between TopDock and PageControl.
  4290. // - Similar issue seen with LocalDirView not aligning down to status bar.
  4291. for (int Index = 0; Index < ControlCount; Index++)
  4292. {
  4293. RealignControl(Controls[Index]);
  4294. }
  4295. if (FTrayIcon->Visible)
  4296. {
  4297. FTrayIcon->Visible = false;
  4298. if (Visible)
  4299. {
  4300. ShowWindow(Handle, SW_SHOW);
  4301. }
  4302. }
  4303. if (FNeedSession && DebugAlwaysTrue(Terminal == NULL))
  4304. {
  4305. FNeedSession = false;
  4306. NonVisualDataModule->StartBusy();
  4307. try
  4308. {
  4309. NeedSession(false);
  4310. }
  4311. __finally
  4312. {
  4313. NonVisualDataModule->EndBusy();
  4314. }
  4315. }
  4316. }
  4317. //---------------------------------------------------------------------------
  4318. void __fastcall TCustomScpExplorerForm::UpdateTrayIcon()
  4319. {
  4320. FTrayIcon->Hint = GetMainForm()->Caption;
  4321. }
  4322. //---------------------------------------------------------------------------
  4323. void __fastcall TCustomScpExplorerForm::ApplicationTitleChanged()
  4324. {
  4325. UpdateTrayIcon();
  4326. }
  4327. //---------------------------------------------------------------------------
  4328. void __fastcall TCustomScpExplorerForm::RestoreApp()
  4329. {
  4330. // workaround
  4331. // TApplication.WndProc sets TApplication.FAppIconic to false,
  4332. // on WM_ACTIVATEAPP, what renders TApplication.Restore no-op function.
  4333. // But WM_ACTIVATEAPP message can be received even when
  4334. // the main window is minimized to the tray and internal editor window is focused
  4335. // (after another application was previously active)
  4336. if (::IsIconic(Handle))
  4337. {
  4338. if (!IsAppIconic())
  4339. {
  4340. SetAppIconic(true);
  4341. }
  4342. }
  4343. ::ApplicationRestore();
  4344. Application->BringToFront();
  4345. }
  4346. //---------------------------------------------------------------------------
  4347. void __fastcall TCustomScpExplorerForm::TrayIconClick(TObject * /*Sender*/)
  4348. {
  4349. RestoreApp();
  4350. }
  4351. //---------------------------------------------------------------------------
  4352. void __fastcall TCustomScpExplorerForm::NewSession(bool FromSite, const UnicodeString & SessionUrl)
  4353. {
  4354. if (OpenInNewWindow())
  4355. {
  4356. // todo: Pass FromSite
  4357. ExecuteNewInstance(SessionUrl);
  4358. }
  4359. else
  4360. {
  4361. TTerminalManager::Instance()->NewSession(FromSite, SessionUrl);
  4362. }
  4363. }
  4364. //---------------------------------------------------------------------------
  4365. UnicodeString __fastcall TCustomScpExplorerForm::CreateHiddenDuplicateSession()
  4366. {
  4367. UnicodeString SessionName = StoredSessions->HiddenPrefix + Terminal->SessionData->SessionName;
  4368. // current working directories become defaults here, what is not right
  4369. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4370. StoredSessions->NewSession(SessionName, SessionData.get());
  4371. // modified only, explicit
  4372. StoredSessions->Save(false, true);
  4373. // encode session name because of slashes in hierarchical sessions
  4374. return EncodeUrlString(SessionName);
  4375. }
  4376. //---------------------------------------------------------------------------
  4377. void __fastcall TCustomScpExplorerForm::DuplicateSession()
  4378. {
  4379. if (OpenInNewWindow())
  4380. {
  4381. ExecuteNewInstance(CreateHiddenDuplicateSession());
  4382. }
  4383. else
  4384. {
  4385. // current working directories become defaults here, what is not right
  4386. std::unique_ptr<TSessionData> SessionData(CloneCurrentSessionData());
  4387. TTerminalManager * Manager = TTerminalManager::Instance();
  4388. TTerminal * Terminal = Manager->NewTerminal(SessionData.get());
  4389. Manager->ActiveTerminal = Terminal;
  4390. }
  4391. }
  4392. //---------------------------------------------------------------------------
  4393. void __fastcall TCustomScpExplorerForm::RenameSession()
  4394. {
  4395. UnicodeString Name = Terminal->SessionData->Name;
  4396. if (InputDialog(LoadStr(RENAME_SESSION_TITLE), LoadStr(RENAME_SESSION_PROMPT), Name, HELP_SESSION_RENAME))
  4397. {
  4398. // Checks for a slash only, so it's not that big deal that we do the check after submitting the dialog only.
  4399. TSessionData::ValidateName(Name);
  4400. // This is inconsistent with how color (for example) is handled.
  4401. Terminal->SessionData->Name = Name;
  4402. UpdateControls();
  4403. // Add/Remove distinguishing paths from sessions of the same name.
  4404. DoTerminalListChanged();
  4405. }
  4406. }
  4407. //---------------------------------------------------------------------------
  4408. bool __fastcall TCustomScpExplorerForm::CanCloseQueue(TTerminalQueue * Queue)
  4409. {
  4410. DebugAssert(Queue != NULL);
  4411. bool Result = Queue->IsEmpty;
  4412. if (!Result)
  4413. {
  4414. SetFocus();
  4415. Result = (MessageDialog(LoadStr(PENDING_QUEUE_ITEMS2), qtWarning, qaOK | qaCancel, HELP_NONE) == qaOK);
  4416. }
  4417. return Result;
  4418. }
  4419. //---------------------------------------------------------------------------
  4420. bool __fastcall TCustomScpExplorerForm::CanCloseQueue()
  4421. {
  4422. return CanCloseQueue(FQueue);
  4423. }
  4424. //---------------------------------------------------------------------------
  4425. void __fastcall TCustomScpExplorerForm::CloseSession()
  4426. {
  4427. if (CanCloseQueue())
  4428. {
  4429. TTerminalManager::Instance()->FreeActiveTerminal();
  4430. }
  4431. }
  4432. //---------------------------------------------------------------------------
  4433. void __fastcall TCustomScpExplorerForm::OpenStoredSession(TSessionData * Data)
  4434. {
  4435. if (OpenInNewWindow())
  4436. {
  4437. // encode session name because of slashes in hierarchical sessions
  4438. ExecuteNewInstance(EncodeUrlString(Data->Name));
  4439. }
  4440. else
  4441. {
  4442. TTerminalManager * Manager = TTerminalManager::Instance();
  4443. TTerminal * Terminal = Manager->NewTerminal(Data);
  4444. Manager->ActiveTerminal = Terminal;
  4445. }
  4446. }
  4447. //---------------------------------------------------------------------------
  4448. void __fastcall TCustomScpExplorerForm::OpenFolderOrWorkspace(const UnicodeString & Name)
  4449. {
  4450. if (OpenInNewWindow())
  4451. {
  4452. ExecuteNewInstance(Name);
  4453. }
  4454. else
  4455. {
  4456. TTerminalManager * Manager = TTerminalManager::Instance();
  4457. std::unique_ptr<TObjectList> DataList(new TObjectList());
  4458. StoredSessions->GetFolderOrWorkspace(Name, DataList.get());
  4459. Manager->ActiveTerminal = Manager->NewTerminals(DataList.get());
  4460. }
  4461. }
  4462. //---------------------------------------------------------------------------
  4463. void __fastcall TCustomScpExplorerForm::FormCloseQuery(TObject * /*Sender*/,
  4464. bool &CanClose)
  4465. {
  4466. if (Terminal != NULL)
  4467. {
  4468. if (Terminal->Active && WinConfiguration->ConfirmClosingSession)
  4469. {
  4470. unsigned int Result;
  4471. TMessageParams Params(mpNeverAskAgainCheck);
  4472. UnicodeString Message;
  4473. int Answers = qaOK | qaCancel;
  4474. if (TTerminalManager::Instance()->Count > 1)
  4475. {
  4476. if (!WinConfiguration->AutoSaveWorkspace)
  4477. {
  4478. Message = LoadStr(CLOSE_SESSIONS_WORKSPACE2);
  4479. Answers = qaYes | qaNo | qaCancel;
  4480. }
  4481. else
  4482. {
  4483. Message = MainInstructions(LoadStr(CLOSE_SESSIONS));
  4484. }
  4485. }
  4486. else
  4487. {
  4488. Message = MainInstructions(FMTLOAD(CLOSE_SESSION, (Terminal->SessionData->SessionName)));
  4489. }
  4490. if (WinConfiguration->AutoSaveWorkspace)
  4491. {
  4492. Message =
  4493. FORMAT("%s\n\n%s", (Message,
  4494. FMTLOAD(AUTO_WORKSPACE, (WorkspaceName()))));
  4495. }
  4496. SetFocus();
  4497. Result = MessageDialog(Message, qtConfirmation,
  4498. Answers, HELP_NONE, &Params);
  4499. if (Result == qaNeverAskAgain)
  4500. {
  4501. WinConfiguration->ConfirmClosingSession = false;
  4502. }
  4503. if (Result == qaNo)
  4504. {
  4505. CanClose = SaveWorkspace(true);
  4506. // note that the workspace will be saved redundatly again from FormClose
  4507. }
  4508. else
  4509. {
  4510. CanClose =
  4511. (Result == qaOK) ||
  4512. (Result == qaYes) || // CLOSE_SESSIONS_WORKSPACE variant
  4513. (Result == qaNeverAskAgain);
  4514. }
  4515. }
  4516. if (CanClose)
  4517. {
  4518. CanClose = CanCloseQueue();
  4519. }
  4520. }
  4521. if (CanClose)
  4522. {
  4523. CanClose =
  4524. FEditorManager->CloseInternalEditors(CloseInternalEditor) &&
  4525. FEditorManager->CloseExternalFilesWithoutProcess();
  4526. if (CanClose)
  4527. {
  4528. while (CanClose && (FLocalEditors->Count > 0))
  4529. {
  4530. int PrevCount = FLocalEditors->Count;
  4531. static_cast<TForm *>(FLocalEditors->Items[0])->Close();
  4532. CanClose = (FLocalEditors->Count < PrevCount);
  4533. }
  4534. if (CanClose)
  4535. {
  4536. CanClose = FEditorManager->Empty(true);
  4537. if (!CanClose)
  4538. {
  4539. SetFocus();
  4540. CanClose =
  4541. (MessageDialog(
  4542. LoadStr(PENDING_EDITORS), qtWarning, qaIgnore | qaCancel, HELP_NONE) == qaIgnore);
  4543. }
  4544. }
  4545. }
  4546. }
  4547. }
  4548. //---------------------------------------------------------------------------
  4549. void __fastcall TCustomScpExplorerForm::CloseInternalEditor(TObject * Sender)
  4550. {
  4551. TForm * Form = dynamic_cast<TForm *>(Sender);
  4552. DebugAssert(Form != NULL);
  4553. Form->Close();
  4554. }
  4555. //---------------------------------------------------------------------------
  4556. void __fastcall TCustomScpExplorerForm::ForceCloseInternalEditor(TObject * Sender)
  4557. {
  4558. TForm * Form = dynamic_cast<TForm *>(Sender);
  4559. delete Form;
  4560. }
  4561. //---------------------------------------------------------------------------
  4562. void __fastcall TCustomScpExplorerForm::ForceCloseLocalEditors()
  4563. {
  4564. while (FLocalEditors->Count > 0)
  4565. {
  4566. delete static_cast<TForm *>(FLocalEditors->Items[0]);
  4567. }
  4568. }
  4569. //---------------------------------------------------------------------------
  4570. void __fastcall TCustomScpExplorerForm::RemoteDirViewDisplayProperties(
  4571. TObject *Sender)
  4572. {
  4573. TStrings *FileList = ((TUnixDirView*)Sender)->CreateFileList(True, False, NULL);
  4574. try
  4575. {
  4576. SetProperties(osRemote, FileList);
  4577. }
  4578. __finally
  4579. {
  4580. delete FileList;
  4581. }
  4582. }
  4583. //---------------------------------------------------------------------------
  4584. void __fastcall TCustomScpExplorerForm::ComponentShowing(Byte Component, bool value)
  4585. {
  4586. if (value)
  4587. {
  4588. if (Component == fcCustomCommandsBand)
  4589. {
  4590. UpdateCustomCommandsToolbar();
  4591. }
  4592. }
  4593. }
  4594. //---------------------------------------------------------------------------
  4595. void __fastcall TCustomScpExplorerForm::SetComponentVisible(Byte Component, Boolean value)
  4596. {
  4597. TControl * Control = GetComponent(Component);
  4598. DebugAssert(Control);
  4599. bool Changed = (Control->Visible != value);
  4600. if (Changed)
  4601. {
  4602. ComponentShowing(Component, value);
  4603. TWinControl * WinControl = dynamic_cast<TWinControl*>(Control);
  4604. bool WasFocused = (WinControl != NULL) && (ActiveControl != NULL) &&
  4605. ((ActiveControl == WinControl) || (ActiveControl->Parent == WinControl));
  4606. if (value)
  4607. {
  4608. int RemainingHeight = Control->Parent->ClientHeight;
  4609. int RemainingWidth = Control->Parent->ClientWidth;
  4610. for (int i = 0; i < Control->Parent->ControlCount; i++)
  4611. {
  4612. TControl * ChildControl = Control->Parent->Controls[i];
  4613. if (ChildControl->Visible)
  4614. {
  4615. switch (ChildControl->Align)
  4616. {
  4617. case alTop:
  4618. case alBottom:
  4619. RemainingHeight -= ChildControl->Height;
  4620. break;
  4621. case alLeft:
  4622. case alRight:
  4623. RemainingWidth -= ChildControl->Width;
  4624. break;
  4625. }
  4626. }
  4627. }
  4628. int Reserve = ScaleByTextHeight(this, 32);
  4629. // queue in explorer, trees in commander
  4630. if (Control->Height > RemainingHeight - Reserve)
  4631. {
  4632. Control->Height = RemainingHeight / 2;
  4633. }
  4634. if (Control->Width > RemainingWidth - Reserve)
  4635. {
  4636. Control->Width = RemainingWidth / 2;
  4637. }
  4638. }
  4639. Control->Visible = value;
  4640. if (WasFocused && Visible)
  4641. {
  4642. DirView(osCurrent)->SetFocus();
  4643. }
  4644. FixControlsPlacement();
  4645. }
  4646. }
  4647. //---------------------------------------------------------------------------
  4648. bool __fastcall TCustomScpExplorerForm::GetComponentVisible(Byte Component)
  4649. {
  4650. TControl * Control = GetComponent(Component);
  4651. if (Control == NULL)
  4652. {
  4653. return false;
  4654. }
  4655. else
  4656. {
  4657. return Control->Visible;
  4658. }
  4659. }
  4660. //---------------------------------------------------------------------------
  4661. bool __fastcall TCustomScpExplorerForm::IsComponentPossible(Byte Component)
  4662. {
  4663. bool Result;
  4664. if ((Component == fcExplorerUpdatesBand) || (Component == fcCommanderUpdatesBand))
  4665. {
  4666. Result = !IsUWP();
  4667. }
  4668. else
  4669. {
  4670. Result = true;
  4671. }
  4672. return Result;
  4673. }
  4674. //---------------------------------------------------------------------------
  4675. void __fastcall TCustomScpExplorerForm::FixControlsPlacement()
  4676. {
  4677. if (RemoteDirView->ItemFocused != NULL)
  4678. {
  4679. RemoteDirView->ItemFocused->MakeVisible(false);
  4680. }
  4681. QueueSplitter->Visible = QueuePanel->Visible;
  4682. RemotePanelSplitter->Visible = RemoteDriveView->Visible;
  4683. }
  4684. //---------------------------------------------------------------------------
  4685. TControl * __fastcall TCustomScpExplorerForm::GetComponent(Byte Component)
  4686. {
  4687. switch (Component) {
  4688. case fcStatusBar: return RemoteStatusBar;
  4689. case fcRemotePopup: return reinterpret_cast<TControl *>(NonVisualDataModule->RemoteFilePopup);
  4690. case fcQueueView: return QueuePanel;
  4691. case fcQueueToolbar: return QueueDock;
  4692. case fcRemoteTree: return RemoteDriveView;
  4693. case fcSessionsTabs: return SessionsPageControl;
  4694. default: return NULL;
  4695. }
  4696. }
  4697. //---------------------------------------------------------------------------
  4698. void __fastcall TCustomScpExplorerForm::DirViewColumnRightClick(
  4699. TObject *Sender, TListColumn *Column, TPoint &Point)
  4700. {
  4701. DebugAssert(NonVisualDataModule && Column && Sender);
  4702. NonVisualDataModule->ListColumn = Column;
  4703. TPopupMenu * DirViewColumnMenu;
  4704. if (Sender == RemoteDirView)
  4705. {
  4706. DirViewColumnMenu = NonVisualDataModule->RemoteDirViewColumnPopup;
  4707. NonVisualDataModule->RemoteSortByExtColumnPopupItem->Visible =
  4708. (Column->Index == uvName);
  4709. NonVisualDataModule->RemoteFormatSizeBytesPopupItem->Visible =
  4710. (Column->Index == uvSize);
  4711. }
  4712. else
  4713. {
  4714. DirViewColumnMenu = NonVisualDataModule->LocalDirViewColumnPopup;
  4715. NonVisualDataModule->LocalSortByExtColumnPopupItem->Visible =
  4716. (Column->Index == dvName);
  4717. NonVisualDataModule->LocalFormatSizeBytesPopupItem->Visible =
  4718. (Column->Index == dvSize);
  4719. }
  4720. TCustomListView * ListView = DebugNotNull(dynamic_cast<TCustomListView *>(Sender));
  4721. TPoint ScreenPoint = ListView->ClientToScreen(Point);
  4722. ScreenPoint.x -= GetScrollPos(ListView->Handle, SB_HORZ);
  4723. DirViewColumnMenu->Popup(ScreenPoint.x, ScreenPoint.y);
  4724. }
  4725. //---------------------------------------------------------------------------
  4726. void __fastcall TCustomScpExplorerForm::DirViewExecFile(
  4727. TObject *Sender, TListItem *Item, bool &AllowExec)
  4728. {
  4729. DoDirViewExecFile(Sender, Item, AllowExec);
  4730. }
  4731. //---------------------------------------------------------------------------
  4732. void __fastcall TCustomScpExplorerForm::DoDirViewExecFile(TObject * Sender,
  4733. TListItem * Item, bool & AllowExec)
  4734. {
  4735. DebugAssert(Sender && Item && Configuration);
  4736. DebugAssert(AllowExec);
  4737. TCustomDirView * ADirView = (TCustomDirView *)Sender;
  4738. bool Remote = (ADirView == DirView(osRemote));
  4739. bool ResolvedSymlinks = !Remote || Terminal->ResolvingSymlinks;
  4740. TOperationSide Side = (Remote ? osRemote : osLocal);
  4741. // Anything special is done on double click only (not on "open" indicated by FForceExecution),
  4742. // on files only (not directories)
  4743. // and only when symlinks are resolved (apply to remote panel only)
  4744. if (!ADirView->ItemIsDirectory(Item) &&
  4745. (ResolvedSymlinks || FForceExecution || Terminal->IsEncryptingFiles()))
  4746. {
  4747. if ((WinConfiguration->DoubleClickAction != dcaOpen) &&
  4748. !FForceExecution &&
  4749. (ResolvedSymlinks || Terminal->IsEncryptingFiles()))
  4750. {
  4751. if (WinConfiguration->DoubleClickAction == dcaCopy)
  4752. {
  4753. // To counter the lock in DirViewBusy (called from TCustomDirView.DoExecute)
  4754. UnlockWindow();
  4755. try
  4756. {
  4757. ExecuteFileOperation(
  4758. foCopy, Side, true, !WinConfiguration->CopyOnDoubleClickConfirmation);
  4759. }
  4760. __finally
  4761. {
  4762. LockWindow();
  4763. }
  4764. AllowExec = false;
  4765. }
  4766. else if (WinConfiguration->DoubleClickAction == dcaEdit)
  4767. {
  4768. if (!Remote || !WinConfiguration->DisableOpenEdit)
  4769. {
  4770. ExecuteFile(Side, efDefaultEditor);
  4771. AllowExec = false;
  4772. }
  4773. }
  4774. else
  4775. {
  4776. DebugFail();
  4777. }
  4778. }
  4779. // if we have not done anything special, fall back to default behavior
  4780. if (AllowExec)
  4781. {
  4782. if (Remote && !WinConfiguration->DisableOpenEdit)
  4783. {
  4784. ExecuteFile(osRemote, efShell);
  4785. AllowExec = false;
  4786. }
  4787. }
  4788. }
  4789. }
  4790. //---------------------------------------------------------------------------
  4791. bool __fastcall TCustomScpExplorerForm::GetHasDirView(TOperationSide Side)
  4792. {
  4793. return ((Side == osRemote) || (Side == osCurrent));
  4794. }
  4795. //---------------------------------------------------------------------------
  4796. void __fastcall TCustomScpExplorerForm::CompareDirectories()
  4797. {
  4798. DebugFail();
  4799. }
  4800. //---------------------------------------------------------------------------
  4801. void __fastcall TCustomScpExplorerForm::SynchronizeDirectories()
  4802. {
  4803. DebugFail();
  4804. }
  4805. //---------------------------------------------------------------------------
  4806. bool __fastcall TCustomScpExplorerForm::DoSynchronizeDirectories(
  4807. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, bool UseDefaults)
  4808. {
  4809. TSynchronizeParamType Params;
  4810. Params.LocalDirectory = LocalDirectory;
  4811. Params.RemoteDirectory = RemoteDirectory;
  4812. int UnusedParams =
  4813. (GUIConfiguration->SynchronizeParams &
  4814. (TTerminal::spPreviewChanges | TTerminal::spTimestamp | TTerminal::spNotByTime | TTerminal::spBySize));
  4815. Params.Params = GUIConfiguration->SynchronizeParams & ~UnusedParams;
  4816. Params.Options = GUIConfiguration->SynchronizeOptions;
  4817. bool SaveSettings = false;
  4818. TSynchronizeController Controller(&DoSynchronize, &DoSynchronizeInvalid,
  4819. &DoSynchronizeTooManyDirectories);
  4820. DebugAssert(FSynchronizeController == NULL);
  4821. FSynchronizeController = &Controller;
  4822. bool Result;
  4823. try
  4824. {
  4825. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  4826. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).Upload;
  4827. int Options =
  4828. FLAGMASK(SynchronizeAllowSelectedOnly(), soAllowSelectedOnly);
  4829. DebugAssert(FOnFeedSynchronizeError == NULL);
  4830. Result = DoSynchronizeDialog(Params, &CopyParam, Controller.StartStop,
  4831. SaveSettings, Options, CopyParamAttrs, GetSynchronizeOptions, SynchronizeSessionLog,
  4832. FOnFeedSynchronizeError, SynchronizeInNewWindow, UseDefaults);
  4833. if (Result)
  4834. {
  4835. if (SaveSettings)
  4836. {
  4837. GUIConfiguration->SynchronizeParams = Params.Params | UnusedParams;
  4838. GUIConfiguration->SynchronizeOptions = Params.Options;
  4839. }
  4840. else
  4841. {
  4842. if (FLAGSET(GUIConfiguration->SynchronizeOptions, soSynchronizeAsk) &&
  4843. FLAGCLEAR(Params.Options, soSynchronizeAsk) &&
  4844. FLAGSET(Params.Options, soSynchronize))
  4845. {
  4846. GUIConfiguration->SynchronizeOptions =
  4847. (GUIConfiguration->SynchronizeOptions & ~soSynchronizeAsk) |
  4848. soSynchronize;
  4849. }
  4850. }
  4851. LocalDirectory = Params.LocalDirectory;
  4852. RemoteDirectory = Params.RemoteDirectory;
  4853. }
  4854. }
  4855. __finally
  4856. {
  4857. FSynchronizeController = NULL;
  4858. DebugAssert(FOnFeedSynchronizeError == NULL);
  4859. FOnFeedSynchronizeError = NULL;
  4860. }
  4861. return Result;
  4862. }
  4863. //---------------------------------------------------------------------------
  4864. void __fastcall TCustomScpExplorerForm::DoSynchronize(
  4865. TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
  4866. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  4867. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  4868. TSynchronizeOptions * Options, bool Full)
  4869. {
  4870. // Keep-up-to-date mode
  4871. if (Terminal->Status == ssOpened)
  4872. {
  4873. try
  4874. {
  4875. int PParams = Params.Params;
  4876. if (!Full)
  4877. {
  4878. PParams |= TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress;
  4879. if (FLAGSET(Params.Options, soRecurse))
  4880. {
  4881. PParams |= TTerminal::spSubDirs;
  4882. }
  4883. }
  4884. else
  4885. {
  4886. // if keepuptodate is non-recursive,
  4887. // full sync before has to be non-recursive as well
  4888. if (FLAGCLEAR(Params.Options, soRecurse))
  4889. {
  4890. PParams |= TTerminal::spNoRecurse;
  4891. }
  4892. }
  4893. Synchronize(LocalDirectory, RemoteDirectory, smRemote, CopyParam,
  4894. PParams, Checklist, Options);
  4895. }
  4896. catch (Exception & E)
  4897. {
  4898. if (FLAGSET(Params.Options, soContinueOnError) && Terminal->Active &&
  4899. DebugAlwaysTrue(FOnFeedSynchronizeError != NULL))
  4900. {
  4901. // noop, already logged in MoreMessageDialog
  4902. }
  4903. else
  4904. {
  4905. ShowExtendedExceptionEx(Terminal, &E);
  4906. throw;
  4907. }
  4908. }
  4909. }
  4910. }
  4911. //---------------------------------------------------------------------------
  4912. void __fastcall TCustomScpExplorerForm::DoSynchronizeInvalid(
  4913. TSynchronizeController * /*Sender*/, const UnicodeString Directory,
  4914. const UnicodeString ErrorStr)
  4915. {
  4916. if (!Directory.IsEmpty())
  4917. {
  4918. SimpleErrorDialog(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), ErrorStr);
  4919. }
  4920. else
  4921. {
  4922. SimpleErrorDialog(LoadStr(WATCH_ERROR_GENERAL), ErrorStr);
  4923. }
  4924. }
  4925. //---------------------------------------------------------------------------
  4926. void __fastcall TCustomScpExplorerForm::DoSynchronizeTooManyDirectories(
  4927. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  4928. {
  4929. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  4930. {
  4931. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  4932. }
  4933. else
  4934. {
  4935. TMessageParams Params(mpNeverAskAgainCheck);
  4936. unsigned int Result = MessageDialog(
  4937. FMTLOAD(TOO_MANY_WATCH_DIRECTORIES, (MaxDirectories, MaxDirectories)),
  4938. qtConfirmation, qaYes | qaNo, HELP_TOO_MANY_WATCH_DIRECTORIES, &Params);
  4939. if ((Result == qaYes) || (Result == qaNeverAskAgain))
  4940. {
  4941. MaxDirectories *= 2;
  4942. if (Result == qaNeverAskAgain)
  4943. {
  4944. GUIConfiguration->MaxWatchDirectories = MaxDirectories;
  4945. }
  4946. }
  4947. else
  4948. {
  4949. Abort();
  4950. }
  4951. }
  4952. }
  4953. //---------------------------------------------------------------------------
  4954. void __fastcall TCustomScpExplorerForm::Synchronize(const UnicodeString LocalDirectory,
  4955. const UnicodeString RemoteDirectory, TSynchronizeMode Mode,
  4956. const TCopyParamType & CopyParam, int Params, TSynchronizeChecklist ** Checklist,
  4957. TSynchronizeOptions * Options)
  4958. {
  4959. // Keep-up-to-date mode
  4960. DebugAssert(!FAutoOperation);
  4961. void * BatchStorage;
  4962. BatchStart(BatchStorage);
  4963. FAutoOperation = true;
  4964. bool AnyOperation = false;
  4965. TDateTime StartTime = Now();
  4966. TSynchronizeChecklist * AChecklist = NULL;
  4967. try
  4968. {
  4969. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  4970. if (FLAGCLEAR(Params, TTerminal::spDelayProgress))
  4971. {
  4972. FSynchronizeProgressForm->Start();
  4973. }
  4974. AChecklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  4975. static_cast<TTerminal::TSynchronizeMode>(Mode),
  4976. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  4977. Options);
  4978. SAFE_DESTROY(FSynchronizeProgressForm);
  4979. AnyOperation = (AChecklist->CheckedCount > 0);
  4980. if (AnyOperation)
  4981. {
  4982. TSynchronizeProgress SynchronizeProgress(AChecklist);
  4983. CreateProgressForm(&SynchronizeProgress);
  4984. try
  4985. {
  4986. Terminal->SynchronizeApply(
  4987. AChecklist, &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  4988. SynchronizeProcessedItem, NULL, NULL, NULL);
  4989. }
  4990. __finally
  4991. {
  4992. DestroyProgressForm();
  4993. }
  4994. }
  4995. }
  4996. __finally
  4997. {
  4998. if (Checklist == NULL)
  4999. {
  5000. delete AChecklist;
  5001. }
  5002. else
  5003. {
  5004. *Checklist = AChecklist;
  5005. }
  5006. FAutoOperation = false;
  5007. SAFE_DESTROY(FSynchronizeProgressForm);
  5008. BatchEnd(BatchStorage);
  5009. ReloadLocalDirectory();
  5010. if (AnyOperation)
  5011. {
  5012. OperationComplete(StartTime);
  5013. }
  5014. }
  5015. }
  5016. //---------------------------------------------------------------------------
  5017. bool __fastcall TCustomScpExplorerForm::SynchronizeAllowSelectedOnly()
  5018. {
  5019. // can be called from command line
  5020. return Visible &&
  5021. ((DirView(osRemote)->SelCount > 0) ||
  5022. (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0)));
  5023. }
  5024. //---------------------------------------------------------------------------
  5025. void __fastcall TCustomScpExplorerForm::SynchronizeSessionLog(const UnicodeString & Message)
  5026. {
  5027. LogSynchronizeEvent(Terminal, Message);
  5028. }
  5029. //---------------------------------------------------------------------------
  5030. void __fastcall TCustomScpExplorerForm::GetSynchronizeOptions(
  5031. int Params, TSynchronizeOptions & Options)
  5032. {
  5033. if (FLAGSET(Params, TTerminal::spSelectedOnly) && SynchronizeAllowSelectedOnly())
  5034. {
  5035. Options.Filter = new TStringList();
  5036. Options.Filter->CaseSensitive = false;
  5037. Options.Filter->Duplicates = Types::dupAccept;
  5038. if (DirView(osRemote)->SelCount > 0)
  5039. {
  5040. DirView(osRemote)->CreateFileList(false, false, Options.Filter);
  5041. }
  5042. if (HasDirView[osLocal] && (DirView(osLocal)->SelCount > 0))
  5043. {
  5044. DirView(osLocal)->CreateFileList(false, false, Options.Filter);
  5045. }
  5046. Options.Filter->Sort();
  5047. }
  5048. }
  5049. //---------------------------------------------------------------------------
  5050. void __fastcall TCustomScpExplorerForm::SynchronizeInNewWindow(
  5051. const TSynchronizeParamType & Params, const TCopyParamType * CopyParams)
  5052. {
  5053. UnicodeString SessionName = CreateHiddenDuplicateSession();
  5054. UnicodeString AdditionalParams =
  5055. FORMAT(L"%s \"%s\" \"%s\" %d %d %s", (
  5056. TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH),
  5057. Params.LocalDirectory, Params.RemoteDirectory, Params.Params, Params.Options,
  5058. TProgramParams::FormatSwitch(DEFAULTS_SWITCH)));
  5059. TCopyParamType Defaults;
  5060. std::unique_ptr<THierarchicalStorage> ConfigStorage(Configuration->CreateConfigStorage());
  5061. ConfigStorage->AccessMode = smRead;
  5062. if (ConfigStorage->OpenSubKey(Configuration->ConfigurationSubKey, false, false))
  5063. {
  5064. GUIConfiguration->LoadCopyParam(ConfigStorage.get(), &Defaults);
  5065. }
  5066. ConfigStorage.reset(NULL);
  5067. std::unique_ptr<TStringList> Options(new TStringList());
  5068. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Options.get(), true));
  5069. GUIConfiguration->SaveCopyParam(OptionsStorage.get(), CopyParams, &Defaults);
  5070. if (Options->Count > 0)
  5071. {
  5072. AdditionalParams +=
  5073. FORMAT(L" %s%s", (TProgramParams::FormatSwitch(RAW_CONFIG_SWITCH), StringsToParams(Options.get())));
  5074. }
  5075. ExecuteNewInstance(SessionName, AdditionalParams);
  5076. }
  5077. //---------------------------------------------------------------------------
  5078. struct TSynchronizeParams
  5079. {
  5080. UnicodeString LocalDirectory;
  5081. UnicodeString RemoteDirectory;
  5082. TSynchronizeMode Mode;
  5083. TSynchronizeChecklist * Checklist;
  5084. int Params;
  5085. TCopyParamType * CopyParam;
  5086. TDateTime CollectElapsed;
  5087. TDateTime * StartTime;
  5088. TProcessedSynchronizationChecklistItem OnProcessedItem;
  5089. };
  5090. //---------------------------------------------------------------------------
  5091. void __fastcall TCustomScpExplorerForm::FullSynchronize(
  5092. TSynchronizeParams & Params, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5093. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5094. {
  5095. DebugAssert(!FAutoOperation);
  5096. void * BatchStorage;
  5097. BatchStart(BatchStorage);
  5098. FAutoOperation = true;
  5099. TFileOperationStatistics Statistics;
  5100. TDateTime Start = Now();
  5101. try
  5102. {
  5103. Params.OnProcessedItem = OnProcessedItem;
  5104. TSynchronizeProgress SynchronizeProgress(Params.Checklist);
  5105. CreateProgressForm(&SynchronizeProgress);
  5106. Terminal->SynchronizeApply(
  5107. Params.Checklist, Params.CopyParam, Params.Params | TTerminal::spNoConfirmation,
  5108. TerminalSynchronizeDirectory, SynchronizeProcessedItem, OnUpdatedSynchronizationChecklistItems, &Params,
  5109. &Statistics);
  5110. }
  5111. __finally
  5112. {
  5113. Params.OnProcessedItem = NULL;
  5114. FAutoOperation = false;
  5115. DestroyProgressForm();
  5116. BatchEnd(BatchStorage);
  5117. ReloadLocalDirectory();
  5118. }
  5119. if (WinConfiguration->SynchronizeSummary)
  5120. {
  5121. UnicodeString Message = MainInstructions(LoadStr(SYNCHRONIZE_COMPLETE)) + L"\n";
  5122. // The statistics should be 0 anyway in this case
  5123. if (FLAGCLEAR(Params.Params, TTerminal::spTimestamp))
  5124. {
  5125. Message += L"\n";
  5126. if (Statistics.FilesUploaded > 0)
  5127. {
  5128. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 1), (FormatNumber(Statistics.FilesUploaded), FormatBytes(Statistics.TotalUploaded))) + L"\n";
  5129. }
  5130. if (Statistics.FilesDownloaded > 0)
  5131. {
  5132. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 2), (FormatNumber(Statistics.FilesDownloaded), FormatBytes(Statistics.TotalDownloaded))) + L"\n";
  5133. }
  5134. if (Statistics.FilesDeletedLocal > 0)
  5135. {
  5136. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 3), (FormatNumber(Statistics.FilesDeletedLocal))) + L"\n";
  5137. }
  5138. if (Statistics.FilesDeletedRemote > 0)
  5139. {
  5140. Message += FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 4), (FormatNumber(Statistics.FilesDeletedRemote))) + L"\n";
  5141. }
  5142. }
  5143. TDateTime Elapsed = (Now() - Start);
  5144. Message +=
  5145. L"\n" +
  5146. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
  5147. FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
  5148. TMessageParams Params(mpNeverAskAgainCheck);
  5149. unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
  5150. if (Result == qaNeverAskAgain)
  5151. {
  5152. WinConfiguration->SynchronizeSummary = false;
  5153. }
  5154. }
  5155. }
  5156. //---------------------------------------------------------------------------
  5157. void __fastcall TCustomScpExplorerForm::SynchronizeProcessedItem(void * Token, const TSynchronizeChecklist::TItem * ChecklistItem)
  5158. {
  5159. if (DebugAlwaysTrue(FProgressForm != NULL) && DebugAlwaysTrue(FProgressForm->SynchronizeProgress != NULL))
  5160. {
  5161. FProgressForm->SynchronizeProgress->ItemProcessed(ChecklistItem);
  5162. }
  5163. // Not set in keep-up-to-date mode - Synchronize() method
  5164. if (Token != NULL)
  5165. {
  5166. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5167. if (Params.OnProcessedItem != NULL)
  5168. {
  5169. Params.OnProcessedItem(NULL, ChecklistItem);
  5170. }
  5171. }
  5172. }
  5173. //---------------------------------------------------------------------------
  5174. void __fastcall TCustomScpExplorerForm::DoFullSynchronize(
  5175. void * Token, TProcessedSynchronizationChecklistItem OnProcessedItem,
  5176. TUpdatedSynchronizationChecklistItems OnUpdatedSynchronizationChecklistItems)
  5177. {
  5178. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5179. *Params.StartTime = Now();
  5180. FullSynchronize(Params, OnProcessedItem, OnUpdatedSynchronizationChecklistItems);
  5181. }
  5182. //---------------------------------------------------------------------------
  5183. void __fastcall TCustomScpExplorerForm::DoSynchronizeChecklistCalculateSize(
  5184. TSynchronizeChecklist * Checklist, const TSynchronizeChecklist::TItemList & Items, void * Token)
  5185. {
  5186. // terminal can be already closed (e.g. dropped connection)
  5187. if (Terminal != NULL)
  5188. {
  5189. TSynchronizeParams & Params = *static_cast<TSynchronizeParams *>(Token);
  5190. Terminal->SynchronizeChecklistCalculateSize(Checklist, Items, Params.CopyParam);
  5191. }
  5192. }
  5193. //---------------------------------------------------------------------------
  5194. void __fastcall TCustomScpExplorerForm::DoSynchronizeMove(
  5195. TOperationSide Side, const UnicodeString & FileName, const UnicodeString & NewFileName, TRemoteFile * RemoteFile)
  5196. {
  5197. TAutoBatch AutoBatch(this);
  5198. TAutoFlag AutoOperationFlag(FAutoOperation);
  5199. if (Side == osRemote)
  5200. {
  5201. std::unique_ptr<TStrings> FileList(new TStringList());
  5202. FileList->AddObject(FileName, RemoteFile);
  5203. UnicodeString Target = UnixExtractFileDir(NewFileName);
  5204. UnicodeString FileMask = DelimitFileNameMask(UnixExtractFileName(NewFileName));
  5205. RemoteDirView->SaveSelection();
  5206. RemoteDirView->SaveSelectedNames();
  5207. try
  5208. {
  5209. Terminal->MoveFiles(FileList.get(), Target, FileMask);
  5210. }
  5211. catch(...)
  5212. {
  5213. RemoteDirView->DiscardSavedSelection();
  5214. throw;
  5215. }
  5216. RemoteDirView->RestoreSelection();
  5217. }
  5218. else if (DebugAlwaysTrue(Side == osLocal))
  5219. {
  5220. if (!MoveFile(FileName.c_str(), NewFileName.c_str()))
  5221. {
  5222. throw EOSExtException(FMTLOAD(RENAME_FILE_ERROR, (FileName, NewFileName)));
  5223. }
  5224. UnicodeString Directory = ExtractFileDir(FileName);
  5225. ReloadLocalDirectory(Directory);
  5226. UnicodeString NewDirectory = ExtractFileDir(NewFileName);
  5227. if (!SamePaths(Directory, NewDirectory))
  5228. {
  5229. ReloadLocalDirectory(NewDirectory);
  5230. }
  5231. }
  5232. }
  5233. //---------------------------------------------------------------------------
  5234. int __fastcall TCustomScpExplorerForm::DoFullSynchronizeDirectories(
  5235. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  5236. TSynchronizeMode & Mode, bool & SaveMode, bool UseDefaults)
  5237. {
  5238. int Result;
  5239. int Params = GUIConfiguration->SynchronizeParams;
  5240. bool SaveSettings = false;
  5241. int Options =
  5242. FLAGMASK(!Terminal->IsCapable[fcTimestampChanging], fsoDisableTimestamp) |
  5243. FLAGMASK(SynchronizeAllowSelectedOnly(), fsoAllowSelectedOnly);
  5244. TCopyParamType CopyParam = GUIConfiguration->CurrentCopyParam;
  5245. TUsableCopyParamAttrs CopyParamAttrs = Terminal->UsableCopyParamAttrs(0);
  5246. bool Continue =
  5247. UseDefaults ||
  5248. DoFullSynchronizeDialog(Mode, Params, LocalDirectory, RemoteDirectory,
  5249. &CopyParam, SaveSettings, SaveMode, Options, CopyParamAttrs);
  5250. if (Continue)
  5251. {
  5252. Configuration->Usage->Inc(L"Synchronizations");
  5253. UpdateCopyParamCounters(CopyParam);
  5254. TSynchronizeOptions SynchronizeOptions;
  5255. GetSynchronizeOptions(Params, SynchronizeOptions);
  5256. if (SaveSettings)
  5257. {
  5258. GUIConfiguration->SynchronizeParams = Params;
  5259. }
  5260. else
  5261. {
  5262. SaveMode = false;
  5263. }
  5264. TDateTime StartTime = Now();
  5265. TSynchronizeChecklist * Checklist = NULL;
  5266. try
  5267. {
  5268. DebugAssert(!FAutoOperation);
  5269. FAutoOperation = true;
  5270. try
  5271. {
  5272. FSynchronizeProgressForm = new TSynchronizeProgressForm(Application, true, true);
  5273. FSynchronizeProgressForm->Start();
  5274. Checklist = Terminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  5275. static_cast<TTerminal::TSynchronizeMode>(Mode),
  5276. &CopyParam, Params | TTerminal::spNoConfirmation, TerminalSynchronizeDirectory,
  5277. &SynchronizeOptions);
  5278. }
  5279. __finally
  5280. {
  5281. FAutoOperation = false;
  5282. SAFE_DESTROY(FSynchronizeProgressForm);
  5283. }
  5284. TSynchronizeParams SynchronizeParams;
  5285. SynchronizeParams.LocalDirectory = LocalDirectory;
  5286. SynchronizeParams.RemoteDirectory = RemoteDirectory;
  5287. SynchronizeParams.Mode = Mode;
  5288. SynchronizeParams.CopyParam = &CopyParam;
  5289. SynchronizeParams.Params = Params;
  5290. SynchronizeParams.Checklist = Checklist;
  5291. SynchronizeParams.CollectElapsed = (Now() - StartTime);
  5292. SynchronizeParams.StartTime = &StartTime;
  5293. SynchronizeParams.OnProcessedItem = NULL;
  5294. Result = Checklist->Count;
  5295. if (Checklist->Count > 0)
  5296. {
  5297. if (FLAGSET(Params, TTerminal::spPreviewChanges))
  5298. {
  5299. if (!DoSynchronizeChecklistDialog(
  5300. Checklist, Mode, Params, LocalDirectory, RemoteDirectory, CustomCommandMenu, DoFullSynchronize,
  5301. DoSynchronizeChecklistCalculateSize, DoSynchronizeMove, &SynchronizeParams))
  5302. {
  5303. Result = -1;
  5304. }
  5305. }
  5306. else
  5307. {
  5308. FullSynchronize(SynchronizeParams, NULL, NULL);
  5309. }
  5310. }
  5311. else
  5312. {
  5313. UnicodeString Message = MainInstructions(LoadStr(COMPARE_NO_DIFFERENCES));
  5314. MessageDialog(Message, qtInformation, qaOK, HELP_SYNCHRONIZE_NO_DIFFERENCES);
  5315. }
  5316. }
  5317. __finally
  5318. {
  5319. delete Checklist;
  5320. }
  5321. OperationComplete(StartTime);
  5322. }
  5323. else
  5324. {
  5325. Result = -1;
  5326. }
  5327. return Result;
  5328. }
  5329. //---------------------------------------------------------------------------
  5330. void __fastcall TCustomScpExplorerForm::TerminalSynchronizeDirectory(
  5331. const UnicodeString LocalDirectory, const UnicodeString RemoteDirectory,
  5332. bool & Continue, bool Collect)
  5333. {
  5334. if (Collect)
  5335. {
  5336. DebugAssert(FSynchronizeProgressForm != NULL);
  5337. if (!FSynchronizeProgressForm->Started)
  5338. {
  5339. FSynchronizeProgressForm->Start();
  5340. }
  5341. FSynchronizeProgressForm->SetData(LocalDirectory, RemoteDirectory, Continue);
  5342. }
  5343. else
  5344. {
  5345. DebugAssert(FSynchronizeProgressForm == NULL);
  5346. DebugAssert(FProgressForm != NULL);
  5347. }
  5348. }
  5349. //---------------------------------------------------------------------------
  5350. void __fastcall TCustomScpExplorerForm::StandaloneEdit(const UnicodeString & FileName)
  5351. {
  5352. UnicodeString FullFileName = AbsolutePath(FTerminal->CurrentDirectory, FileName);
  5353. TRemoteFile * File = NULL;
  5354. Terminal->ReadFile(FullFileName, File);
  5355. if (File != NULL)
  5356. {
  5357. std::unique_ptr<TRemoteFile> FileOwner(File);
  5358. TAutoFlag Flag(FStandaloneEditing);
  5359. ExecuteRemoteFile(FullFileName, File, efInternalEditor);
  5360. Application->ShowMainForm = false;
  5361. Application->Run();
  5362. }
  5363. }
  5364. //---------------------------------------------------------------------------
  5365. void __fastcall TCustomScpExplorerForm::ExploreLocalDirectory()
  5366. {
  5367. DebugFail();
  5368. }
  5369. //---------------------------------------------------------------------------
  5370. TSessionData * __fastcall TCustomScpExplorerForm::CloneCurrentSessionData()
  5371. {
  5372. std::unique_ptr<TSessionData> SessionData(new TSessionData(L""));
  5373. SessionData->Assign(Terminal->SessionData);
  5374. UpdateSessionData(SessionData.get());
  5375. Terminal->UpdateSessionCredentials(SessionData.get());
  5376. if (!Terminal->SessionData->HasSessionName())
  5377. {
  5378. // Particularly for "Workspace/XXXX" name, we need to reset it, as it would become user-visible
  5379. // once IsWorkspace is cleared
  5380. SessionData->Name = UnicodeString();
  5381. SessionData->IsWorkspace = false;
  5382. }
  5383. return SessionData.release();
  5384. }
  5385. //---------------------------------------------------------------------------
  5386. void __fastcall TCustomScpExplorerForm::SaveCurrentSession()
  5387. {
  5388. TSessionData * SessionData = CloneCurrentSessionData();
  5389. try
  5390. {
  5391. TSessionData * EditingSessionData = StoredSessions->FindSame(SessionData);
  5392. DoSaveSession(SessionData, EditingSessionData, true, NULL);
  5393. }
  5394. __finally
  5395. {
  5396. delete SessionData;
  5397. }
  5398. }
  5399. //---------------------------------------------------------------------------
  5400. TObjectList * __fastcall TCustomScpExplorerForm::DoCollectWorkspace()
  5401. {
  5402. TTerminalManager * Manager = TTerminalManager::Instance();
  5403. std::unique_ptr<TObjectList> DataList(new TObjectList());
  5404. if (DebugAlwaysTrue(Terminal != NULL))
  5405. {
  5406. // Update (Managed)Terminal->StateData
  5407. UpdateTerminal(Terminal);
  5408. }
  5409. Manager->SaveWorkspace(DataList.get());
  5410. return DataList.release();
  5411. }
  5412. //---------------------------------------------------------------------------
  5413. void __fastcall TCustomScpExplorerForm::DoSaveWorkspace(
  5414. const UnicodeString & Name, TObjectList * DataList, bool SavePasswords, bool Explicit)
  5415. {
  5416. WinConfiguration->LastWorkspace = Name;
  5417. if (!SavePasswords)
  5418. {
  5419. for (int Index = 0; Index < DataList->Count; Index++)
  5420. {
  5421. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5422. if (SessionData->Link.IsEmpty())
  5423. {
  5424. SessionData->ClearSessionPasswords();
  5425. }
  5426. }
  5427. }
  5428. StoredSessions->NewWorkspace(Name, DataList);
  5429. // modified only
  5430. StoredSessions->Save(false, Explicit);
  5431. }
  5432. //---------------------------------------------------------------------------
  5433. UnicodeString __fastcall TCustomScpExplorerForm::WorkspaceName()
  5434. {
  5435. return
  5436. DefaultStr(WinConfiguration->LastWorkspace,
  5437. DefaultStr(WinConfiguration->AutoWorkspace, LoadStr(NEW_WORKSPACE)));
  5438. }
  5439. //---------------------------------------------------------------------------
  5440. bool __fastcall TCustomScpExplorerForm::SaveWorkspace(bool EnableAutoSave)
  5441. {
  5442. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  5443. bool AnyNonStoredSessionWithPassword = false;
  5444. bool AnyNonStoredNonWorkspaceSessionWithPassword = false;
  5445. bool AllNonStoredSessionsAnonymous = true;
  5446. TTerminalManager * Manager = TTerminalManager::Instance();
  5447. for (int Index = 0; Index < DataList->Count; Index++)
  5448. {
  5449. TSessionData * SessionData = dynamic_cast<TSessionData *>(DataList->Items[Index]);
  5450. if (SessionData->Link.IsEmpty())
  5451. {
  5452. if (SessionData->HasAnySessionPassword())
  5453. {
  5454. AnyNonStoredSessionWithPassword = true;
  5455. if (!Manager->Terminals[Index]->SessionData->IsWorkspace)
  5456. {
  5457. AnyNonStoredNonWorkspaceSessionWithPassword = true;
  5458. }
  5459. }
  5460. if (!SameText(SessionData->UserName, AnonymousUserName))
  5461. {
  5462. AllNonStoredSessionsAnonymous = false;
  5463. }
  5464. }
  5465. }
  5466. bool SavePasswords;
  5467. bool * PSavePasswords;
  5468. bool NotRecommendedSavingPasswords =
  5469. !CustomWinConfiguration->UseMasterPassword &&
  5470. !AllNonStoredSessionsAnonymous;
  5471. if (Configuration->DisablePasswordStoring ||
  5472. !AnyNonStoredSessionWithPassword)
  5473. {
  5474. PSavePasswords = NULL;
  5475. SavePasswords = false;
  5476. }
  5477. else
  5478. {
  5479. PSavePasswords = &SavePasswords;
  5480. SavePasswords =
  5481. !AnyNonStoredNonWorkspaceSessionWithPassword ||
  5482. !NotRecommendedSavingPasswords;
  5483. }
  5484. UnicodeString Name = WorkspaceName();
  5485. bool CreateShortcut = false;
  5486. bool Result =
  5487. DoSaveWorkspaceDialog(
  5488. Name, PSavePasswords, NotRecommendedSavingPasswords, CreateShortcut,
  5489. EnableAutoSave);
  5490. if (Result)
  5491. {
  5492. DoSaveWorkspace(Name, DataList.get(), SavePasswords, true);
  5493. if (CreateShortcut)
  5494. {
  5495. TOperationVisualizer Visualizer;
  5496. UnicodeString AdditionalParams = TProgramParams::FormatSwitch(DESKTOP_SWITCH);
  5497. CreateDesktopSessionShortCut(Name, L"", AdditionalParams, -1, WORKSPACE_ICON);
  5498. }
  5499. if (EnableAutoSave)
  5500. {
  5501. WinConfiguration->AutoSaveWorkspace = true;
  5502. WinConfiguration->AutoWorkspace = Name;
  5503. if (PSavePasswords != NULL)
  5504. {
  5505. WinConfiguration->AutoSaveWorkspacePasswords = SavePasswords;
  5506. }
  5507. }
  5508. }
  5509. return Result;
  5510. }
  5511. //---------------------------------------------------------------------------
  5512. void __fastcall TCustomScpExplorerForm::UpdateTerminal(TTerminal * Terminal)
  5513. {
  5514. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  5515. DebugAssert(ManagedTerminal != NULL);
  5516. SAFE_DESTROY(ManagedTerminal->RemoteExplorerState);
  5517. if (WinConfiguration->PreservePanelState)
  5518. {
  5519. ManagedTerminal->RemoteExplorerState = RemoteDirView->SaveState();
  5520. }
  5521. UpdateSessionData(ManagedTerminal->StateData);
  5522. }
  5523. //---------------------------------------------------------------------------
  5524. void __fastcall TCustomScpExplorerForm::UpdateSessionData(TSessionData * Data)
  5525. {
  5526. // Keep in sync with TSessionData::CopyStateData
  5527. DebugAssert(Data != NULL);
  5528. // This is inconsistent with how color (for example) is handled.
  5529. Data->Name = Terminal->SessionData->Name;
  5530. // cannot use RemoteDirView->Path, because it is empty if connection
  5531. // was already closed
  5532. // also only peek, we may not be connected at all atm
  5533. // so make sure we do not try retrieving current directory from the server
  5534. // (particularly with FTP)
  5535. UnicodeString ACurrentDirectory = Terminal->PeekCurrentDirectory();
  5536. if (!ACurrentDirectory.IsEmpty())
  5537. {
  5538. Data->RemoteDirectory = ACurrentDirectory;
  5539. }
  5540. Data->Color = SessionColor;
  5541. }
  5542. //---------------------------------------------------------------------------
  5543. void __fastcall TCustomScpExplorerForm::ToolBarResize(TObject *Sender)
  5544. {
  5545. TTBXToolbar * Toolbar = dynamic_cast<TTBXToolbar*>(Sender);
  5546. DebugAssert(Toolbar != NULL);
  5547. for (int i = 0; i < Toolbar->Items->Count; i++)
  5548. {
  5549. TTBXCustomDropDownItem * DropDownItem;
  5550. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5551. if (DropDownItem != NULL)
  5552. {
  5553. ToolbarItemResize(DropDownItem,
  5554. Toolbar->Width - (Toolbar->View->BaseSize.x - DropDownItem->EditWidth) -
  5555. Toolbar->NonClientWidth);
  5556. break;
  5557. }
  5558. }
  5559. }
  5560. //---------------------------------------------------------------------------
  5561. void __fastcall TCustomScpExplorerForm::ToolbarItemResize(TTBXCustomDropDownItem * Item, int Width)
  5562. {
  5563. Item->EditWidth = Width;
  5564. }
  5565. //---------------------------------------------------------------------------
  5566. void __fastcall TCustomScpExplorerForm::ToolbarGetBaseSize(
  5567. TTBCustomToolbar * Toolbar, TPoint & ASize)
  5568. {
  5569. for (int i = 0; i < Toolbar->Items->Count; i++)
  5570. {
  5571. TTBXCustomDropDownItem * DropDownItem;
  5572. DropDownItem = dynamic_cast<TTBXCustomDropDownItem *>(Toolbar->Items->Items[i]);
  5573. if (DropDownItem != NULL)
  5574. {
  5575. ASize.x -= DropDownItem->EditWidth;
  5576. ASize.x += ScaleByTextHeight(this, 50) /* minimal combo width */;
  5577. }
  5578. }
  5579. }
  5580. //---------------------------------------------------------------------------
  5581. void __fastcall TCustomScpExplorerForm::DoWarnLackOfTempSpace(
  5582. const UnicodeString Path, __int64 RequiredSpace, bool & Continue)
  5583. {
  5584. if (WinConfiguration->DDWarnLackOfTempSpace)
  5585. {
  5586. UnicodeString ADrive = ExtractFileDrive(ExpandFileName(Path));
  5587. if (!ADrive.IsEmpty())
  5588. {
  5589. __int64 FreeSpace = DiskFree((Byte)(ADrive[1]-'A'+1));
  5590. DebugAssert(RequiredSpace >= 0);
  5591. __int64 RequiredWithReserve;
  5592. RequiredWithReserve = (__int64)(RequiredSpace * WinConfiguration->DDWarnLackOfTempSpaceRatio);
  5593. if (FreeSpace < RequiredWithReserve)
  5594. {
  5595. unsigned int Result;
  5596. TMessageParams Params(mpNeverAskAgainCheck);
  5597. Result = MessageDialog(FMTLOAD(DD_WARN_LACK_OF_TEMP_SPACE, (Path,
  5598. FormatBytes(FreeSpace), FormatBytes(RequiredSpace))),
  5599. qtWarning, qaYes | qaNo, HELP_DD_WARN_LACK_OF_TEMP_SPACE, &Params);
  5600. if (Result == qaNeverAskAgain)
  5601. {
  5602. WinConfiguration->DDWarnLackOfTempSpace = false;
  5603. }
  5604. Continue = (Result == qaYes || Result == qaNeverAskAgain);
  5605. }
  5606. }
  5607. }
  5608. }
  5609. //---------------------------------------------------------------------------
  5610. void __fastcall TCustomScpExplorerForm::AddBookmark(TOperationSide Side)
  5611. {
  5612. DoOpenDirectoryDialog(odAddBookmark, Side);
  5613. }
  5614. //---------------------------------------------------------------------------
  5615. TStrings * __fastcall TCustomScpExplorerForm::CreateVisitedDirectories(TOperationSide Side)
  5616. {
  5617. // we should better use TCustomDirView::FCaseSensitive, but it is private
  5618. TStringList * VisitedDirectories = CreateSortedStringList((Side == osRemote));
  5619. try
  5620. {
  5621. TCustomDirView * DView = DirView(Side);
  5622. for (int Index = -DView->BackCount; Index <= DView->ForwardCount; Index++)
  5623. {
  5624. VisitedDirectories->Add(DView->HistoryPath[Index]);
  5625. }
  5626. }
  5627. catch (...)
  5628. {
  5629. delete VisitedDirectories;
  5630. throw;
  5631. }
  5632. return VisitedDirectories;
  5633. }
  5634. //---------------------------------------------------------------------------
  5635. void __fastcall TCustomScpExplorerForm::DoOpenDirectoryDialog(
  5636. TOpenDirectoryMode Mode, TOperationSide Side)
  5637. {
  5638. bool Continue = true;
  5639. if (Mode == odAddBookmark)
  5640. {
  5641. TMessageParams Params(mpNeverAskAgainCheck);
  5642. Params.NeverAskAgainTitle = LoadStr(ADD_BOOKMARK_SHARED);
  5643. Params.NeverAskAgainCheckedInitially = WinConfiguration->UseSharedBookmarks;
  5644. UnicodeString Message = MainInstructions(FMTLOAD(ADD_BOOKMARK_CONFIRM, (DirView(Side)->PathName)));
  5645. unsigned int Answer =
  5646. MessageDialog(Message,
  5647. qtConfirmation, qaOK | qaCancel, HELP_ADD_BOOKMARK_CONFIRM, &Params);
  5648. if (Answer == qaNeverAskAgain)
  5649. {
  5650. Continue = true;
  5651. WinConfiguration->UseSharedBookmarks = true;
  5652. }
  5653. else if (Answer == qaOK)
  5654. {
  5655. Continue = true;
  5656. WinConfiguration->UseSharedBookmarks = false;
  5657. }
  5658. else
  5659. {
  5660. Continue = false;
  5661. }
  5662. }
  5663. if (Continue)
  5664. {
  5665. TStrings * VisitedDirectories = CreateVisitedDirectories(Side);
  5666. try
  5667. {
  5668. UnicodeString Name = DirView(Side)->PathName;
  5669. bool Repeat;
  5670. do
  5671. {
  5672. Repeat = false;
  5673. if (::DoOpenDirectoryDialog(Mode, Side, Name, VisitedDirectories, Terminal,
  5674. // do not allow switching to location profiles,
  5675. // if we are not connected
  5676. HasDirView[osLocal] && (Terminal != NULL)))
  5677. {
  5678. TWindowLock Lock(this);
  5679. if (!TryOpenDirectory(Side, Name))
  5680. {
  5681. Repeat = true;
  5682. Mode = odBrowse;
  5683. }
  5684. }
  5685. }
  5686. while (Repeat);
  5687. }
  5688. __finally
  5689. {
  5690. delete VisitedDirectories;
  5691. }
  5692. }
  5693. }
  5694. //---------------------------------------------------------------------------
  5695. bool __fastcall TCustomScpExplorerForm::CommandSessionFallback()
  5696. {
  5697. DebugAssert(!FTerminal->CommandSessionOpened);
  5698. return TTerminalManager::Instance()->ConnectTerminal(FTerminal->CommandSession);
  5699. }
  5700. //---------------------------------------------------------------------------
  5701. bool __fastcall TCustomScpExplorerForm::EnsureCommandSessionFallback(TFSCapability Capability)
  5702. {
  5703. bool Result = FTerminal->IsCapable[Capability] ||
  5704. FTerminal->CommandSessionOpened;
  5705. if (!Result)
  5706. {
  5707. DebugAssert(FTerminal->IsCapable[fcSecondaryShell]);
  5708. if (!GUIConfiguration->ConfirmCommandSession)
  5709. {
  5710. Result = true;
  5711. }
  5712. else
  5713. {
  5714. TMessageParams Params(mpNeverAskAgainCheck);
  5715. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo();
  5716. unsigned int Answer = MessageDialog(FMTLOAD(PERFORM_ON_COMMAND_SESSION2,
  5717. (FileSystemInfo.ProtocolName, FileSystemInfo.ProtocolName)), qtConfirmation,
  5718. qaOK | qaCancel, HELP_PERFORM_ON_COMMAND_SESSION, &Params);
  5719. if (Answer == qaNeverAskAgain)
  5720. {
  5721. GUIConfiguration->ConfirmCommandSession = false;
  5722. Result = true;
  5723. }
  5724. else if (Answer == qaOK)
  5725. {
  5726. Result = true;
  5727. }
  5728. }
  5729. if (Result)
  5730. {
  5731. Result = CommandSessionFallback();
  5732. }
  5733. }
  5734. return Result;
  5735. }
  5736. //---------------------------------------------------------------------------
  5737. void __fastcall TCustomScpExplorerForm::OpenConsole(UnicodeString Command)
  5738. {
  5739. if (EnsureCommandSessionFallback(fcAnyCommand))
  5740. {
  5741. DoConsoleDialog(Terminal, Command);
  5742. }
  5743. }
  5744. //---------------------------------------------------------------------------
  5745. void __fastcall TCustomScpExplorerForm::FileControlDDDragEnter(
  5746. TObject *Sender, _di_IDataObject /*DataObj*/, int /*grfKeyState*/,
  5747. const TPoint & /*Point*/, int & /*dwEffect*/, bool & Accept)
  5748. {
  5749. if (IsFileControl(DropSourceControl, osRemote) &&
  5750. (FDDExtMapFile != NULL))
  5751. {
  5752. Accept = true;
  5753. }
  5754. FDDTargetControl = dynamic_cast<TControl*>(Sender);
  5755. DebugAssert(FDDTargetControl != NULL);
  5756. }
  5757. //---------------------------------------------------------------------------
  5758. void __fastcall TCustomScpExplorerForm::SessionsDDDragEnter(
  5759. _di_IDataObject DataObj, int KeyState,
  5760. const TPoint & Point, int & Effect, bool & Accept)
  5761. {
  5762. FileControlDDDragEnter(SessionsPageControl, DataObj, KeyState, Point, Effect, Accept);
  5763. }
  5764. //---------------------------------------------------------------------------
  5765. void __fastcall TCustomScpExplorerForm::QueueDDDragEnter(
  5766. _di_IDataObject DataObj, int KeyState,
  5767. const TPoint & Point, int & Effect, bool & Accept)
  5768. {
  5769. FileControlDDDragEnter(QueueView3, DataObj, KeyState, Point, Effect, Accept);
  5770. }
  5771. //---------------------------------------------------------------------------
  5772. void __fastcall TCustomScpExplorerForm::FileControlDDDragLeave(
  5773. TObject * Sender)
  5774. {
  5775. DebugUsedParam(Sender);
  5776. DebugAssert(FDDTargetControl == Sender);
  5777. FDDTargetControl = NULL;
  5778. }
  5779. //---------------------------------------------------------------------------
  5780. void __fastcall TCustomScpExplorerForm::SessionsDDDragLeave()
  5781. {
  5782. FileControlDDDragLeave(SessionsPageControl);
  5783. }
  5784. //---------------------------------------------------------------------------
  5785. void __fastcall TCustomScpExplorerForm::QueueDDDragLeave()
  5786. {
  5787. FileControlDDDragLeave(QueueView3);
  5788. }
  5789. //---------------------------------------------------------------------------
  5790. void __fastcall TCustomScpExplorerForm::AddEditLink(TOperationSide Side, bool Add)
  5791. {
  5792. DebugAssert(GetSide(Side) == osRemote);
  5793. DebugUsedParam(Side);
  5794. bool Edit = false;
  5795. TRemoteFile * File = NULL;
  5796. UnicodeString FileName;
  5797. UnicodeString PointTo;
  5798. bool SymbolicLink = true;
  5799. if (RemoteDirView->ItemFocused)
  5800. {
  5801. DebugAssert(RemoteDirView->ItemFocused->Data);
  5802. File = (TRemoteFile *)RemoteDirView->ItemFocused->Data;
  5803. Edit = !Add && File->IsSymLink && Terminal->SessionData->ResolveSymlinks;
  5804. if (Edit)
  5805. {
  5806. FileName = File->FileName;
  5807. PointTo = File->LinkTo;
  5808. }
  5809. else
  5810. {
  5811. PointTo = File->FileName;
  5812. }
  5813. }
  5814. if (DoSymlinkDialog(FileName, PointTo, osRemote, SymbolicLink, Edit,
  5815. Terminal->IsCapable[fcHardLink]))
  5816. {
  5817. Configuration->Usage->Inc(L"RemoteLinksCreated");
  5818. if (Edit)
  5819. {
  5820. DebugAssert(File->FileName == FileName);
  5821. int Params = dfNoRecursive;
  5822. Terminal->ExceptionOnFail = true;
  5823. try
  5824. {
  5825. Terminal->DeleteFile(L"", File, &Params);
  5826. }
  5827. __finally
  5828. {
  5829. Terminal->ExceptionOnFail = false;
  5830. }
  5831. }
  5832. Terminal->CreateLink(FileName, PointTo, SymbolicLink);
  5833. }
  5834. }
  5835. //---------------------------------------------------------------------------
  5836. bool __fastcall TCustomScpExplorerForm::CanAddEditLink(TOperationSide Side)
  5837. {
  5838. return
  5839. (Terminal != NULL) &&
  5840. ((GetSide(Side) != osRemote) ||
  5841. (Terminal->ResolvingSymlinks &&
  5842. Terminal->IsCapable[fcSymbolicLink]));
  5843. }
  5844. //---------------------------------------------------------------------------
  5845. bool __fastcall TCustomScpExplorerForm::LinkFocused()
  5846. {
  5847. return
  5848. (FCurrentSide == osRemote) &&
  5849. (RemoteDirView->ItemFocused != NULL) &&
  5850. ((TRemoteFile *)RemoteDirView->ItemFocused->Data)->IsSymLink &&
  5851. Terminal->SessionData->ResolveSymlinks;
  5852. }
  5853. //---------------------------------------------------------------------------
  5854. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFile()
  5855. {
  5856. DebugAssert(!WinConfiguration->DisableOpenEdit);
  5857. FForceExecution = true;
  5858. try
  5859. {
  5860. DirView(osCurrent)->ExecuteCurrentFile();
  5861. }
  5862. __finally
  5863. {
  5864. FForceExecution = false;
  5865. }
  5866. }
  5867. //---------------------------------------------------------------------------
  5868. void __fastcall TCustomScpExplorerForm::ExecuteCurrentFileWith(bool OnFocused)
  5869. {
  5870. TEditorData ExternalEditor;
  5871. ExternalEditor.Editor = edExternal;
  5872. bool Remember = false;
  5873. if (DoEditorPreferencesDialog(&ExternalEditor, Remember, epmAdHoc,
  5874. (GetSide(osCurrent) == osRemote)))
  5875. {
  5876. if (Remember)
  5877. {
  5878. TEditorList * EditorList = new TEditorList;
  5879. try
  5880. {
  5881. *EditorList = *WinConfiguration->EditorList;
  5882. bool Found = false;
  5883. int i = 0;
  5884. while (!Found && (i < EditorList->Count))
  5885. {
  5886. const TEditorPreferences * Editor = EditorList->Editors[i];
  5887. if ((Editor->Data->Editor == edExternal) &&
  5888. (Editor->Data->ExternalEditor == ExternalEditor.ExternalEditor))
  5889. {
  5890. Found = true;
  5891. }
  5892. i++;
  5893. }
  5894. if (!Found)
  5895. {
  5896. EditorList->Add(new TEditorPreferences(ExternalEditor));
  5897. WinConfiguration->EditorList = EditorList;
  5898. }
  5899. }
  5900. __finally
  5901. {
  5902. delete EditorList;
  5903. }
  5904. }
  5905. ExecuteFile(osCurrent, efExternalEditor, &ExternalEditor, true, OnFocused);
  5906. }
  5907. }
  5908. //---------------------------------------------------------------------------
  5909. void __fastcall TCustomScpExplorerForm::TerminalRemoved(TObject * Sender)
  5910. {
  5911. FEditorManager->ProcessFiles(FileTerminalRemoved, Sender);
  5912. if (FFileFindTerminal == Sender)
  5913. {
  5914. FFileFindTerminal = NULL;
  5915. HideFileFindDialog();
  5916. }
  5917. if (FClipboardTerminal == Sender)
  5918. {
  5919. ClipboardClear(); // implies ClipboardStop
  5920. }
  5921. }
  5922. //---------------------------------------------------------------------------
  5923. void __fastcall TCustomScpExplorerForm::FileTerminalRemoved(const UnicodeString FileName,
  5924. TEditedFileData * Data, TObject * /*Token*/, void * Arg)
  5925. {
  5926. TTerminal * Terminal = static_cast<TTerminal *>(Arg);
  5927. DebugAssert(Terminal != NULL);
  5928. if (Data->Terminal == Terminal)
  5929. {
  5930. Data->Terminal = NULL;
  5931. }
  5932. }
  5933. //---------------------------------------------------------------------------
  5934. void __fastcall TCustomScpExplorerForm::LastTerminalClosed(TObject * /*Sender*/)
  5935. {
  5936. UpdateControls();
  5937. SessionColor = TColor(0);
  5938. UpdateRemotePathComboBox(false);
  5939. NeedSession(true);
  5940. }
  5941. //---------------------------------------------------------------------------
  5942. void __fastcall TCustomScpExplorerForm::NeedSession(bool ReloadSessions)
  5943. {
  5944. try
  5945. {
  5946. try
  5947. {
  5948. TTerminalManager::Instance()->NewSession(false, L"", ReloadSessions, this);
  5949. }
  5950. __finally
  5951. {
  5952. if (!WinConfiguration->KeepOpenWhenNoSession &&
  5953. (!Terminal || !Terminal->Active))
  5954. {
  5955. TerminateApplication();
  5956. }
  5957. }
  5958. }
  5959. catch (EAbort &)
  5960. {
  5961. // swallow
  5962. // The TTerminalManager does not expect the OnLastTerminalClose to throw without trying to connect.
  5963. // Also when called from TWinControl.UpdateShowing => CMShowingChanged, the showing is aborted on exception
  5964. }
  5965. }
  5966. //---------------------------------------------------------------------------
  5967. void __fastcall TCustomScpExplorerForm::DoTerminalListChanged()
  5968. {
  5969. TStrings * TerminalList = TTerminalManager::Instance()->TerminalList;
  5970. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  5971. Configuration->Usage->SetMax(L"MaxOpenedSessions", TerminalList->Count);
  5972. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 0, 0);
  5973. try
  5974. {
  5975. while ((SessionsPageControl->PageCount > TerminalList->Count + 1) ||
  5976. // Clear the design time unthemed tab
  5977. ((SessionsPageControl->PageCount > 0) &&
  5978. (dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[SessionsPageControl->PageCount - 1]) == NULL)))
  5979. {
  5980. delete SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  5981. }
  5982. for (int Index = 0; Index <= TerminalList->Count; Index++)
  5983. {
  5984. TThemeTabSheet * TabSheet;
  5985. if (Index >= SessionsPageControl->PageCount)
  5986. {
  5987. TabSheet = new TThemeTabSheet(SessionsPageControl);
  5988. TabSheet->PageControl = SessionsPageControl;
  5989. }
  5990. else
  5991. {
  5992. TabSheet = DebugNotNull(dynamic_cast<TThemeTabSheet *>(SessionsPageControl->Pages[Index]));
  5993. }
  5994. bool IsSessionTab = (Index < TerminalList->Count);
  5995. if (IsSessionTab)
  5996. {
  5997. TTerminal * Terminal = dynamic_cast<TTerminal *>(TerminalList->Objects[Index]);
  5998. TabSheet->Tag = reinterpret_cast<int>(Terminal);
  5999. UpdateSessionTab(TabSheet);
  6000. }
  6001. else
  6002. {
  6003. TabSheet->ImageIndex = FNewSessionTabImageIndex;
  6004. TabSheet->Tag = 0; // not really needed
  6005. // 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)
  6006. UpdateNewSessionTab();
  6007. }
  6008. TabSheet->ShowCloseButton = IsSessionTab;
  6009. }
  6010. }
  6011. __finally
  6012. {
  6013. SendMessage(SessionsPageControl->Handle, WM_SETREDRAW, 1, 0);
  6014. }
  6015. SessionsPageControl->ActivePageIndex = ActiveTerminalIndex;
  6016. }
  6017. //---------------------------------------------------------------------------
  6018. void __fastcall TCustomScpExplorerForm::TerminalListChanged(TObject * /*Sender*/)
  6019. {
  6020. DoTerminalListChanged();
  6021. }
  6022. //---------------------------------------------------------------------------
  6023. void __fastcall TCustomScpExplorerForm::UpdateNewSessionTab()
  6024. {
  6025. TTabSheet * TabSheet = SessionsPageControl->Pages[SessionsPageControl->PageCount - 1];
  6026. DebugAssert(TabSheet->ImageIndex == 0);
  6027. TabSheet->Caption =
  6028. WinConfiguration->SelectiveToolbarText ?
  6029. StripHotkey(StripTrailingPunctuation(NonVisualDataModule->NewSessionAction->Caption)) :
  6030. UnicodeString();
  6031. }
  6032. //---------------------------------------------------------------------------
  6033. TTerminal * __fastcall TCustomScpExplorerForm::GetSessionTabTerminal(TTabSheet * TabSheet)
  6034. {
  6035. return reinterpret_cast<TTerminal *>(TabSheet->Tag);
  6036. }
  6037. //---------------------------------------------------------------------------
  6038. void __fastcall TCustomScpExplorerForm::UpdateSessionTab(TTabSheet * TabSheet)
  6039. {
  6040. if (DebugAlwaysTrue(TabSheet != NULL))
  6041. {
  6042. TManagedTerminal * ManagedTerminal =
  6043. dynamic_cast<TManagedTerminal *>(GetSessionTabTerminal(TabSheet));
  6044. if (DebugAlwaysTrue(ManagedTerminal != NULL))
  6045. {
  6046. TColor Color = (ManagedTerminal == FTerminal) ? FSessionColor : ManagedTerminal->StateData->Color;
  6047. TabSheet->ImageIndex = AddSessionColor(Color);
  6048. UnicodeString TabCaption = TTerminalManager::Instance()->GetTerminalTitle(ManagedTerminal, true);
  6049. TabSheet->Caption = SessionsPageControl->FormatCaptionWithCloseButton(TabCaption);
  6050. TThemeTabSheet * ThemeTabSheet = dynamic_cast<TThemeTabSheet *>(TabSheet);
  6051. if (DebugAlwaysTrue(ThemeTabSheet != NULL))
  6052. {
  6053. ThemeTabSheet->Shadowed = !ManagedTerminal->Active;
  6054. }
  6055. }
  6056. }
  6057. }
  6058. //---------------------------------------------------------------------------
  6059. bool __fastcall TCustomScpExplorerForm::SessionTabSwitched()
  6060. {
  6061. DebugAssert(SessionsPageControl->ActivePage != NULL);
  6062. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  6063. bool Result = (Terminal != NULL);
  6064. if (Result)
  6065. {
  6066. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6067. }
  6068. else
  6069. {
  6070. try
  6071. {
  6072. NewSession(false);
  6073. }
  6074. __finally
  6075. {
  6076. DoTerminalListChanged();
  6077. }
  6078. FSessionsPageControlNewSessionTime = Now();
  6079. }
  6080. return Result;
  6081. }
  6082. //---------------------------------------------------------------------------
  6083. void __fastcall TCustomScpExplorerForm::SessionsPageControlChange(TObject * /*Sender*/)
  6084. {
  6085. SessionTabSwitched();
  6086. }
  6087. //---------------------------------------------------------------------------
  6088. void __fastcall TCustomScpExplorerForm::TransferListChange(TObject * Sender)
  6089. {
  6090. TTBXStringList * TransferList = dynamic_cast<TTBXStringList *>(Sender);
  6091. DebugAssert(TransferList != NULL);
  6092. UnicodeString Name;
  6093. if (TransferList->ItemIndex <= 0)
  6094. {
  6095. Name = L"";
  6096. }
  6097. else
  6098. {
  6099. Name = GUIConfiguration->CopyParamList->Names[TransferList->ItemIndex - 1];
  6100. }
  6101. if (FCopyParamAutoSelected.IsEmpty())
  6102. {
  6103. // if previous preset was not autoselected, make new preset the "default"
  6104. FCopyParamDefault = Name;
  6105. }
  6106. GUIConfiguration->CopyParamCurrent = Name;
  6107. }
  6108. //---------------------------------------------------------------------------
  6109. void __fastcall TCustomScpExplorerForm::UpdateTransferLabel()
  6110. {
  6111. // sanity check
  6112. bool ExistingPreset =
  6113. (FTransferListHoverIndex >= 0) &&
  6114. (FTransferListHoverIndex < 1 + GUIConfiguration->CopyParamList->Count);
  6115. DebugAssert(ExistingPreset);
  6116. if (ExistingPreset)
  6117. {
  6118. HDC DC = GetDC(0);
  6119. TCanvas * Canvas = new TCanvas();
  6120. try
  6121. {
  6122. Canvas->Handle = DC;
  6123. Canvas->Font = GetToolbarFont(this);
  6124. UnicodeString Name;
  6125. if (FTransferListHoverIndex == 0)
  6126. {
  6127. Name = L"";
  6128. }
  6129. else
  6130. {
  6131. Name = GUIConfiguration->CopyParamList->Names[FTransferListHoverIndex - 1];
  6132. }
  6133. TTBXLabelItem * TransferLabel = dynamic_cast<TTBXLabelItem*>(
  6134. static_cast<TComponent*>(GetComponent(fcTransferLabel)));
  6135. TTBXStringList * TransferList = dynamic_cast<TTBXStringList*>(
  6136. static_cast<TObject*>(GetComponent(fcTransferList)));
  6137. UnicodeString InfoStr =
  6138. GUIConfiguration->CopyParamPreset[Name].
  6139. GetInfoStr(L"; ",
  6140. FLAGMASK(((Terminal != NULL) && Terminal->Active), Terminal->UsableCopyParamAttrs(0).General));
  6141. int MaxWidth = TransferList->MinWidth - (2 * TransferLabel->Margin) - ScaleByTextHeight(this, 10);
  6142. if (Canvas->TextExtent(InfoStr).cx > MaxWidth)
  6143. {
  6144. while (Canvas->TextExtent(InfoStr + Ellipsis).cx > MaxWidth)
  6145. {
  6146. InfoStr.SetLength(InfoStr.Length() - 1);
  6147. }
  6148. InfoStr += Ellipsis;
  6149. }
  6150. // UpdateCaption does not cause invalidation of whole submenu, while
  6151. // setting Caption property does.
  6152. // also it probably does not resize the label, even if necessary
  6153. // (we do not want that anyway)
  6154. TransferLabel->UpdateCaption(InfoStr);
  6155. }
  6156. __finally
  6157. {
  6158. Canvas->Handle = NULL;
  6159. ReleaseDC(0, DC);
  6160. delete Canvas;
  6161. }
  6162. }
  6163. }
  6164. //---------------------------------------------------------------------------
  6165. void __fastcall TCustomScpExplorerForm::TransferListDrawItem(
  6166. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, const TRect & /*ARect*/,
  6167. int /*AIndex*/, int AHoverIndex, bool & /*DrawDefault*/)
  6168. {
  6169. if (FTransferListHoverIndex != AHoverIndex)
  6170. {
  6171. FTransferListHoverIndex = AHoverIndex;
  6172. UpdateTransferLabel();
  6173. }
  6174. }
  6175. //---------------------------------------------------------------------------
  6176. void __fastcall TCustomScpExplorerForm::WMAppCommand(TMessage & Message)
  6177. {
  6178. int Command = GET_APPCOMMAND_LPARAM(Message.LParam);
  6179. TShiftState Shift = KeyDataToShiftState(GET_KEYSTATE_LPARAM(Message.LParam));
  6180. if ((Shift * (TShiftState() << ssShift << ssAlt << ssCtrl)).Empty())
  6181. {
  6182. if (Command == APPCOMMAND_BROWSER_FAVORITES)
  6183. {
  6184. OpenDirectory(GetSide(osCurrent));
  6185. Message.Result = 1;
  6186. }
  6187. else
  6188. {
  6189. TForm::Dispatch(&Message);
  6190. }
  6191. }
  6192. else
  6193. {
  6194. TForm::Dispatch(&Message);
  6195. }
  6196. }
  6197. //---------------------------------------------------------------------------
  6198. void __fastcall TCustomScpExplorerForm::CMDialogChar(TMessage & AMessage)
  6199. {
  6200. TCMDialogChar & Message = reinterpret_cast<TCMDialogChar &>(AMessage);
  6201. if ((FIgnoreNextDialogChar != 0) &&
  6202. (toupper(Message.CharCode) == toupper(FIgnoreNextDialogChar)))
  6203. {
  6204. Message.Result = 1;
  6205. }
  6206. else
  6207. {
  6208. TForm::Dispatch(&Message);
  6209. }
  6210. FIgnoreNextDialogChar = 0;
  6211. }
  6212. //---------------------------------------------------------------------------
  6213. void __fastcall TCustomScpExplorerForm::WMSysCommand(TMessage & Message)
  6214. {
  6215. // The four low-order bits are used internally by Windows
  6216. unsigned int Cmd = (Message.WParam & 0xFFF0);
  6217. // SC_RESTORE, SC_MAXIMIZE, SC_MINIMIZE - buttons on windows title
  6218. // SC_DEFAULT - double click on windows title (does not work, at least on WinXP)
  6219. // 61730 - restore thru double click - undocumented
  6220. // 61490 - maximize thru double click - undocumented
  6221. if ((Cmd == SC_RESTORE) || (Cmd == SC_MAXIMIZE) ||
  6222. (Cmd == SC_MINIMIZE) || (Cmd == SC_DEFAULT))
  6223. {
  6224. SysResizing(Cmd);
  6225. }
  6226. TForm::Dispatch(&Message);
  6227. }
  6228. //---------------------------------------------------------------------------
  6229. void __fastcall TCustomScpExplorerForm::WMQueryEndSession(TMessage & Message)
  6230. {
  6231. // We were actually never able to make ENDSESSION_CRITICAL happen.
  6232. // Also there no point returning TRUE as we are not able to
  6233. // handle the abrupt termination caused by subsequent WM_ENDSESSION cleanly.
  6234. // Hence the process termination might be safer :)
  6235. if ((Message.LParam != ENDSESSION_CRITICAL) &&
  6236. (((FQueue != NULL) && !FQueue->IsEmpty) || (FProgressForm != NULL)))
  6237. {
  6238. Message.Result = FALSE;
  6239. }
  6240. else
  6241. {
  6242. Message.Result = TRUE;
  6243. }
  6244. // Do not call default handling as that triggers OnCloseQuery,
  6245. // where our implementation will popup confirmation dialogs, what we do not want,
  6246. // as per Vista guidelines:
  6247. // https://docs.microsoft.com/en-us/windows/desktop/shutdown/shutting-down
  6248. }
  6249. //---------------------------------------------------------------------------
  6250. void __fastcall TCustomScpExplorerForm::WMEndSession(TWMEndSession & Message)
  6251. {
  6252. if (Message.EndSession && IsApplicationMinimized())
  6253. {
  6254. // WORKAROUND
  6255. // TApplication.WndProc() calls Application.Terminate() before Halt(),
  6256. // when it receives WM_ENDSESSION,
  6257. // but that sometimes (particularly when application is minimized) cause crashes.
  6258. // Crash popups message that blocks log off.
  6259. // Obviously application cannot shutdown cleanly after WM_ENDSESSION,
  6260. // so we call ExitProcess() immediately, not even trying to cleanup.
  6261. // It still causes beep, so there's likely some popup, but it does not block
  6262. // log off.
  6263. ExitProcess(0);
  6264. }
  6265. TForm::Dispatch(&Message);
  6266. }
  6267. #ifdef _DEBUG
  6268. //---------------------------------------------------------------------------
  6269. void __fastcall TCustomScpExplorerForm::WMWindowPosChanged(TWMWindowPosMsg & Message)
  6270. {
  6271. TForm::Dispatch(&Message);
  6272. }
  6273. #endif
  6274. //---------------------------------------------------------------------------
  6275. void __fastcall TCustomScpExplorerForm::SysResizing(unsigned int /*Cmd*/)
  6276. {
  6277. }
  6278. //---------------------------------------------------------------------------
  6279. void __fastcall TCustomScpExplorerForm::DoShow()
  6280. {
  6281. // only now are the controls resized finally, so the size constraints
  6282. // will not conflict with possibly very small window size
  6283. RestoreFormParams();
  6284. FixControlsPlacement();
  6285. if (Position == poDefaultPosOnly)
  6286. {
  6287. CutFormToDesktop(this);
  6288. }
  6289. TForm::DoShow();
  6290. FSessionsDragDropFilesEx->DragDropControl = SessionsPageControl;
  6291. FQueueDragDropFilesEx->DragDropControl = QueueView3;
  6292. if (Terminal == NULL)
  6293. {
  6294. StartingDisconnected();
  6295. }
  6296. UpdatePixelsPerInchMainWindowCounter();
  6297. FShowing = true;
  6298. }
  6299. //---------------------------------------------------------------------------
  6300. void __fastcall TCustomScpExplorerForm::UpdatePixelsPerInchMainWindowCounter()
  6301. {
  6302. Configuration->Usage->Set(L"PixelsPerInchMainWindow", PixelsPerInch);
  6303. }
  6304. //---------------------------------------------------------------------------
  6305. void __fastcall TCustomScpExplorerForm::StartingDisconnected()
  6306. {
  6307. DoTerminalListChanged();
  6308. }
  6309. //---------------------------------------------------------------------------
  6310. void __fastcall TCustomScpExplorerForm::PopupTrayBalloon(TTerminal * Terminal,
  6311. const UnicodeString & Str, TQueryType Type, Exception * E, unsigned int Seconds,
  6312. TNotifyEvent OnBalloonClick, TObject * UserData)
  6313. {
  6314. bool Do;
  6315. UnicodeString Message;
  6316. if (E == NULL)
  6317. {
  6318. Message = Str;
  6319. Do = true;
  6320. }
  6321. else
  6322. {
  6323. Do = ExceptionMessage(E, Message);
  6324. }
  6325. if (Do && WinConfiguration->BalloonNotifications)
  6326. {
  6327. UnicodeString Title;
  6328. if ((Terminal == NULL) && (Type == qtInformation) && ExtractMainInstructions(Message, Title))
  6329. {
  6330. Message = TrimLeft(Message);
  6331. }
  6332. else
  6333. {
  6334. Message = UnformatMessage(Message);
  6335. const ResourceString * Captions[] = { &_SMsgDlgConfirm, &_SMsgDlgWarning,
  6336. &_SMsgDlgError, &_SMsgDlgInformation, NULL };
  6337. Title = LoadResourceString(Captions[Type]);
  6338. if (Terminal != NULL)
  6339. {
  6340. Title = FORMAT(L"%s - %s",
  6341. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), Title));
  6342. }
  6343. }
  6344. if (Seconds == 0)
  6345. {
  6346. Seconds = WinConfiguration->NotificationsTimeout;
  6347. }
  6348. FTrayIcon->PopupBalloon(Title, Message, Type, Seconds * MSecsPerSec, OnBalloonClick, UserData);
  6349. }
  6350. }
  6351. //---------------------------------------------------------------------------
  6352. unsigned int __fastcall TCustomScpExplorerForm::MoreMessageDialog(const UnicodeString Message,
  6353. TStrings * MoreMessages, TQueryType Type, unsigned int Answers,
  6354. UnicodeString HelpKeyword, const TMessageParams * Params,
  6355. TTerminal * Terminal)
  6356. {
  6357. if (((WinConfiguration->ContinueOnError && (FErrorList != NULL)) ||
  6358. (FOnFeedSynchronizeError != NULL)) &&
  6359. (Params != NULL) && (Params->Params & mpAllowContinueOnError) )
  6360. {
  6361. if (FOnFeedSynchronizeError != NULL)
  6362. {
  6363. FOnFeedSynchronizeError(Message, MoreMessages, Type, HelpKeyword);
  6364. }
  6365. else
  6366. {
  6367. DebugAssert(FErrorList != NULL);
  6368. TStringList * MoreMessagesCopy = NULL;
  6369. if (MoreMessages)
  6370. {
  6371. MoreMessagesCopy = new TStringList();
  6372. MoreMessagesCopy->Assign(MoreMessages);
  6373. }
  6374. FErrorList->AddObject(Message, MoreMessagesCopy);
  6375. }
  6376. PopupTrayBalloon(Terminal, Message, Type);
  6377. return ContinueAnswer(Answers);
  6378. }
  6379. else
  6380. {
  6381. bool UseBalloon = FTrayIcon->Visible;
  6382. if (UseBalloon)
  6383. {
  6384. PopupTrayBalloon(Terminal, Message, Type);
  6385. }
  6386. unsigned int Result;
  6387. try
  6388. {
  6389. Result = ::MoreMessageDialog(Message, MoreMessages, Type, Answers, HelpKeyword, Params);
  6390. }
  6391. __finally
  6392. {
  6393. // cancel only balloon we popped up, otherwise we may cancel notification
  6394. // balloon that was there before the message dialog
  6395. // (such as "dd confirmation opt in balloon)
  6396. if (UseBalloon)
  6397. {
  6398. FTrayIcon->CancelBalloon();
  6399. }
  6400. }
  6401. return Result;
  6402. }
  6403. }
  6404. //---------------------------------------------------------------------------
  6405. void __fastcall TCustomScpExplorerForm::ShowExtendedException(
  6406. TTerminal * Terminal, Exception * E)
  6407. {
  6408. if (FTrayIcon->Visible)
  6409. {
  6410. PopupTrayBalloon(Terminal, L"", qtError, E);
  6411. }
  6412. // particularly prevent opening new session from jump list,
  6413. // while exception is shown
  6414. NonVisualDataModule->StartBusy();
  6415. try
  6416. {
  6417. ShowExtendedExceptionEx(Terminal, E);
  6418. }
  6419. __finally
  6420. {
  6421. NonVisualDataModule->EndBusy();
  6422. FTrayIcon->CancelBalloon();
  6423. }
  6424. }
  6425. //---------------------------------------------------------------------------
  6426. void __fastcall TCustomScpExplorerForm::TerminalReady()
  6427. {
  6428. // cannot rely on active page being page for active terminal,
  6429. // as it can happen that active page is the "new session" page
  6430. // (e.g. when reconnecting active terminal, while login dialog
  6431. // invoked from "new session" page is modal)
  6432. int ActiveTerminalIndex = TTerminalManager::Instance()->ActiveTerminalIndex;
  6433. UpdateSessionTab(SessionsPageControl->Pages[ActiveTerminalIndex]);
  6434. }
  6435. //---------------------------------------------------------------------------
  6436. void __fastcall TCustomScpExplorerForm::InactiveTerminalException(
  6437. TTerminal * Terminal, Exception * E)
  6438. {
  6439. Notify(Terminal, L"", qtError, false, NULL, NULL, E);
  6440. if (!Terminal->Active)
  6441. {
  6442. int Index = TTerminalManager::Instance()->IndexOf(Terminal);
  6443. if (DebugAlwaysTrue((Index >= 0) && (Index < SessionsPageControl->PageCount)))
  6444. {
  6445. UpdateSessionTab(SessionsPageControl->Pages[Index]);
  6446. }
  6447. }
  6448. }
  6449. //---------------------------------------------------------------------------
  6450. void __fastcall TCustomScpExplorerForm::Notify(TTerminal * Terminal,
  6451. UnicodeString Message, TQueryType Type,
  6452. bool Important, TNotifyEvent OnClick, TObject * UserData, Exception * E)
  6453. {
  6454. if ((E == NULL) ||
  6455. ExceptionMessage(E, Message))
  6456. {
  6457. unsigned int Seconds = WinConfiguration->NotificationsTimeout;
  6458. if (Important)
  6459. {
  6460. Seconds *= 5;
  6461. }
  6462. UnicodeString NoteMessage(UnformatMessage(Message));
  6463. if (Terminal != NULL)
  6464. {
  6465. NoteMessage = FORMAT(L"%s: %s",
  6466. (TTerminalManager::Instance()->GetTerminalTitle(Terminal, true), NoteMessage));
  6467. }
  6468. if (WinConfiguration->BalloonNotifications)
  6469. {
  6470. AddNote(NoteMessage);
  6471. PopupTrayBalloon(Terminal, Message, Type, NULL, Seconds, OnClick, UserData);
  6472. }
  6473. else
  6474. {
  6475. FlashOnBackground();
  6476. PostNote(NoteMessage, Seconds, OnClick, UserData);
  6477. }
  6478. }
  6479. }
  6480. //---------------------------------------------------------------------------
  6481. void __fastcall TCustomScpExplorerForm::QueueEmptyNoteClicked(TObject * Sender)
  6482. {
  6483. RestoreApp();
  6484. TTerminalNoteData * TerminalNoteData = dynamic_cast<TTerminalNoteData *>(Sender);
  6485. if (DebugAlwaysTrue(TerminalNoteData != NULL) &&
  6486. !NonVisualDataModule->Busy)
  6487. {
  6488. TTerminal * Terminal = TerminalNoteData->Terminal;
  6489. TTerminalManager::Instance()->ActiveTerminal = Terminal;
  6490. if (!ComponentVisible[fcQueueView])
  6491. {
  6492. ToggleQueueVisibility();
  6493. GoToQueue();
  6494. }
  6495. }
  6496. }
  6497. //---------------------------------------------------------------------------
  6498. void __fastcall TCustomScpExplorerForm::QueueEvent(TTerminal * ATerminal,
  6499. TTerminalQueue * /*Queue*/, TQueueEvent Event)
  6500. {
  6501. TManagedTerminal * ManagedTerminal = DebugNotNull(dynamic_cast<TManagedTerminal *>(ATerminal));
  6502. UnicodeString Message;
  6503. TNotifyEvent OnClick = NULL;
  6504. TObject * UserData = NULL;
  6505. bool QueueInvisible = !ComponentVisible[fcQueueView] || IsApplicationMinimized();
  6506. switch (Event)
  6507. {
  6508. case qeEmptyButMonitored:
  6509. if ((ATerminal != Terminal) || QueueInvisible)
  6510. {
  6511. Message = LoadStr(BALLOON_QUEUE_EMPTY);
  6512. OnClick = QueueEmptyNoteClicked;
  6513. TTerminalNoteData * TerminalNoteData = new TTerminalNoteData();
  6514. TerminalNoteData->Terminal = ATerminal;
  6515. UserData = TerminalNoteData;
  6516. }
  6517. break;
  6518. case qeEmpty:
  6519. OperationComplete(ManagedTerminal->QueueOperationStart);
  6520. break;
  6521. case qePendingUserAction:
  6522. if ((ATerminal != Terminal) ||
  6523. (QueueInvisible && !IsQueueAutoPopup()))
  6524. {
  6525. Message = LoadStr(BALLOON_QUEUE_USER_ACTION);
  6526. }
  6527. break;
  6528. default:
  6529. DebugFail();
  6530. }
  6531. if (!Message.IsEmpty())
  6532. {
  6533. Notify(ATerminal, Message, qtInformation, false, OnClick, UserData);
  6534. }
  6535. }
  6536. //---------------------------------------------------------------------------
  6537. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDragFileList(
  6538. TObject * /*Sender*/, TFileList * FileList, bool & Created)
  6539. {
  6540. if (FDDExtMapFile != NULL)
  6541. {
  6542. CloseHandle(FDDExtMapFile);
  6543. FDDExtMapFile = NULL;
  6544. }
  6545. if (WinConfiguration->DDFakeFile)
  6546. {
  6547. DDFakeFileInitDrag(FileList, Created);
  6548. }
  6549. }
  6550. //---------------------------------------------------------------------------
  6551. void __fastcall TCustomScpExplorerForm::DDFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  6552. {
  6553. if (DebugAlwaysTrue(!FDragFakeDirectory.IsEmpty()) &&
  6554. SameText(ExtractFileName(FileName), ExtractFileName(FDragFakeDirectory)))
  6555. {
  6556. FFakeFileDropTarget = FileName;
  6557. }
  6558. }
  6559. //---------------------------------------------------------------------------
  6560. UnicodeString __fastcall TCustomScpExplorerForm::CreateFakeTransferDirectory()
  6561. {
  6562. UnicodeString Result = ExcludeTrailingBackslash(WinConfiguration->TemporaryDir());
  6563. if (!ForceDirectories(ApiPath(Result)))
  6564. {
  6565. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (Result)));
  6566. }
  6567. FileSetAttr(ApiPath(Result), faHidden);
  6568. return Result;
  6569. }
  6570. //---------------------------------------------------------------------------
  6571. void __fastcall TCustomScpExplorerForm::DDFakeFileInitDrag(TFileList * FileList,
  6572. bool & Created)
  6573. {
  6574. FFakeFileDropTarget = UnicodeString();
  6575. FDragFakeDirectory = CreateFakeTransferDirectory();
  6576. FileList->AddItem(NULL, FDragFakeDirectory);
  6577. Created = true;
  6578. if (!WinConfiguration->IsDDExtRunning() || (GetWindowsBuild() >= 17134))
  6579. {
  6580. FDragFakeMonitors = StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, DDFakeCreated);
  6581. }
  6582. FDDExtMapFile = CreateFileMappingA((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
  6583. 0, sizeof(TDragExtCommStruct), AnsiString(DRAG_EXT_MAPPING).c_str());
  6584. {
  6585. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6586. TDragExtCommStruct* CommStruct;
  6587. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6588. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6589. DebugAssert(CommStruct != NULL);
  6590. CommStruct->Version = TDragExtCommStruct::CurrentVersion;
  6591. CommStruct->Dragging = true;
  6592. wcsncpy(CommStruct->DropDest, FDragFakeDirectory.c_str(),
  6593. LENOF(CommStruct->DropDest));
  6594. NULL_TERMINATE(CommStruct->DropDest);
  6595. UnmapViewOfFile(CommStruct);
  6596. }
  6597. }
  6598. //---------------------------------------------------------------------------
  6599. bool __fastcall TCustomScpExplorerForm::RemoteFileControlFileOperation(
  6600. TObject * Sender, TFileOperation Operation, bool NoConfirmation, void * Param)
  6601. {
  6602. bool Result;
  6603. if (Sender == RemoteDirView)
  6604. {
  6605. Result = ExecuteFileOperation(Operation, osRemote, true, NoConfirmation, Param);
  6606. }
  6607. else
  6608. {
  6609. DebugAssert(Sender == RemoteDriveView);
  6610. TStrings * FileList = RemoteDriveView->DragFileList();
  6611. try
  6612. {
  6613. Result = ExecuteFileOperation(Operation, osRemote, FileList, NoConfirmation, Param);
  6614. }
  6615. __finally
  6616. {
  6617. delete FileList;
  6618. }
  6619. }
  6620. if (FDDTargetControl == RemoteDriveView)
  6621. {
  6622. RemoteDriveView->UpdateDropTarget();
  6623. }
  6624. // foRemoteMove happens when file/dir is dragged within the remote tree view
  6625. // or from tree view to dir view.
  6626. // foMove happens when file/dir is dragged from remote tree view outside of
  6627. // application via dragex
  6628. if (((Operation == foRemoteMove) || (Operation == foMove)) &&
  6629. (DropSourceControl == RemoteDriveView))
  6630. {
  6631. RemoteDriveView->UpdateDropSource();
  6632. }
  6633. return Result;
  6634. }
  6635. //---------------------------------------------------------------------------
  6636. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDEnd(TObject * Sender)
  6637. {
  6638. // This also handles drops of remote files to queue.
  6639. // Drops of local files (uploads) are handled in QueueDDProcessDropped.
  6640. SAFE_DESTROY(FDDFileList);
  6641. if (!FFakeFileDropTarget.IsEmpty())
  6642. {
  6643. RemoveDir(ApiPath(FFakeFileDropTarget));
  6644. }
  6645. if ((FDDExtMapFile != NULL) || (FDDTargetControl == QueueView3))
  6646. {
  6647. try
  6648. {
  6649. TDragResult DDResult = (Sender == RemoteDirView) ?
  6650. RemoteDirView->LastDDResult : RemoteDriveView->LastDDResult;
  6651. // Focus is moved to the target application,
  6652. // but as we are going to present the UI, we need to steal the focus back.
  6653. // This most likely won't work though (windows does not allow application
  6654. // to steal focus most of the time)
  6655. Application->BringToFront();
  6656. // On older version of Windows we never got drMove here, see also comment below.
  6657. // On Windows 10, we get the "move".
  6658. if ((DDResult == drCopy) || (DDResult == drMove) || (DDResult == drInvalid))
  6659. {
  6660. UnicodeString TargetDirectory;
  6661. TFileOperation Operation;
  6662. // drInvalid may mean drMove, see comment below
  6663. switch (DDResult)
  6664. {
  6665. case drCopy:
  6666. Operation = foCopy;
  6667. break;
  6668. case drMove:
  6669. Operation = foMove;
  6670. break;
  6671. default:
  6672. DebugFail();
  6673. case drInvalid:
  6674. // prefer "copy" for safety
  6675. Operation = FLAGSET(FLastDropEffect, DROPEFFECT_MOVE) ? foMove : foCopy;
  6676. break;
  6677. }
  6678. TTransferOperationParam Param;
  6679. UnicodeString CounterName;
  6680. bool ForceQueue;
  6681. if (!DDGetTarget(Param.TargetDirectory, ForceQueue, CounterName))
  6682. {
  6683. // we get drInvalid both if d&d was intercepted by ddext,
  6684. // and when users drops on no-drop location.
  6685. // we tell the difference by existence of response from ddext,
  6686. // so we ignore absence of response in this case
  6687. if (DDResult != drInvalid)
  6688. {
  6689. // here we know that the extension is installed,
  6690. // as it is checked as soon as drag&drop starts from
  6691. // RemoteFileControlDDCreateDragFileList
  6692. Configuration->Usage->Inc(L"DownloadsDragDropExternalExtTargetUnknown");
  6693. throw ExtException(NULL, LoadStr(DRAGEXT_TARGET_UNKNOWN2), HELP_DRAGEXT_TARGET_UNKNOWN);
  6694. }
  6695. }
  6696. else
  6697. {
  6698. // download using fake file
  6699. Param.Temp = false;
  6700. Param.DragDrop = true;
  6701. if (ForceQueue)
  6702. {
  6703. Param.Queue = asOn;
  6704. }
  6705. if (Sender == RemoteDirView)
  6706. {
  6707. Param.Options = FLAGMASK(SelectedAllFilesInDirView(RemoteDirView), coAllFiles);
  6708. }
  6709. if (RemoteFileControlFileOperation(Sender, Operation,
  6710. (WinConfiguration->DDTransferConfirmation == asOff), &Param))
  6711. {
  6712. Configuration->Usage->Inc(CounterName);
  6713. }
  6714. }
  6715. }
  6716. }
  6717. __finally
  6718. {
  6719. CloseHandle(FDDExtMapFile);
  6720. FDDExtMapFile = NULL;
  6721. delete FDragFakeMonitors;
  6722. FDragFakeMonitors = NULL;
  6723. RemoveDir(ApiPath(FDragFakeDirectory));
  6724. FDragFakeDirectory = L"";
  6725. FFakeFileDropTarget = UnicodeString();
  6726. }
  6727. }
  6728. if (FDragDropSshTerminate.get() != NULL)
  6729. {
  6730. FDragDropSshTerminate->Rethrow();
  6731. }
  6732. }
  6733. //---------------------------------------------------------------------------
  6734. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDGiveFeedback(
  6735. TObject * /*Sender*/, int dwEffect, HRESULT & /*Result*/)
  6736. {
  6737. // Remember drop effect so we know (when user drops files), if we copy or move
  6738. FLastDropEffect = dwEffect;
  6739. }
  6740. //---------------------------------------------------------------------------
  6741. bool __fastcall TCustomScpExplorerForm::DDGetTarget(
  6742. UnicodeString & Directory, bool & ForceQueue, UnicodeString & CounterName)
  6743. {
  6744. bool Result;
  6745. if (FDDTargetControl == QueueView3)
  6746. {
  6747. Directory = DefaultDownloadTargetDirectory();
  6748. Result = true;
  6749. CounterName = L"DownloadsDragDropQueue";
  6750. ForceQueue = true;
  6751. }
  6752. else if (!FFakeFileDropTarget.IsEmpty())
  6753. {
  6754. Directory = ExcludeTrailingBackslash(ExtractFilePath(FFakeFileDropTarget));
  6755. Result = true;
  6756. ForceQueue = false;
  6757. CounterName = L"DownloadsDragDropFakeFile";
  6758. }
  6759. else
  6760. {
  6761. ForceQueue = false;
  6762. Enabled = false;
  6763. try
  6764. {
  6765. int Timer = 0;
  6766. Result = false;
  6767. while (!Result && (Timer < WinConfiguration->DDExtTimeout))
  6768. {
  6769. {
  6770. TMutexGuard Guard(FDDExtMutex, DRAGEXT_MUTEX_RELEASE_TIMEOUT);
  6771. TDragExtCommStruct* CommStruct;
  6772. CommStruct = static_cast<TDragExtCommStruct*>(MapViewOfFile(FDDExtMapFile,
  6773. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  6774. DebugAssert(CommStruct != NULL);
  6775. Result = !CommStruct->Dragging;
  6776. if (Result)
  6777. {
  6778. Directory = ExtractFilePath(CommStruct->DropDest);
  6779. CounterName = L"DownloadsDragDropExternalExt";
  6780. }
  6781. UnmapViewOfFile(CommStruct);
  6782. }
  6783. if (!Result)
  6784. {
  6785. Sleep(50);
  6786. Timer += 50;
  6787. Application->ProcessMessages();
  6788. }
  6789. }
  6790. }
  6791. __finally
  6792. {
  6793. Enabled = true;
  6794. }
  6795. }
  6796. return Result;
  6797. }
  6798. //---------------------------------------------------------------------------
  6799. void __fastcall TCustomScpExplorerForm::AddDelayedDirectoryDeletion(
  6800. const UnicodeString TempDir, int SecDelay)
  6801. {
  6802. TDateTime Alarm = Now() + (double)((double)SecDelay*OneMillisecond);
  6803. FDelayedDeletionList->AddObject(TempDir, reinterpret_cast<TObject*>(Alarm.FileDate()));
  6804. if (FDelayedDeletionTimer == NULL)
  6805. {
  6806. DebugAssert(HandleAllocated());
  6807. FDelayedDeletionTimer = new TTimer(this);
  6808. FDelayedDeletionTimer->Interval = 10000;
  6809. FDelayedDeletionTimer->OnTimer = DoDelayedDeletion;
  6810. }
  6811. else
  6812. {
  6813. FDelayedDeletionTimer->Enabled = true;
  6814. }
  6815. }
  6816. //---------------------------------------------------------------------------
  6817. void __fastcall TCustomScpExplorerForm::DoDelayedDeletion(TObject * Sender)
  6818. {
  6819. DebugAssert(FDelayedDeletionList != NULL);
  6820. TDateTime N = Now();
  6821. TDateTime Alert;
  6822. UnicodeString Directory;
  6823. for (int Index = FDelayedDeletionList->Count-1; Index >= 0; Index--)
  6824. {
  6825. Alert = FileDateToDateTime(reinterpret_cast<int>(FDelayedDeletionList->Objects[Index]));
  6826. if ((N >= Alert) || (Sender == NULL))
  6827. {
  6828. Directory = FDelayedDeletionList->Strings[Index];
  6829. if (DeleteDirectory(ExcludeTrailingBackslash(Directory)))
  6830. {
  6831. FDelayedDeletionList->Delete(Index);
  6832. }
  6833. }
  6834. }
  6835. if (FDelayedDeletionList->Count == 0)
  6836. {
  6837. FDelayedDeletionTimer->Enabled = false;
  6838. }
  6839. }
  6840. //---------------------------------------------------------------------------
  6841. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDTargetDrop()
  6842. {
  6843. if (IsFileControl(FDDTargetControl, osRemote) ||
  6844. (FDDTargetControl == SessionsPageControl))
  6845. {
  6846. TTerminal * TargetTerminal = NULL;
  6847. TFileOperation Operation = foNone;
  6848. if (FDDTargetControl == SessionsPageControl)
  6849. {
  6850. TPoint Point = SessionsPageControl->ScreenToClient(Mouse->CursorPos);
  6851. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  6852. // do not allow dropping on the "+" tab
  6853. TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  6854. if (TargetTerminal != NULL)
  6855. {
  6856. if ((FLastDropEffect == DROPEFFECT_MOVE) &&
  6857. (TargetTerminal == TTerminalManager::Instance()->ActiveTerminal))
  6858. {
  6859. Operation = foRemoteMove;
  6860. }
  6861. else
  6862. {
  6863. Operation = foRemoteCopy;
  6864. }
  6865. }
  6866. }
  6867. else
  6868. {
  6869. if (FLastDropEffect == DROPEFFECT_MOVE)
  6870. {
  6871. Operation = foRemoteMove;
  6872. }
  6873. else if (FLastDropEffect == DROPEFFECT_COPY)
  6874. {
  6875. Operation = foRemoteCopy;
  6876. }
  6877. }
  6878. if (Operation != foNone)
  6879. {
  6880. RemoteFileControlFileOperation(DropSourceControl,
  6881. Operation, (WinConfiguration->DDTransferConfirmation == asOff), TargetTerminal);
  6882. }
  6883. // abort drag&drop
  6884. Abort();
  6885. }
  6886. else if ((FDDExtMapFile == NULL) && (FLastDropEffect != DROPEFFECT_NONE) &&
  6887. // Drops of remote files to queue are handled in RemoteFileControlDDEnd
  6888. (FDDTargetControl != QueueView3))
  6889. {
  6890. DebugAssert(!FDragTempDir.IsEmpty());
  6891. TTransferType Type;
  6892. UnicodeString TempDir = FDragTempDir;
  6893. // We clear FDragTempDir before calling
  6894. // just in case it fail (raises exception)
  6895. FDragTempDir = L"";
  6896. Type = (FLastDropEffect & DROPEFFECT_MOVE ? ttMove : Type = ttCopy);
  6897. TGUICopyParamType CopyParams = GUIConfiguration->CurrentCopyParam;
  6898. // empty directory parameter means temp directory -> don't display it!
  6899. UnicodeString TargetDir = L"";
  6900. int Options = 0;
  6901. if (!CopyParamDialog(tdToLocal, Type, true, FDDFileList,
  6902. TargetDir, CopyParams, (WinConfiguration->DDTransferConfirmation != asOff), true, Options))
  6903. {
  6904. Abort();
  6905. }
  6906. // TargetDir is set when dropped on local file control
  6907. // (this was workaround for legacy dirview event handling, now it should be
  6908. // made prettier)
  6909. if (TargetDir.IsEmpty())
  6910. {
  6911. TargetDir = TempDir;
  6912. if (ForceDirectories(ApiPath(TargetDir)))
  6913. {
  6914. DebugAssert(Terminal && !TargetDir.IsEmpty());
  6915. FPendingTempSpaceWarn = true;
  6916. try
  6917. {
  6918. FDragDropOperation = true;
  6919. int Params = cpTemporary |
  6920. (Type == ttMove ? cpDelete : 0);
  6921. DDDownload(FDDFileList, TargetDir, &CopyParams, Params);
  6922. Configuration->Usage->Inc(L"DownloadsDragDropExternalTemp");
  6923. }
  6924. __finally
  6925. {
  6926. FDragDropOperation = false;
  6927. FPendingTempSpaceWarn = false;
  6928. AddDelayedDirectoryDeletion(TargetDir, WinConfiguration->DDDeleteDelay);
  6929. }
  6930. }
  6931. else
  6932. {
  6933. throw Exception(FMTLOAD(CREATE_TEMP_DIR_ERROR, (TargetDir)));
  6934. }
  6935. }
  6936. }
  6937. }
  6938. //---------------------------------------------------------------------------
  6939. void __fastcall TCustomScpExplorerForm::DDDownload(
  6940. TStrings * FilesToCopy, const UnicodeString & TargetDir, const TCopyParamType * CopyParam, int Params)
  6941. {
  6942. TAutoBatch AutoBatch(this);
  6943. UpdateCopyParamCounters(*CopyParam);
  6944. Terminal->CopyToLocal(FilesToCopy, TargetDir, CopyParam, Params, NULL);
  6945. if (FLAGSET(Params, cpDelete) && (DropSourceControl == RemoteDriveView))
  6946. {
  6947. RemoteDriveView->UpdateDropSource();
  6948. }
  6949. }
  6950. //---------------------------------------------------------------------------
  6951. class TFakeDataObjectFilesEx : public TDataObjectFilesEx
  6952. {
  6953. public:
  6954. __fastcall TFakeDataObjectFilesEx(TFileList * AFileList, bool RenderPIDL,
  6955. bool RenderFilename) : TDataObjectFilesEx(AFileList, RenderPIDL, RenderFilename, true)
  6956. {
  6957. }
  6958. virtual bool __fastcall AllowData(const tagFORMATETC & FormatEtc)
  6959. {
  6960. return (FormatEtc.cfFormat == CF_HDROP) ? false :
  6961. TDataObjectFilesEx::AllowData(FormatEtc);
  6962. }
  6963. };
  6964. //---------------------------------------------------------------------------
  6965. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDCreateDataObject(
  6966. TObject * Sender, TDataObject *& DataObject)
  6967. {
  6968. if (FDDExtMapFile != NULL)
  6969. {
  6970. TFileList * FileList = DragDropFiles(Sender)->FileList;
  6971. if (!FileList->RenderPIDLs() || !FileList->RenderNames())
  6972. {
  6973. Abort();
  6974. }
  6975. if (FileList->Count > 0)
  6976. {
  6977. TDataObjectFilesEx * FilesObject = new TFakeDataObjectFilesEx(FileList, true, true);
  6978. if (!FilesObject->IsValid(true, true))
  6979. {
  6980. FilesObject->_Release();
  6981. }
  6982. else
  6983. {
  6984. DataObject = FilesObject;
  6985. }
  6986. }
  6987. }
  6988. }
  6989. //---------------------------------------------------------------------------
  6990. void __fastcall TCustomScpExplorerForm::GoToCommandLine()
  6991. {
  6992. DebugFail();
  6993. }
  6994. //---------------------------------------------------------------------------
  6995. void __fastcall TCustomScpExplorerForm::GoToTree()
  6996. {
  6997. ComponentVisible[fcRemoteTree] = true;
  6998. RemoteDriveView->SetFocus();
  6999. }
  7000. //---------------------------------------------------------------------------
  7001. TStrings * __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7002. TPanelExport Export)
  7003. {
  7004. TCustomDirView * DirView = this->DirView(Side);
  7005. std::unique_ptr<TStrings> ExportData(new TStringList());
  7006. switch (Export)
  7007. {
  7008. case pePath:
  7009. ExportData->Add(DirView->PathName);
  7010. break;
  7011. case peFileList:
  7012. case peFullFileList:
  7013. {
  7014. bool FullPath = (Export == peFullFileList);
  7015. DirView->CreateFileList(false, FullPath, ExportData.get());
  7016. for (int Index = 0; Index < ExportData->Count; Index++)
  7017. {
  7018. if (ExportData->Strings[Index].Pos(L" ") > 0)
  7019. {
  7020. ExportData->Strings[Index] = FORMAT(L"\"%s\"", (ExportData->Strings[Index]));
  7021. }
  7022. }
  7023. }
  7024. break;
  7025. default:
  7026. DebugFail();
  7027. }
  7028. return ExportData.release();
  7029. }
  7030. //---------------------------------------------------------------------------
  7031. void __fastcall TCustomScpExplorerForm::PanelExport(TOperationSide Side,
  7032. TPanelExport Export, TPanelExportDestination Destination)
  7033. {
  7034. std::unique_ptr<TStrings> ExportData(PanelExport(Side, Export));
  7035. PanelExportStore(Side, Export, Destination, ExportData.get());
  7036. }
  7037. //---------------------------------------------------------------------------
  7038. void __fastcall TCustomScpExplorerForm::PanelExportStore(TOperationSide /*Side*/,
  7039. TPanelExport /*Export*/, TPanelExportDestination Destination,
  7040. TStrings * ExportData)
  7041. {
  7042. if (Destination == pedClipboard)
  7043. {
  7044. TInstantOperationVisualizer Visualizer;
  7045. CopyToClipboard(ExportData);
  7046. }
  7047. else
  7048. {
  7049. DebugFail();
  7050. }
  7051. }
  7052. //---------------------------------------------------------------------------
  7053. void __fastcall TCustomScpExplorerForm::Filter(TOperationSide Side)
  7054. {
  7055. TCustomDirView * DirView = this->DirView(Side);
  7056. TFileFilter Filter;
  7057. DefaultFileFilter(Filter);
  7058. Filter.Masks = DirView->Mask;
  7059. if (DoFilterMaskDialog(DirView, &Filter))
  7060. {
  7061. DirView->Mask = TFileMasks::NormalizeMask(Filter.Masks);
  7062. Configuration->Usage->Inc(L"Filters");
  7063. }
  7064. }
  7065. //---------------------------------------------------------------------------
  7066. TQueueOperation __fastcall TCustomScpExplorerForm::DefaultQueueOperation()
  7067. {
  7068. return FQueueController->DefaultOperation();
  7069. }
  7070. //---------------------------------------------------------------------------
  7071. bool __fastcall TCustomScpExplorerForm::AllowQueueOperation(
  7072. TQueueOperation Operation, void ** Param)
  7073. {
  7074. switch (Operation)
  7075. {
  7076. case qoPreferences:
  7077. return true;
  7078. case qoGoTo:
  7079. return ComponentVisible[fcQueueView] && QueueView3->Enabled;
  7080. case qoOnceEmpty:
  7081. return !FQueueController->Empty;
  7082. default:
  7083. return FQueueController->AllowOperation(Operation, Param);
  7084. }
  7085. }
  7086. //---------------------------------------------------------------------------
  7087. void __fastcall TCustomScpExplorerForm::GoToQueue()
  7088. {
  7089. if (DebugAlwaysTrue(QueueView3->Visible))
  7090. {
  7091. QueueView3->SetFocus();
  7092. }
  7093. }
  7094. //---------------------------------------------------------------------------
  7095. void __fastcall TCustomScpExplorerForm::ExecuteQueueOperation(
  7096. TQueueOperation Operation, void * Param)
  7097. {
  7098. if (Operation == qoGoTo)
  7099. {
  7100. GoToQueue();
  7101. }
  7102. else if (Operation == qoPreferences)
  7103. {
  7104. PreferencesDialog(pmQueue);
  7105. }
  7106. else
  7107. {
  7108. FQueueController->ExecuteOperation(Operation, Param);
  7109. }
  7110. }
  7111. //---------------------------------------------------------------------------
  7112. bool __fastcall TCustomScpExplorerForm::GetQueueEnabled()
  7113. {
  7114. return (Queue != NULL) && Queue->Enabled;
  7115. }
  7116. //---------------------------------------------------------------------------
  7117. void __fastcall TCustomScpExplorerForm::ToggleQueueEnabled()
  7118. {
  7119. DebugAssert(Queue != NULL);
  7120. if (Queue != NULL)
  7121. {
  7122. Queue->Enabled = !Queue->Enabled;
  7123. }
  7124. }
  7125. //---------------------------------------------------------------------------
  7126. void __fastcall TCustomScpExplorerForm::QueueView3ContextPopup(
  7127. TObject * /*Sender*/, TPoint & /*MousePos*/, bool & /*Handled*/)
  7128. {
  7129. FQueueActedItem = QueueView3->ItemFocused;
  7130. }
  7131. //---------------------------------------------------------------------------
  7132. /*virtual*/ int __fastcall TCustomScpExplorerForm::GetStaticComponentsHeight()
  7133. {
  7134. return TopDock->Height + QueueSplitter->Height;
  7135. }
  7136. //---------------------------------------------------------------------------
  7137. void __fastcall TCustomScpExplorerForm::QueueSplitterCanResize(
  7138. TObject * /*Sender*/, int & NewSize, bool & Accept)
  7139. {
  7140. // when queue is hidden by double-clicking splitter, stray attempt to
  7141. // resize the panel with strange value arrives, make sure it is ignored
  7142. if (ComponentVisible[fcQueueView])
  7143. {
  7144. int HeightLimit = ClientHeight - GetStaticComponentsHeight() -
  7145. RemotePanel->Constraints->MinHeight;
  7146. if (NewSize > HeightLimit)
  7147. {
  7148. NewSize = HeightLimit;
  7149. }
  7150. }
  7151. else
  7152. {
  7153. Accept = false;
  7154. }
  7155. }
  7156. //---------------------------------------------------------------------------
  7157. void __fastcall TCustomScpExplorerForm::QueueView3StartDrag(TObject * /*Sender*/,
  7158. TDragObject *& /*DragObject*/)
  7159. {
  7160. FQueueActedItem = QueueView3->ItemFocused;
  7161. }
  7162. //---------------------------------------------------------------------------
  7163. void __fastcall TCustomScpExplorerForm::QueueView3DragOver(TObject * /*Sender*/,
  7164. TObject * Source, int X, int Y, TDragState /*State*/, bool & Accept)
  7165. {
  7166. Accept = true;
  7167. if (Source == QueueView3)
  7168. {
  7169. TListItem * DropTarget = QueueView3->GetItemAt(X, Y);
  7170. Accept = (DropTarget != NULL) && (FQueueActedItem != NULL);
  7171. if (Accept)
  7172. {
  7173. TQueueItemProxy * QueueItem;
  7174. TQueueItemProxy * DestQueueItem;
  7175. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7176. DestQueueItem = static_cast<TQueueItemProxy *>(DropTarget->Data);
  7177. Accept = (QueueItem != DestQueueItem) &&
  7178. (QueueItem->Status == TQueueItem::qsPending) &&
  7179. (DestQueueItem->Status == TQueueItem::qsPending);
  7180. }
  7181. }
  7182. }
  7183. //---------------------------------------------------------------------------
  7184. void __fastcall TCustomScpExplorerForm::QueueView3DragDrop(TObject * /*Sender*/,
  7185. TObject * /*Source*/, int /*X*/, int /*Y*/)
  7186. {
  7187. if ((FQueueActedItem != NULL) && (QueueView3->DropTarget != NULL))
  7188. {
  7189. TQueueItemProxy * QueueItem;
  7190. TQueueItemProxy * DestQueueItem;
  7191. QueueItem = static_cast<TQueueItemProxy *>(FQueueActedItem->Data);
  7192. DestQueueItem = static_cast<TQueueItemProxy *>(QueueView3->DropTarget->Data);
  7193. QueueItem->Move(DestQueueItem);
  7194. }
  7195. }
  7196. //---------------------------------------------------------------------------
  7197. void __fastcall TCustomScpExplorerForm::QueueView3Enter(TObject * /*Sender*/)
  7198. {
  7199. if ((QueueView3->ItemFocused == NULL) &&
  7200. (QueueView3->Items->Count > 0))
  7201. {
  7202. QueueView3->ItemFocused = QueueView3->Items->Item[0];
  7203. }
  7204. QueueLabelUpdateStatus();
  7205. }
  7206. //---------------------------------------------------------------------------
  7207. void __fastcall TCustomScpExplorerForm::QueueView3Exit(TObject * /*Sender*/)
  7208. {
  7209. QueueLabelUpdateStatus();
  7210. }
  7211. //---------------------------------------------------------------------------
  7212. void __fastcall TCustomScpExplorerForm::QueueLabelUpdateStatus()
  7213. {
  7214. QueueLabel->UpdateStatus();
  7215. }
  7216. //---------------------------------------------------------------------------
  7217. void __fastcall TCustomScpExplorerForm::QueueView3SelectItem(
  7218. TObject * /*Sender*/, TListItem * /*Item*/, bool Selected)
  7219. {
  7220. if (Selected)
  7221. {
  7222. NonVisualDataModule->UpdateNonVisibleActions();
  7223. }
  7224. }
  7225. //---------------------------------------------------------------------------
  7226. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::DragDropFiles(TObject * Sender)
  7227. {
  7228. TDragDropFilesEx * Result = NULL;
  7229. if (Sender == SessionsPageControl)
  7230. {
  7231. Result = FSessionsDragDropFilesEx;
  7232. }
  7233. else if (Sender == QueueView3)
  7234. {
  7235. Result = FQueueDragDropFilesEx;
  7236. }
  7237. else
  7238. {
  7239. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7240. if (DirView != NULL)
  7241. {
  7242. Result = DirView->DragDropFilesEx;
  7243. }
  7244. else
  7245. {
  7246. TCustomDriveView * DriveView = dynamic_cast<TCustomDriveView *>(Sender);
  7247. if (DriveView != NULL)
  7248. {
  7249. Result = DriveView->DragDropFilesEx;
  7250. }
  7251. }
  7252. }
  7253. DebugAssert(Result != NULL);
  7254. return Result;
  7255. }
  7256. //---------------------------------------------------------------------------
  7257. bool __fastcall TCustomScpExplorerForm::SelectedAllFilesInDirView(TCustomDirView * DView)
  7258. {
  7259. return (DView->SelCount == DView->FilesCount);
  7260. }
  7261. //---------------------------------------------------------------------------
  7262. bool __fastcall TCustomScpExplorerForm::DraggingAllFilesFromDirView(TOperationSide Side, TStrings * FileList)
  7263. {
  7264. return HasDirView[Side] && (DropSourceControl == DirView(Side)) && (FileList->Count == DirView(Side)->FilesCount);
  7265. }
  7266. //---------------------------------------------------------------------------
  7267. void __fastcall TCustomScpExplorerForm::RemoteFileControlDragDropFileOperation(
  7268. TObject * Sender, int Effect, UnicodeString TargetPath, bool ForceQueue, bool DragDrop)
  7269. {
  7270. TFileOperation Operation;
  7271. switch (Effect)
  7272. {
  7273. case DROPEFFECT_MOVE:
  7274. Operation = foMove;
  7275. break;
  7276. case DROPEFFECT_COPY:
  7277. // occures on WinXP (reported by user)
  7278. default:
  7279. Operation = foCopy;
  7280. break;
  7281. };
  7282. TDragDropFilesEx * DragDropFilesEx = DragDropFiles(Sender);
  7283. // see a comment in TUnixDirView::PerformItemDragDropOperation
  7284. if (DragDropFilesEx->FileList->Count > 0)
  7285. {
  7286. TStrings * FileList = new TStringList();
  7287. try
  7288. {
  7289. for (int Index = 0; Index < DragDropFilesEx->FileList->Count; Index++)
  7290. {
  7291. FileList->Add(DragDropFilesEx->FileList->Items[Index]->Name);
  7292. }
  7293. FDragDropOperation = true;
  7294. TTransferOperationParam Param;
  7295. Param.TargetDirectory = TargetPath;
  7296. // upload, no temp dirs
  7297. Param.Temp = false;
  7298. Param.DragDrop = DragDrop;
  7299. Param.Options =
  7300. FLAGMASK(DraggingAllFilesFromDirView(osLocal, FileList), coAllFiles);
  7301. if (ForceQueue)
  7302. {
  7303. Param.Queue = asOn;
  7304. }
  7305. bool NoConfirmation = DragDrop ? (WinConfiguration->DDTransferConfirmation == asOff) : false;
  7306. if (ExecuteFileOperation(Operation, osLocal, FileList, NoConfirmation, &Param))
  7307. {
  7308. if (IsFileControl(DropSourceControl, osLocal))
  7309. {
  7310. Configuration->Usage->Inc(L"UploadsDragDropInternal");
  7311. }
  7312. else
  7313. {
  7314. Configuration->Usage->Inc(L"UploadsDragDropExternal");
  7315. }
  7316. }
  7317. }
  7318. __finally
  7319. {
  7320. FDragDropOperation = false;
  7321. delete FileList;
  7322. }
  7323. }
  7324. }
  7325. //---------------------------------------------------------------------------
  7326. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDFileOperation(
  7327. TObject * Sender, int Effect, UnicodeString /*SourcePath*/,
  7328. UnicodeString TargetPath, bool Paste, bool & /*DoOperation*/)
  7329. {
  7330. RemoteFileControlDragDropFileOperation(Sender, Effect, TargetPath, false, !Paste);
  7331. }
  7332. //---------------------------------------------------------------------------
  7333. void __fastcall TCustomScpExplorerForm::RemoteFileContolDDChooseEffect(
  7334. TObject * Sender, int grfKeyState, int & dwEffect)
  7335. {
  7336. // if any drop effect is allowed at all (e.g. no drop to self and drop to parent)
  7337. if ((dwEffect != DROPEFFECT_None) &&
  7338. IsFileControl(DropSourceControl, osRemote))
  7339. {
  7340. // do not allow drop on remote panel (on free space, still allow drop on directories)
  7341. if ((Sender == RemoteDirView) && (DropSourceControl == RemoteDirView) &&
  7342. (RemoteDirView->DropTarget == NULL))
  7343. {
  7344. dwEffect = DROPEFFECT_None;
  7345. }
  7346. else
  7347. {
  7348. if (dwEffect == DROPEFFECT_Copy)
  7349. {
  7350. bool MoveCapable = FTerminal->IsCapable[fcRemoteMove];
  7351. // currently we support copying always (at least via temporary directory);
  7352. // remove associated checks once this all proves stable and working
  7353. bool CopyCapable = true;
  7354. // if we do not support neither of operations, there's no discussion
  7355. if (!MoveCapable && !CopyCapable)
  7356. {
  7357. dwEffect = DROPEFFECT_None;
  7358. }
  7359. else
  7360. {
  7361. // The default effect inside remote panel is move,
  7362. // unless we do not support it, but support copy
  7363. if (FLAGCLEAR(grfKeyState, MK_CONTROL))
  7364. {
  7365. dwEffect = MoveCapable ? DROPEFFECT_Move : DROPEFFECT_Copy;
  7366. }
  7367. else
  7368. {
  7369. // with ctrl-down, we want copy unless it is not supported
  7370. dwEffect = CopyCapable ? DROPEFFECT_Copy : DROPEFFECT_None;
  7371. }
  7372. }
  7373. }
  7374. }
  7375. }
  7376. }
  7377. //---------------------------------------------------------------------------
  7378. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragFileName(
  7379. TObject * Sender, TRemoteFile * File, UnicodeString & FileName)
  7380. {
  7381. if (FDDTotalSize >= 0)
  7382. {
  7383. if (File->IsDirectory)
  7384. {
  7385. FDDTotalSize = -1;
  7386. }
  7387. else
  7388. {
  7389. FDDTotalSize += File->Size;
  7390. }
  7391. }
  7392. DebugAssert(!FDragTempDir.IsEmpty());
  7393. // TODO: this is quite ineffective
  7394. // TODO: what if invalid character replacement is disabled?
  7395. FileName = FDragTempDir + GUIConfiguration->CurrentCopyParam.ValidLocalFileName(File->FileName);
  7396. UnicodeString TransferFileName;
  7397. if (Sender == RemoteDriveView)
  7398. {
  7399. TransferFileName = UnixExcludeTrailingBackslash(File->FullFileName);
  7400. }
  7401. else
  7402. {
  7403. TransferFileName = File->FileName;
  7404. }
  7405. FDDFileList->AddObject(TransferFileName, File);
  7406. }
  7407. //---------------------------------------------------------------------------
  7408. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDDragDetect(
  7409. TObject * /*Sender*/, int /*grfKeyState*/, const TPoint & /*DetectStart*/,
  7410. const TPoint & /*Point*/, TDragDetectStatus /*DragStatus*/)
  7411. {
  7412. // sometimes we do not get DDEnd so the list is not released
  7413. SAFE_DESTROY(FDDFileList);
  7414. FDDFileList = new TStringList();
  7415. FDragTempDir = WinConfiguration->TemporaryDir();
  7416. FDDTotalSize = 0;
  7417. FDragDropSshTerminate.reset(NULL);
  7418. }
  7419. //---------------------------------------------------------------------------
  7420. void __fastcall TCustomScpExplorerForm::RemoteFileControlDDQueryContinueDrag(
  7421. TObject * /*Sender*/, BOOL /*FEscapePressed*/, int /*grfKeyState*/,
  7422. HRESULT & Result)
  7423. {
  7424. if (Result == DRAGDROP_S_DROP)
  7425. {
  7426. try
  7427. {
  7428. GlobalDragImageList->HideDragImage();
  7429. try
  7430. {
  7431. RemoteFileControlDDTargetDrop();
  7432. }
  7433. catch(ESshTerminate & E)
  7434. {
  7435. FDragDropSshTerminate.reset(E.Clone());
  7436. }
  7437. }
  7438. catch (Exception &E)
  7439. {
  7440. // If downloading fails we need to cancel drag&drop, otherwise
  7441. // Explorer shows error
  7442. // But by the way exception probably never reach this point as
  7443. // it's catched on way.
  7444. // Fatal exceptions get here (like when opening a secondary shell extension for file duplication fails).
  7445. Result = DRAGDROP_S_CANCEL;
  7446. ShowExtendedException(Terminal, &E);
  7447. }
  7448. }
  7449. }
  7450. //---------------------------------------------------------------------------
  7451. void __fastcall TCustomScpExplorerForm::DirViewMatchMask(
  7452. TObject * /*Sender*/, UnicodeString FileName, bool Directory, __int64 Size,
  7453. TDateTime Modification, UnicodeString Masks, bool & Matches, bool AllowImplicitMatches)
  7454. {
  7455. TFileMasks::TParams MaskParams;
  7456. MaskParams.Size = Size;
  7457. MaskParams.Modification = Modification;
  7458. // this does not re-parse the mask if it is the same as the last time
  7459. FDirViewMatchMask = Masks;
  7460. bool ImplicitMatch;
  7461. Matches =
  7462. // RecurseInclude parameter has no effect as the Path is empty
  7463. FDirViewMatchMask.Matches(FileName, Directory, UnicodeString(L""), &MaskParams, true, ImplicitMatch) &&
  7464. (AllowImplicitMatches || !ImplicitMatch);
  7465. }
  7466. //---------------------------------------------------------------------------
  7467. void __fastcall TCustomScpExplorerForm::DirViewGetOverlay(
  7468. TObject * Sender, TListItem * Item, WORD & Indexes)
  7469. {
  7470. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  7471. UnicodeString Ext;
  7472. if (DirView == RemoteDirView)
  7473. {
  7474. Ext = UnixExtractFileExt(DirView->ItemFileName(Item));
  7475. }
  7476. else
  7477. {
  7478. Ext = ExtractFileExt(DirView->ItemFileName(Item));
  7479. }
  7480. if (SameText(Ext, Configuration->PartialExt))
  7481. {
  7482. Indexes |= oiPartial;
  7483. }
  7484. }
  7485. //---------------------------------------------------------------------------
  7486. bool __fastcall TCustomScpExplorerForm::CanPasteToDirViewFromClipBoard()
  7487. {
  7488. return
  7489. DirViewEnabled(osCurrent) &&
  7490. DirView(osCurrent)->CanPasteFromClipBoard();
  7491. }
  7492. //---------------------------------------------------------------------------
  7493. bool __fastcall TCustomScpExplorerForm::CanPasteFromClipBoard()
  7494. {
  7495. bool Result = false;
  7496. if (CanPasteToDirViewFromClipBoard())
  7497. {
  7498. Result = true;
  7499. }
  7500. else
  7501. {
  7502. UnicodeString ClipboardText;
  7503. if (NonEmptyTextFromClipboard(ClipboardText))
  7504. {
  7505. if (StoredSessions->IsUrl(ClipboardText))
  7506. {
  7507. Result = true;
  7508. }
  7509. else
  7510. {
  7511. Result = DirViewEnabled(osCurrent);
  7512. }
  7513. }
  7514. }
  7515. return Result;
  7516. }
  7517. //---------------------------------------------------------------------------
  7518. void __fastcall TCustomScpExplorerForm::PasteFromClipBoard()
  7519. {
  7520. if (DoesClipboardContainOurFiles())
  7521. {
  7522. if (DebugAlwaysTrue(CanPasteToDirViewFromClipBoard()))
  7523. {
  7524. TTerminalManager * Manager = TTerminalManager::Instance();
  7525. TTerminal * TergetTerminal = Manager->ActiveTerminal;
  7526. Manager->ActiveTerminal = FClipboardTerminal;
  7527. ExecuteFileOperation(foRemoteCopy, osRemote, FClipboardFileList.get(), false, TergetTerminal);
  7528. }
  7529. }
  7530. else if (CanPasteToDirViewFromClipBoard())
  7531. {
  7532. DirView(osCurrent)->PasteFromClipBoard();
  7533. }
  7534. else
  7535. {
  7536. UnicodeString ClipboardText;
  7537. if (NonEmptyTextFromClipboard(ClipboardText))
  7538. {
  7539. if (StoredSessions->IsUrl(ClipboardText))
  7540. {
  7541. NewSession(false, ClipboardText);
  7542. }
  7543. else
  7544. {
  7545. DirView(osCurrent)->Path = ClipboardText;
  7546. }
  7547. }
  7548. }
  7549. }
  7550. //---------------------------------------------------------------------------
  7551. void __fastcall TCustomScpExplorerForm::FileListFromClipboard()
  7552. {
  7553. // TBD
  7554. }
  7555. //---------------------------------------------------------------------------
  7556. void __fastcall TCustomScpExplorerForm::SelectAll(TOperationSide Side, TSelectMode Mode)
  7557. {
  7558. TCustomDirView * ADirView = DirView(Side);
  7559. ADirView->SelectAll(Mode);
  7560. ADirView->SetFocus();
  7561. }
  7562. //---------------------------------------------------------------------------
  7563. void __fastcall TCustomScpExplorerForm::SelectByMask(TOperationSide Side, bool Select)
  7564. {
  7565. TCustomDirView * ADirView = DirView(Side);
  7566. TFileFilter Filter;
  7567. DefaultFileFilter(Filter);
  7568. if (DoSelectMaskDialog(ADirView, Select, &Filter, Configuration))
  7569. {
  7570. Configuration->Usage->Inc(L"MaskSelections");
  7571. ADirView->SelectFiles(Filter, Select);
  7572. ADirView->SetFocus();
  7573. }
  7574. }
  7575. //---------------------------------------------------------------------------
  7576. void __fastcall TCustomScpExplorerForm::RestoreSelectedNames(TOperationSide Side)
  7577. {
  7578. TCustomDirView * ADirView = DirView(Side);
  7579. ADirView->RestoreSelectedNames();
  7580. ADirView->SetFocus();
  7581. }
  7582. //---------------------------------------------------------------------------
  7583. void __fastcall TCustomScpExplorerForm::SelectSameExt(bool Select)
  7584. {
  7585. TCustomDirView * CurrentDirView = DirView(osCurrent);
  7586. if (DebugAlwaysTrue(CurrentDirView->ItemFocused != NULL))
  7587. {
  7588. UnicodeString FileName = CurrentDirView->ItemFileName(CurrentDirView->ItemFocused);
  7589. UnicodeString Ext;
  7590. if (GetSide(osCurrent) == osRemote)
  7591. {
  7592. Ext = UnixExtractFileExt(FileName);
  7593. }
  7594. else
  7595. {
  7596. Ext = ExtractFileExt(FileName);
  7597. }
  7598. if (Ext.IsEmpty())
  7599. {
  7600. Ext = L".";
  7601. }
  7602. TFileFilter Filter;
  7603. Filter.Masks = FORMAT(L"*%s", (Ext));
  7604. Filter.Directories = false;
  7605. CurrentDirView->SelectFiles(Filter, Select);
  7606. }
  7607. }
  7608. //---------------------------------------------------------------------------
  7609. UnicodeString __fastcall TCustomScpExplorerForm::FileStatusBarText(
  7610. const TStatusFileInfo & FileInfo, TOperationSide Side)
  7611. {
  7612. UnicodeString Result;
  7613. if ((Side == osRemote) && (Terminal == NULL))
  7614. {
  7615. // noop
  7616. }
  7617. else
  7618. {
  7619. Result =
  7620. FMTLOAD(FILE_INFO_FORMAT,
  7621. (FormatBytes(FileInfo.SelectedSize),
  7622. FormatBytes(FileInfo.FilesSize),
  7623. FormatNumber(FileInfo.SelectedCount),
  7624. FormatNumber(FileInfo.FilesCount)));
  7625. }
  7626. return Result;
  7627. }
  7628. //---------------------------------------------------------------------------
  7629. void __fastcall TCustomScpExplorerForm::FileStatusBarPanelClick(
  7630. TTBXStatusPanel * Panel, TOperationSide Side)
  7631. {
  7632. if (Panel->Index == 1)
  7633. {
  7634. ToggleShowHiddenFiles();
  7635. }
  7636. else if (Panel->Index == 2)
  7637. {
  7638. Filter(Side);
  7639. }
  7640. }
  7641. //---------------------------------------------------------------------------
  7642. void __fastcall TCustomScpExplorerForm::UpdateFileStatusBar(
  7643. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo, TOperationSide Side)
  7644. {
  7645. DebugAssert(!StatusBar->SimplePanel);
  7646. StatusBar->Panels->Items[0]->Caption = FileStatusBarText(FileInfo, Side);
  7647. UpdateFileStatusExtendedPanels(StatusBar, FileInfo);
  7648. }
  7649. //---------------------------------------------------------------------------
  7650. void __fastcall TCustomScpExplorerForm::UpdateFileStatusExtendedPanels(
  7651. TTBXStatusBar * StatusBar, const TStatusFileInfo & FileInfo)
  7652. {
  7653. DebugAssert(StatusBar->Panels->Count >= 3);
  7654. TTBXStatusPanel * HiddenFilesPanel = StatusBar->Panels->Items[1];
  7655. if (FileInfo.HiddenCount > 0)
  7656. {
  7657. HiddenFilesPanel->Caption = FMTLOAD(FILE_INFO_HIDDEN2, (FormatNumber(FileInfo.HiddenCount)));
  7658. HiddenFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7659. }
  7660. else
  7661. {
  7662. HiddenFilesPanel->ViewPriority = 0;
  7663. // not really necessary, just to cleanup no-longer-valid data
  7664. HiddenFilesPanel->Caption = L"";
  7665. }
  7666. TTBXStatusPanel * FilteredFilesPanel = StatusBar->Panels->Items[2];
  7667. if (FileInfo.FilteredCount > 0)
  7668. {
  7669. FilteredFilesPanel->Caption = FMTLOAD(FILE_INFO_FILTERED2, (FormatNumber(FileInfo.FilteredCount)));
  7670. FilteredFilesPanel->ViewPriority = 90; // <100 allows hiding panel when it does not fit
  7671. }
  7672. else
  7673. {
  7674. FilteredFilesPanel->ViewPriority = 0;
  7675. // not really necessary, just to cleanup no-longer-valid data
  7676. FilteredFilesPanel->Caption = L"";
  7677. }
  7678. }
  7679. //---------------------------------------------------------------------------
  7680. void __fastcall TCustomScpExplorerForm::RemoteStatusBarClick(
  7681. TObject * /*Sender*/)
  7682. {
  7683. if (RemoteDirView->Enabled)
  7684. {
  7685. RemoteDirView->SetFocus();
  7686. }
  7687. }
  7688. //---------------------------------------------------------------------------
  7689. void __fastcall TCustomScpExplorerForm::ToggleQueueVisibility()
  7690. {
  7691. TQueueViewConfiguration Config = WinConfiguration->QueueView;
  7692. switch (Config.Show)
  7693. {
  7694. case qvShow:
  7695. if ((FQueueStatus != NULL) && (FQueueStatus->Count > 0))
  7696. {
  7697. Config.Show = qvHide;
  7698. }
  7699. else
  7700. {
  7701. Config.Show = Config.LastHideShow;
  7702. }
  7703. break;
  7704. case qvHideWhenEmpty:
  7705. if (ComponentVisible[fcQueueView])
  7706. {
  7707. Config.Show = qvHide;
  7708. }
  7709. else
  7710. {
  7711. Config.LastHideShow = Config.Show;
  7712. Config.Show = qvShow;
  7713. }
  7714. break;
  7715. case qvHide:
  7716. Config.LastHideShow = Config.Show;
  7717. Config.Show = qvShow;
  7718. break;
  7719. }
  7720. WinConfiguration->QueueView = Config;
  7721. }
  7722. //---------------------------------------------------------------------------
  7723. UnicodeString __fastcall TCustomScpExplorerForm::PathForCaption()
  7724. {
  7725. UnicodeString Result;
  7726. if (FTerminal != NULL)
  7727. {
  7728. switch (WinConfiguration->PathInCaption)
  7729. {
  7730. case picShort:
  7731. Result = TTerminalManager::Instance()->GetTerminalShortPath(FTerminal);
  7732. break;
  7733. case picFull:
  7734. Result = FTerminal->CurrentDirectory;
  7735. break;
  7736. }
  7737. }
  7738. return Result;
  7739. }
  7740. //---------------------------------------------------------------------------
  7741. void __fastcall TCustomScpExplorerForm::UpdateControls()
  7742. {
  7743. TTerminalManager::Instance()->UpdateAppTitle();
  7744. // WORAKRDOUND: Disabling list view when it is not showing yet does not set its
  7745. // background to gray on Windows 7 (works on Windows 10).
  7746. // See also EnableControl
  7747. if (Showing)
  7748. {
  7749. bool HasTerminal = (Terminal != NULL) && Terminal->Active;
  7750. if (HasTerminal)
  7751. {
  7752. // Update path when it changes
  7753. if ((SessionsPageControl->ActivePage != NULL) && (GetSessionTabTerminal(SessionsPageControl->ActivePage) == Terminal))
  7754. {
  7755. UpdateSessionTab(SessionsPageControl->ActivePage);
  7756. }
  7757. if (!RemoteDirView->Enabled)
  7758. {
  7759. RemoteDirView->Enabled = true;
  7760. if (FRemoteDirViewWasFocused)
  7761. {
  7762. ActiveControl = RemoteDirView;
  7763. }
  7764. }
  7765. RemoteDriveView->Enabled = true;
  7766. RemoteDirView->Color = (FSessionColor != 0 ? FSessionColor : clWindow);
  7767. RemoteDriveView->Color = RemoteDirView->Color;
  7768. }
  7769. else
  7770. {
  7771. if (RemoteDirView->Enabled)
  7772. {
  7773. // This is first called when the form is being constructed
  7774. // (not anymore due to Showing test above)
  7775. // but the false is overriden in the constructor later.
  7776. // An even later in TScpCommanderForm::DoShow()
  7777. FRemoteDirViewWasFocused = (ActiveControl == RemoteDirView);
  7778. EnableControl(RemoteDirView, false);
  7779. }
  7780. EnableControl(RemoteDriveView, false);
  7781. }
  7782. EnableControl(QueueView3, HasTerminal && Terminal->IsCapable[fsBackgroundTransfers]);
  7783. QueueLabelUpdateStatus();
  7784. reinterpret_cast<TTBCustomItem *>(GetComponent(fcRemotePathComboBox))->Enabled = HasTerminal;
  7785. }
  7786. }
  7787. //---------------------------------------------------------------------------
  7788. void __fastcall TCustomScpExplorerForm::DoDirViewLoaded(TCustomDirView * /*Sender*/)
  7789. {
  7790. UpdateControls();
  7791. }
  7792. //---------------------------------------------------------------------------
  7793. void __fastcall TCustomScpExplorerForm::DirViewLoaded(
  7794. TObject * Sender)
  7795. {
  7796. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  7797. DebugAssert(DirView != NULL);
  7798. DoDirViewLoaded(DirView);
  7799. TransferPresetAutoSelect();
  7800. }
  7801. //---------------------------------------------------------------------------
  7802. void __fastcall TCustomScpExplorerForm::StartUpdates()
  7803. {
  7804. TUpdatesConfiguration Updates = WinConfiguration->Updates;
  7805. // first run after installation
  7806. if (double(Updates.LastCheck) == 0)
  7807. {
  7808. // make sure next time there will be an update (if enabled)
  7809. Updates.LastCheck = TDateTime(1);
  7810. WinConfiguration->Updates = Updates;
  7811. }
  7812. else if ((double(Updates.Period) > 0) &&
  7813. (Now() - Updates.LastCheck >= Updates.Period))
  7814. {
  7815. TThreadMethod OnUpdatesChecked = NULL;
  7816. if (!IsUWP())
  7817. {
  7818. OnUpdatesChecked = UpdatesChecked;
  7819. }
  7820. StartUpdateThread(OnUpdatesChecked);
  7821. }
  7822. }
  7823. //---------------------------------------------------------------------------
  7824. void __fastcall TCustomScpExplorerForm::UpdatesChecked()
  7825. {
  7826. UnicodeString Message;
  7827. bool New;
  7828. TQueryType Type;
  7829. GetUpdatesMessage(Message, New, Type, false);
  7830. if (!Message.IsEmpty())
  7831. {
  7832. if (New)
  7833. {
  7834. Message = FMTLOAD(NEW_VERSION_CLICK, (Message));
  7835. }
  7836. if (!New && (Type != qtWarning))
  7837. {
  7838. PostNote(UnformatMessage(Message), 0, UpdatesNoteClicked, NULL);
  7839. }
  7840. else
  7841. {
  7842. Notify(NULL, Message, Type, true, UpdatesNoteClicked);
  7843. }
  7844. if (New)
  7845. {
  7846. Configuration->Usage->Inc(L"UpdateNotifications");
  7847. }
  7848. }
  7849. }
  7850. //---------------------------------------------------------------------------
  7851. void __fastcall TCustomScpExplorerForm::UpdatesNoteClicked(TObject * /*Sender*/)
  7852. {
  7853. RestoreApp();
  7854. bool CanDisplay = !NonVisualDataModule->Busy;
  7855. if (!CanDisplay && (Screen->ActiveForm != NULL))
  7856. {
  7857. CanDisplay = (Screen->ActiveForm->Perform(WM_CAN_DISPLAY_UPDATES, 0, 0) != 0);
  7858. }
  7859. if (CanDisplay)
  7860. {
  7861. Configuration->Usage->Inc(L"UpdateNotificationsClicked");
  7862. CheckForUpdates(true);
  7863. }
  7864. else
  7865. {
  7866. Configuration->Usage->Inc(L"UpdateNotificationsBusy");
  7867. }
  7868. }
  7869. //---------------------------------------------------------------------------
  7870. void __fastcall TCustomScpExplorerForm::GetTransferPresetAutoSelectData(
  7871. TCopyParamRuleData & Data)
  7872. {
  7873. DebugAssert(Terminal != NULL);
  7874. Data.HostName = Terminal->SessionData->HostNameExpanded;
  7875. Data.UserName = Terminal->SessionData->UserNameExpanded;
  7876. Data.RemoteDirectory = RemoteDirView->PathName;
  7877. }
  7878. //---------------------------------------------------------------------------
  7879. void __fastcall TCustomScpExplorerForm::TransferPresetAutoSelect()
  7880. {
  7881. // Terminal can be null when we are changing local directory implicitly,
  7882. // because it has been deleted, while no session is connected
  7883. // (Login dialog is open)
  7884. if (FAllowTransferPresetAutoSelect && (Terminal != NULL))
  7885. {
  7886. TCopyParamRuleData Data;
  7887. GetTransferPresetAutoSelectData(Data);
  7888. int CopyParamIndex = GUIConfiguration->CopyParamList->Find(Data);
  7889. UnicodeString CopyParamCurrent = GUIConfiguration->CopyParamCurrent;
  7890. if (CopyParamIndex < 0)
  7891. {
  7892. // there is no preset that matches autoselection
  7893. // set preset that we consider "default"
  7894. FCopyParamAutoSelected = L""; // forget last autoselected preset
  7895. GUIConfiguration->CopyParamCurrent = FCopyParamDefault;
  7896. }
  7897. else
  7898. {
  7899. // there is preset matching autoselection
  7900. UnicodeString CopyParamName = GUIConfiguration->CopyParamList->Names[CopyParamIndex];
  7901. if (CopyParamName == FCopyParamAutoSelected)
  7902. {
  7903. // autoselected the same preset as the last time
  7904. // make no change (i.e. preserve custom user preset, if any)
  7905. }
  7906. else
  7907. {
  7908. // autoselected the different preset then the last time (or there
  7909. // was default preset set)
  7910. FCopyParamAutoSelected = CopyParamName; // remember autoselection
  7911. GUIConfiguration->CopyParamCurrent = CopyParamName;
  7912. }
  7913. }
  7914. if (GUIConfiguration->CopyParamCurrent != CopyParamCurrent)
  7915. {
  7916. if (CopyParamIndex >= 0)
  7917. {
  7918. Configuration->Usage->Inc(L"CopyParamAutoSelects");
  7919. }
  7920. TTransferPresetNoteData * Data = new TTransferPresetNoteData;
  7921. try
  7922. {
  7923. int Fmt =
  7924. (CopyParamIndex < 0) ?
  7925. (GUIConfiguration->CopyParamIndex < 0 ? COPY_PARAM_DEFAULT_NORM : COPY_PARAM_DEFAULT_CUSTOM) :
  7926. COPY_PARAM_AUTOSELECTED;
  7927. UnicodeString Message = FMTLOAD(Fmt, (StripHotkey(GUIConfiguration->CopyParamCurrent)));
  7928. Data->Message = MainInstructions(Message);
  7929. int CopyParamAttrs = Terminal->UsableCopyParamAttrs(0).General;
  7930. UnicodeString Info = GUIConfiguration->CurrentCopyParam.GetInfoStr(L"\n",
  7931. CopyParamAttrs);
  7932. if (CopyParamIndex >= 0)
  7933. {
  7934. DebugAssert(GUIConfiguration->CopyParamList->Rules[CopyParamIndex] != NULL);
  7935. Info = FORMAT(L"%s\n \n%s", (Info,
  7936. FMTLOAD(COPY_PARAM_RULE,
  7937. (GUIConfiguration->CopyParamList->Rules[CopyParamIndex]->GetInfoStr(L"\n")))));
  7938. }
  7939. Data->Message += L"\n\n" + Info;
  7940. if (WinConfiguration->CopyParamAutoSelectNotice)
  7941. {
  7942. TransferPresetNoteMessage(Data, true);
  7943. }
  7944. else
  7945. {
  7946. PostNote(Message, 0, TransferPresetNoteClicked, Data);
  7947. Data = NULL; // ownership passed
  7948. }
  7949. }
  7950. __finally
  7951. {
  7952. delete Data;
  7953. }
  7954. }
  7955. }
  7956. }
  7957. //---------------------------------------------------------------------------
  7958. void __fastcall TCustomScpExplorerForm::TransferPresetNoteMessage(
  7959. TTransferPresetNoteData * NoteData, bool AllowNeverAskAgain)
  7960. {
  7961. DebugAssert(NoteData != NULL);
  7962. TMessageParams Params(AllowNeverAskAgain ? mpNeverAskAgainCheck : 0);
  7963. TQueryButtonAlias Aliases[1];
  7964. Aliases[0].Button = qaIgnore; // "ignore" is after "ok"
  7965. Aliases[0].Alias = LoadStr(CONFIGURE_BUTTON);
  7966. Params.Aliases = Aliases;
  7967. Params.AliasesCount = LENOF(Aliases);
  7968. unsigned int Result =
  7969. MoreMessageDialog(NoteData->Message, NULL, qtInformation,
  7970. qaOK | qaIgnore, HELP_COPY_PARAM_AUTOSELECTED, &Params);
  7971. switch (Result)
  7972. {
  7973. case qaNeverAskAgain:
  7974. DebugAssert(AllowNeverAskAgain);
  7975. WinConfiguration->CopyParamAutoSelectNotice = false;
  7976. break;
  7977. case qaIgnore:
  7978. PreferencesDialog(pmPresets);
  7979. break;
  7980. }
  7981. }
  7982. //---------------------------------------------------------------------------
  7983. void __fastcall TCustomScpExplorerForm::TransferPresetNoteClicked(TObject * Sender)
  7984. {
  7985. // as of now useless, as this is used for notes only, never for balloons, ...
  7986. RestoreApp();
  7987. // .. and we should never be busy here
  7988. if (DebugAlwaysTrue(!NonVisualDataModule->Busy))
  7989. {
  7990. TransferPresetNoteMessage(DebugNotNull(dynamic_cast<TTransferPresetNoteData *>(Sender)), false);
  7991. }
  7992. }
  7993. //---------------------------------------------------------------------------
  7994. void __fastcall TCustomScpExplorerForm::PreferencesDialog(
  7995. TPreferencesMode APreferencesMode)
  7996. {
  7997. std::unique_ptr<TPreferencesDialogData> PreferencesData;
  7998. TCopyParamRuleData Data;
  7999. if (Terminal != NULL)
  8000. {
  8001. PreferencesData.reset(new TPreferencesDialogData());
  8002. GetTransferPresetAutoSelectData(Data);
  8003. PreferencesData->CopyParamRuleData = &Data;
  8004. }
  8005. DoPreferencesDialog(APreferencesMode, PreferencesData.get());
  8006. }
  8007. //---------------------------------------------------------------------------
  8008. void __fastcall TCustomScpExplorerForm::AdHocCustomCommandValidate(
  8009. const TCustomCommandType & Command)
  8010. {
  8011. if (CustomCommandState(Command, FEditingFocusedAdHocCommand, ccltAll) <= 0)
  8012. {
  8013. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (Command.Command)));
  8014. }
  8015. }
  8016. //---------------------------------------------------------------------------
  8017. void __fastcall TCustomScpExplorerForm::AdHocCustomCommand(bool OnFocused)
  8018. {
  8019. bool RemoteAllowed = CustomCommandRemoteAllowed();
  8020. TCustomCommandType Command;
  8021. // make sure we use local custom command when remote are not supported
  8022. if (RemoteAllowed || FLAGSET(FLastCustomCommand.Params, ccLocal))
  8023. {
  8024. Command = FLastCustomCommand;
  8025. }
  8026. else
  8027. {
  8028. Command.Params = Command.Params | ccLocal;
  8029. }
  8030. Command.Name = LoadStr(CUSTOM_COMMAND_AD_HOC_NAME);
  8031. FEditingFocusedAdHocCommand = OnFocused;
  8032. bool LocalSide = (FCurrentSide == osLocal);
  8033. int Options =
  8034. FLAGMASK((!RemoteAllowed || LocalSide), ccoDisableRemote) |
  8035. FLAGMASK(LocalSide, ccoDisableRemoteFiles);
  8036. if (DoCustomCommandDialog(Command, WinConfiguration->CustomCommandList,
  8037. ccmAdHoc, Options, AdHocCustomCommandValidate, NULL))
  8038. {
  8039. FLastCustomCommand = Command;
  8040. UpdateCustomCommandsToolbar();
  8041. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &Command);
  8042. }
  8043. }
  8044. //---------------------------------------------------------------------------
  8045. void __fastcall TCustomScpExplorerForm::LastCustomCommand(bool OnFocused)
  8046. {
  8047. DebugAssert(!FLastCustomCommand.Command.IsEmpty());
  8048. int State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8049. DebugAssert(State > 0);
  8050. if (State <= 0)
  8051. {
  8052. throw Exception(FMTLOAD(CUSTOM_COMMAND_IMPOSSIBLE, (FLastCustomCommand.Command)));
  8053. }
  8054. ExecuteFileOperation(foCustomCommand, osRemote, OnFocused, false, &FLastCustomCommand);
  8055. }
  8056. //---------------------------------------------------------------------------
  8057. bool __fastcall TCustomScpExplorerForm::GetLastCustomCommand(bool OnFocused,
  8058. TCustomCommandType & Command, int & State)
  8059. {
  8060. bool Result = !FLastCustomCommand.Command.IsEmpty();
  8061. if (Result)
  8062. {
  8063. Command = FLastCustomCommand;
  8064. State = CustomCommandState(FLastCustomCommand, OnFocused, ccltAll);
  8065. }
  8066. return Result;
  8067. }
  8068. //---------------------------------------------------------------------------
  8069. void __fastcall TCustomScpExplorerForm::BeforeAction()
  8070. {
  8071. if (RemoteDirView->ItemFocused != NULL)
  8072. {
  8073. RemoteDirView->ItemFocused->CancelEdit();
  8074. }
  8075. }
  8076. //---------------------------------------------------------------------------
  8077. void __fastcall TCustomScpExplorerForm::PostComponentHide(Byte Component)
  8078. {
  8079. DebugAssert(ComponentVisible[Component]);
  8080. PostMessage(Handle, WM_COMPONENT_HIDE, Component, 0);
  8081. }
  8082. //---------------------------------------------------------------------------
  8083. void __fastcall TCustomScpExplorerForm::QueueSplitterDblClick(TObject * /*Sender*/)
  8084. {
  8085. // when queue panel is resized here directly, the status bar is stretched
  8086. // over whole space the panel occupied
  8087. PostComponentHide(fcQueueView);
  8088. }
  8089. //---------------------------------------------------------------------------
  8090. void __fastcall TCustomScpExplorerForm::Dispatch(void * Message)
  8091. {
  8092. TMessage * M = static_cast<TMessage*>(Message);
  8093. switch (M->Msg)
  8094. {
  8095. case CM_DIALOGCHAR:
  8096. CMDialogChar(*M);
  8097. break;
  8098. case WM_APPCOMMAND:
  8099. WMAppCommand(*M);
  8100. break;
  8101. case WM_SYSCOMMAND:
  8102. WMSysCommand(*M);
  8103. break;
  8104. case WM_QUERYENDSESSION:
  8105. WMQueryEndSession(*M);
  8106. break;
  8107. case WM_ENDSESSION:
  8108. WMEndSession(*reinterpret_cast<TWMEndSession *>(M));
  8109. break;
  8110. #ifdef _DEBUG
  8111. case WM_WINDOWPOSCHANGED:
  8112. WMWindowPosChanged(*reinterpret_cast<TWMWindowPosMsg *>(M));
  8113. break;
  8114. case CM_CUSTOMSTYLECHANGED:
  8115. TForm::Dispatch(Message);
  8116. break;
  8117. #endif
  8118. case WM_COMPONENT_HIDE:
  8119. {
  8120. Byte Component = static_cast<Byte>(M->WParam);
  8121. // sanity check
  8122. if (ComponentVisible[Component])
  8123. {
  8124. // special treatment
  8125. if (Component == fcQueueView)
  8126. {
  8127. ToggleQueueVisibility();
  8128. DebugAssert(!ComponentVisible[fcQueueView]);
  8129. }
  8130. else
  8131. {
  8132. ComponentVisible[Component] = false;
  8133. }
  8134. }
  8135. }
  8136. break;
  8137. case WM_COPYDATA:
  8138. WMCopyData(*M);
  8139. break;
  8140. case WM_MANAGES_CAPTION:
  8141. // caption managed in TTerminalManager::UpdateAppTitle()
  8142. M->Result = 1;
  8143. break;
  8144. case WM_WANTS_MOUSEWHEEL:
  8145. M->Result = 1;
  8146. break;
  8147. case CM_SHOWINGCHANGED:
  8148. CMShowingChanged(*M);
  8149. break;
  8150. case WM_CLOSE:
  8151. WMClose(*M);
  8152. break;
  8153. case WM_DPICHANGED:
  8154. WMDpiChanged(*M);
  8155. break;
  8156. case CM_DPICHANGED:
  8157. CMDpiChanged(*M);
  8158. break;
  8159. default:
  8160. TForm::Dispatch(Message);
  8161. break;
  8162. }
  8163. }
  8164. //---------------------------------------------------------------------------
  8165. void __fastcall TCustomScpExplorerForm::UpdateImages()
  8166. {
  8167. // noop
  8168. }
  8169. //---------------------------------------------------------------------------
  8170. void __fastcall TCustomScpExplorerForm::CMDpiChanged(TMessage & Message)
  8171. {
  8172. TForm::Dispatch(&Message);
  8173. FSessionColors->Clear();
  8174. AddFixedSessionImages();
  8175. RegenerateSessionColorsImageList(FSessionColors, FSessionColorMaskImageIndex);
  8176. UpdateImages();
  8177. }
  8178. //---------------------------------------------------------------------------
  8179. void __fastcall TCustomScpExplorerForm::WMDpiChanged(TMessage & Message)
  8180. {
  8181. TForm::Dispatch(&Message);
  8182. Configuration->Usage->Inc(L"PixelsPerInchChanged");
  8183. UpdatePixelsPerInchMainWindowCounter();
  8184. }
  8185. //---------------------------------------------------------------------------
  8186. void __fastcall TCustomScpExplorerForm::WMClose(TMessage & Message)
  8187. {
  8188. // Cannot close window while we are busy.
  8189. // We cannot test this in FormCloseQuery as that is called also from
  8190. // Close(), which is called by CloseApplicationAction. So we can be busy
  8191. // there already even, when it is legitimate to close the application.
  8192. // Possibly a better place to handle this would be WMSysCommand.
  8193. if (NonVisualDataModule->Busy)
  8194. {
  8195. Message.Result = 1;
  8196. }
  8197. else
  8198. {
  8199. TForm::Dispatch(&Message);
  8200. }
  8201. }
  8202. //---------------------------------------------------------------------------
  8203. void __fastcall TCustomScpExplorerForm::CMShowingChanged(TMessage & Message)
  8204. {
  8205. TForm::Dispatch(&Message);
  8206. if (Showing && (Terminal == NULL))
  8207. {
  8208. // When we are starting minimized (i.e. from an installer),
  8209. // postpone showing Login dialog until we get restored.
  8210. // Otherwise the Login dialog (and Authentication window) show restored
  8211. // over invidible (minimized) main window.
  8212. if (WindowState == wsMinimized)
  8213. {
  8214. FNeedSession = true;
  8215. }
  8216. else
  8217. {
  8218. // This happens before application ever goes idle, so the toolbars would
  8219. // stay enabled (initial state) until the Login dialog is dismissed.
  8220. UpdateActions();
  8221. NonVisualDataModule->StartBusy();
  8222. try
  8223. {
  8224. // Need to process WM_ACTIVATEAPP before showing the Login dialog,
  8225. // otherwise the dialog does not receive focus.
  8226. // With Commander interface the ProcessMessages is called already
  8227. // by TDriveView, but with Explorer interface, we need to call it explicily
  8228. Application->ProcessMessages();
  8229. // do not reload sessions, they have been loaded just now (optimization)
  8230. NeedSession(false);
  8231. }
  8232. __finally
  8233. {
  8234. NonVisualDataModule->EndBusy();
  8235. }
  8236. }
  8237. }
  8238. }
  8239. //---------------------------------------------------------------------------
  8240. void __fastcall TCustomScpExplorerForm::FormConstrainedResize(
  8241. TObject * /*Sender*/, int & MinWidth, int & MinHeight, int & MaxWidth,
  8242. int & MaxHeight)
  8243. {
  8244. // workaround for bug in TWinControl.CalcConstraints
  8245. // Check for empty rect (restore from iconic state) is done there only after
  8246. // call to AdjustClientRect, which enlarges the rect (for forms).
  8247. TRect R = GetClientRect();
  8248. // when restoring from iconic state, place no restrictions
  8249. if (IsRectEmpty(R))
  8250. {
  8251. MinWidth = 0;
  8252. MinHeight = 0;
  8253. MaxWidth = 32000;
  8254. MaxHeight = 32000;
  8255. }
  8256. }
  8257. //---------------------------------------------------------------------------
  8258. void __fastcall TCustomScpExplorerForm::GetSpaceAvailable(const UnicodeString Path,
  8259. TSpaceAvailable & ASpaceAvailable, bool & Close)
  8260. {
  8261. // terminal can be already closed (e.g. dropped connection)
  8262. if ((Terminal != NULL) && Terminal->IsCapable[fcCheckingSpaceAvailable])
  8263. {
  8264. Configuration->Usage->Inc(L"SpaceAvailableChecks");
  8265. try
  8266. {
  8267. Terminal->SpaceAvailable(Path, ASpaceAvailable);
  8268. }
  8269. catch(...)
  8270. {
  8271. if (!Terminal->Active)
  8272. {
  8273. Close = true;
  8274. }
  8275. throw;
  8276. }
  8277. }
  8278. }
  8279. //---------------------------------------------------------------------------
  8280. void __fastcall TCustomScpExplorerForm::FileSystemInfo()
  8281. {
  8282. const TSessionInfo & SessionInfo = Terminal->GetSessionInfo();
  8283. const TFileSystemInfo & FileSystemInfo = Terminal->GetFileSystemInfo(true);
  8284. TGetSpaceAvailable OnGetSpaceAvailable = NULL;
  8285. if (Terminal->IsCapable[fcCheckingSpaceAvailable])
  8286. {
  8287. OnGetSpaceAvailable = GetSpaceAvailable;
  8288. }
  8289. DoFileSystemInfoDialog(SessionInfo, FileSystemInfo, Terminal->CurrentDirectory,
  8290. OnGetSpaceAvailable);
  8291. }
  8292. //---------------------------------------------------------------------------
  8293. TSessionData * __fastcall TCustomScpExplorerForm::SessionDataForCode()
  8294. {
  8295. std::unique_ptr<TSessionData> Data(CloneCurrentSessionData());
  8296. Terminal->FillSessionDataForCode(Data.get());
  8297. return Data.release();
  8298. }
  8299. //---------------------------------------------------------------------------
  8300. void __fastcall TCustomScpExplorerForm::GenerateUrl(TStrings * Paths)
  8301. {
  8302. std::unique_ptr<TSessionData> Data(SessionDataForCode());
  8303. DoGenerateUrlDialog(Data.get(), Paths);
  8304. }
  8305. //---------------------------------------------------------------------------
  8306. void __fastcall TCustomScpExplorerForm::SessionGenerateUrl()
  8307. {
  8308. GenerateUrl(NULL);
  8309. }
  8310. //---------------------------------------------------------------------------
  8311. void __fastcall TCustomScpExplorerForm::FileGenerateUrl()
  8312. {
  8313. std::unique_ptr<TStrings> Paths(new TStringList());
  8314. DirView(osCurrent)->CreateFileList(false, true, Paths.get());
  8315. GenerateUrl(Paths.get());
  8316. }
  8317. //---------------------------------------------------------------------------
  8318. void __fastcall TCustomScpExplorerForm::SetSessionColor(TColor value)
  8319. {
  8320. if (value != FSessionColor)
  8321. {
  8322. FSessionColor = value;
  8323. TColor C = (value != 0 ? value : Vcl::Graphics::clNone);
  8324. TTBXColorItem * ColorItem = dynamic_cast<TTBXColorItem *>(
  8325. static_cast<TObject *>(GetComponent(fcColorMenu)));
  8326. DebugAssert(ColorItem != NULL);
  8327. ColorItem->Color = C;
  8328. NonVisualDataModule->ColorMenuItem->Color = C;
  8329. // Is null when called from LastTerminalClosed
  8330. if (Terminal != NULL)
  8331. {
  8332. SessionsPageControl->ActivePage->ImageIndex = AddSessionColor(value);
  8333. }
  8334. UpdateControls();
  8335. }
  8336. }
  8337. //---------------------------------------------------------------------------
  8338. bool __fastcall TCustomScpExplorerForm::CancelNote(bool Force)
  8339. {
  8340. bool Result = FNoteTimer->Enabled;
  8341. if (Result)
  8342. {
  8343. // cannot cancel note too early
  8344. bool NotEarly =
  8345. (Now() - FNoteShown >
  8346. EncodeTimeVerbose(0, 0, (unsigned short)(WinConfiguration->NotificationsStickTime), 0));
  8347. if (Force || NotEarly)
  8348. {
  8349. FNoteTimer->Enabled = false;
  8350. FNote = L"";
  8351. SAFE_DESTROY(FNoteData);
  8352. FOnNoteClick = NULL;
  8353. FNoteHints = FNotes->Text;
  8354. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8355. UpdateStatusBar();
  8356. }
  8357. }
  8358. return Result;
  8359. }
  8360. //---------------------------------------------------------------------------
  8361. void __fastcall TCustomScpExplorerForm::NoteTimer(TObject * /*Sender*/)
  8362. {
  8363. DebugAssert(FNoteTimer->Enabled);
  8364. CancelNote(true);
  8365. }
  8366. //---------------------------------------------------------------------------
  8367. void __fastcall TCustomScpExplorerForm::AddNote(UnicodeString Note, bool UpdateNow)
  8368. {
  8369. int P = Note.Pos(L"\n");
  8370. if (P > 0)
  8371. {
  8372. Note.SetLength(P - 1);
  8373. }
  8374. FNotes->Add(FORMAT(L"[%s] %s",
  8375. (FormatDateTime(Configuration->TimeFormat, Now()), Note)));
  8376. while (FNotes->Count > 10)
  8377. {
  8378. FNotes->Delete(0);
  8379. }
  8380. if (UpdateNow)
  8381. {
  8382. FNoteHints = FNotes->Text;
  8383. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8384. UpdateStatusBar();
  8385. }
  8386. }
  8387. //---------------------------------------------------------------------------
  8388. void __fastcall TCustomScpExplorerForm::PostNote(UnicodeString Note,
  8389. unsigned int Seconds, TNotifyEvent OnNoteClick, TObject * NoteData)
  8390. {
  8391. int P = Note.Pos(L"\n");
  8392. if (P > 0)
  8393. {
  8394. Note.SetLength(P - 1);
  8395. }
  8396. FNoteHints = FNotes->Text;
  8397. FNoteHints.Delete(FNoteHints.Length() - 1, 2);
  8398. FNote = Note;
  8399. SAFE_DESTROY(FNoteData);
  8400. FNoteData = NoteData;
  8401. FOnNoteClick = OnNoteClick;
  8402. AddNote(Note, false);
  8403. UpdateStatusBar();
  8404. FNoteShown = Now();
  8405. FNoteTimer->Enabled = false;
  8406. if (Seconds == 0)
  8407. {
  8408. Seconds = WinConfiguration->NotificationsTimeout;
  8409. }
  8410. FNoteTimer->Interval = Seconds * MSecsPerSec;
  8411. FNoteTimer->Enabled = true;
  8412. }
  8413. //---------------------------------------------------------------------------
  8414. void __fastcall TCustomScpExplorerForm::ReadDirectoryCancelled()
  8415. {
  8416. PostNote(LoadStr(DIRECTORY_READING_CANCELLED), 0, NULL, NULL);
  8417. }
  8418. //---------------------------------------------------------------------------
  8419. void __fastcall TCustomScpExplorerForm::SynchronizeBrowsingChanged()
  8420. {
  8421. if (NonVisualDataModule->SynchronizeBrowsingAction->Checked)
  8422. {
  8423. Configuration->Usage->Inc(L"SynchronizeBrowsingEnabled");
  8424. }
  8425. PostNote(FORMAT(LoadStrPart(SYNC_DIR_BROWSE_TOGGLE, 1),
  8426. (LoadStrPart(SYNC_DIR_BROWSE_TOGGLE,
  8427. (NonVisualDataModule->SynchronizeBrowsingAction->Checked ? 2 : 3)))),
  8428. 0, NULL, NULL);
  8429. }
  8430. //---------------------------------------------------------------------------
  8431. void __fastcall TCustomScpExplorerForm::ToggleShowHiddenFiles()
  8432. {
  8433. WinConfiguration->ShowHiddenFiles = !WinConfiguration->ShowHiddenFiles;
  8434. PostNote(FORMAT(LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE, 1),
  8435. (LoadStrPart(SHOW_HIDDEN_FILES_TOGGLE,
  8436. (WinConfiguration->ShowHiddenFiles ? 2 : 3)))), 0, NULL, NULL);
  8437. }
  8438. //---------------------------------------------------------------------------
  8439. void __fastcall TCustomScpExplorerForm::SetFormatSizeBytes(TFormatBytesStyle Style)
  8440. {
  8441. WinConfiguration->FormatSizeBytes = Style;
  8442. }
  8443. //---------------------------------------------------------------------------
  8444. void __fastcall TCustomScpExplorerForm::ToggleAutoReadDirectoryAfterOp()
  8445. {
  8446. Configuration->AutoReadDirectoryAfterOp = !Configuration->AutoReadDirectoryAfterOp;
  8447. PostNote(FORMAT(LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE, 1),
  8448. (LoadStrPart(AUTO_READ_DIRECTORY_TOGGLE,
  8449. (Configuration->AutoReadDirectoryAfterOp ? 2 : 3)))), 0, NULL, NULL);
  8450. }
  8451. //---------------------------------------------------------------------------
  8452. void __fastcall TCustomScpExplorerForm::StatusBarPanelDblClick(
  8453. TTBXCustomStatusBar * Sender, TTBXStatusPanel * Panel)
  8454. {
  8455. if (Panel->Index == 0)
  8456. {
  8457. if (FOnNoteClick != NULL)
  8458. {
  8459. // prevent the user data from being freed by possible call
  8460. // to CancelNote or PostNote during call to OnNoteClick
  8461. std::unique_ptr<TObject> NoteData(FNoteData);
  8462. TNotifyEvent OnNoteClick = FOnNoteClick;
  8463. FNoteData = NULL;
  8464. // need to cancel the note as we are going to delete its user data
  8465. CancelNote(true);
  8466. OnNoteClick(NoteData.get());
  8467. }
  8468. }
  8469. if (Panel->Index >= Sender->Panels->Count - SessionPanelCount)
  8470. {
  8471. FileSystemInfo();
  8472. }
  8473. }
  8474. //---------------------------------------------------------------------------
  8475. void __fastcall TCustomScpExplorerForm::LockWindow(bool Force)
  8476. {
  8477. // workaround:
  8478. // 1) for unknown reason, disabling window, while minimized,
  8479. // prevents it from restoring, even if it was enabled again meanwhile
  8480. // 2) when disabling the main window, while another has focus
  8481. // minimize is no longer possible
  8482. // ("keep up to date" dialog - though does not seem to be the case with "find file" -
  8483. // so it has probably something to do with the semi-modalness of the "keep up to date" dialog)
  8484. // Shouldn't we use IsApplicationMinimized() here?
  8485. if ((FLockSuspendLevel == 0) && !IsIconic(Application->Handle) && (Force || (Screen->ActiveForm == this)))
  8486. {
  8487. Enabled = false;
  8488. }
  8489. FLockLevel++;
  8490. }
  8491. //---------------------------------------------------------------------------
  8492. void __fastcall TCustomScpExplorerForm::UnlockWindow()
  8493. {
  8494. DebugAssert(FLockLevel > 0);
  8495. FLockLevel--;
  8496. if (FLockLevel == 0)
  8497. {
  8498. DebugAssert(FLockSuspendLevel == 0);
  8499. Enabled = true;
  8500. }
  8501. }
  8502. //---------------------------------------------------------------------------
  8503. void __fastcall TCustomScpExplorerForm::SuspendWindowLock()
  8504. {
  8505. // We need to make sure that window is enabled when the last modal window closes
  8506. // otherwise focus is not restored correctly.
  8507. // So we re-enable the window when modal window opens and
  8508. // disable it back after is closes
  8509. if (FLockLevel > 0)
  8510. {
  8511. // while we have nesting counter, we know that we never be called
  8512. // recursivelly as Application->OnModalBegin is called only
  8513. // for the top-level modal window
  8514. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8515. {
  8516. // won't be disabled when conditions in LockWindow() were not satisfied
  8517. FDisabledOnLockSuspend = !Enabled;
  8518. // When minimized to tray (or actually when set to SW_HIDE),
  8519. // setting Enabled makes the window focusable even when there's
  8520. // modal window over it
  8521. if (!FTrayIcon->Visible)
  8522. {
  8523. Enabled = true;
  8524. }
  8525. }
  8526. FLockSuspendLevel++;
  8527. }
  8528. }
  8529. //---------------------------------------------------------------------------
  8530. void __fastcall TCustomScpExplorerForm::ResumeWindowLock()
  8531. {
  8532. if (FLockSuspendLevel > 0)
  8533. {
  8534. DebugAssert(FLockLevel > 0);
  8535. FLockSuspendLevel--;
  8536. // see comment in SuspendWindowLock
  8537. if (DebugAlwaysTrue(FLockSuspendLevel == 0))
  8538. {
  8539. // Note that window can be enabled here, when we were minized to tray when
  8540. // was SuspendWindowLock() called.
  8541. // We should possibly do the same check as in LockWindow(),
  8542. // if it is ever possible that the consitions change between
  8543. // SuspendWindowLock() and ResumeWindowLock()
  8544. Enabled = !FDisabledOnLockSuspend;
  8545. }
  8546. }
  8547. }
  8548. //---------------------------------------------------------------------------
  8549. void __fastcall TCustomScpExplorerForm::UpdateRemotePathComboBox(bool TextOnly)
  8550. {
  8551. if (!TextOnly)
  8552. {
  8553. TTBXComboBoxItem * RemotePathComboBox =
  8554. reinterpret_cast<TTBXComboBoxItem *>(GetComponent(fcRemotePathComboBox));
  8555. TStrings * Items = RemotePathComboBox->Strings;
  8556. Items->BeginUpdate();
  8557. try
  8558. {
  8559. Items->Clear();
  8560. if (Terminal != NULL)
  8561. {
  8562. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8563. while (!IsUnixRootPath(APath))
  8564. {
  8565. int P = APath.LastDelimiter(L'/');
  8566. DebugAssert(P >= 0);
  8567. Items->Insert(0, APath.SubString(P + 1, APath.Length() - P));
  8568. APath.SetLength(P - 1);
  8569. }
  8570. Items->Insert(0, Customunixdirview_SUnixDefaultRootName);
  8571. }
  8572. }
  8573. __finally
  8574. {
  8575. RemotePathComboBox->ItemIndex = Items->Count - 1;
  8576. // Setting ItemIndex to -1 does not reset its text
  8577. if (Items->Count == 0)
  8578. {
  8579. RemotePathComboBox->Text = L"";
  8580. }
  8581. Items->EndUpdate();
  8582. }
  8583. }
  8584. }
  8585. //---------------------------------------------------------------------------
  8586. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxAdjustImageIndex(
  8587. TTBXComboBoxItem * Sender, const UnicodeString /*AText*/, int AIndex,
  8588. int & ImageIndex)
  8589. {
  8590. if (AIndex < 0)
  8591. {
  8592. AIndex = Sender->ItemIndex;
  8593. }
  8594. ImageIndex = (AIndex < Sender->Strings->Count - 1 ? StdDirIcon : StdDirSelIcon);
  8595. }
  8596. //---------------------------------------------------------------------------
  8597. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxDrawItem(
  8598. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, TRect & ARect, int AIndex,
  8599. int /*AHoverIndex*/, bool & /*DrawDefault*/)
  8600. {
  8601. ARect.Left += (10 * AIndex);
  8602. }
  8603. //---------------------------------------------------------------------------
  8604. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxMeasureWidth(
  8605. TTBXCustomList * /*Sender*/, TCanvas * /*ACanvas*/, int AIndex, int &AWidth)
  8606. {
  8607. AWidth += (10 * AIndex);
  8608. }
  8609. //---------------------------------------------------------------------------
  8610. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxItemClick(
  8611. TObject * Sender)
  8612. {
  8613. TTBXComboBoxItem * RemotePathComboBox = dynamic_cast<TTBXComboBoxItem*>(Sender);
  8614. UnicodeString APath = UnixExcludeTrailingBackslash(RemoteDirView->Path);
  8615. int Index = RemotePathComboBox->ItemIndex;
  8616. while (Index < RemotePathComboBox->Strings->Count - 1)
  8617. {
  8618. APath = UnixExtractFileDir(APath);
  8619. Index++;
  8620. }
  8621. // VanDyke style paths
  8622. if (APath.IsEmpty())
  8623. {
  8624. DebugAssert(RemotePathComboBox->ItemIndex == 0);
  8625. APath = ROOTDIRECTORY;
  8626. }
  8627. if (RemoteDirView->Path != APath)
  8628. {
  8629. RemoteDirView->Path = APath;
  8630. }
  8631. }
  8632. //---------------------------------------------------------------------------
  8633. void __fastcall TCustomScpExplorerForm::RemotePathComboBoxCancel(TObject * Sender)
  8634. {
  8635. DebugAssert(Sender == GetComponent(fcRemotePathComboBox));
  8636. DebugUsedParam(Sender);
  8637. UpdateRemotePathComboBox(true);
  8638. }
  8639. //---------------------------------------------------------------------------
  8640. void __fastcall TCustomScpExplorerForm::DirViewEditing(
  8641. TObject * Sender, TListItem * Item, bool & /*AllowEdit*/)
  8642. {
  8643. TCustomDirView * DirView = dynamic_cast<TCustomDirView *>(Sender);
  8644. DebugAssert(DirView != NULL);
  8645. if (!WinConfiguration->RenameWholeName && !DirView->ItemIsDirectory(Item))
  8646. {
  8647. HWND Edit = ListView_GetEditControl(DirView->Handle);
  8648. // OnEditing is called also from TCustomListView::CanEdit
  8649. if (Edit != NULL)
  8650. {
  8651. EditSelectBaseName(Edit);
  8652. }
  8653. }
  8654. }
  8655. //---------------------------------------------------------------------------
  8656. //---------------------------------------------------------------------------
  8657. TDragDropFilesEx * __fastcall TCustomScpExplorerForm::CreateDragDropFilesEx()
  8658. {
  8659. TDragDropFilesEx * Result = new TDragDropFilesEx(this);
  8660. Result->AutoDetectDnD = false;
  8661. Result->NeedValid = TFileExMustDnDSet() << nvFilename;
  8662. Result->RenderDataOn = rdoEnterAndDropSync;
  8663. Result->TargetEffects = TDropEffectSet() << deCopy << deMove;
  8664. return Result;
  8665. }
  8666. //---------------------------------------------------------------------------
  8667. void __fastcall TCustomScpExplorerForm::CreateWnd()
  8668. {
  8669. TForm::CreateWnd();
  8670. if (FSessionsDragDropFilesEx == NULL)
  8671. {
  8672. FSessionsDragDropFilesEx = CreateDragDropFilesEx();
  8673. FSessionsDragDropFilesEx->OnDragOver = SessionsDDDragOver;
  8674. FSessionsDragDropFilesEx->OnProcessDropped = SessionsDDProcessDropped;
  8675. FSessionsDragDropFilesEx->OnDragEnter = SessionsDDDragEnter;
  8676. FSessionsDragDropFilesEx->OnDragLeave = SessionsDDDragLeave;
  8677. }
  8678. if (FQueueDragDropFilesEx == NULL)
  8679. {
  8680. FQueueDragDropFilesEx = CreateDragDropFilesEx();
  8681. // No need to set OnDragOver as we do not have any restrictions
  8682. FQueueDragDropFilesEx->OnProcessDropped = QueueDDProcessDropped;
  8683. FQueueDragDropFilesEx->OnDragEnter = QueueDDDragEnter;
  8684. FQueueDragDropFilesEx->OnDragLeave = QueueDDDragLeave;
  8685. }
  8686. }
  8687. //---------------------------------------------------------------------------
  8688. void __fastcall TCustomScpExplorerForm::DestroyWnd()
  8689. {
  8690. TForm::DestroyWnd();
  8691. FSessionsDragDropFilesEx->DragDropControl = NULL;
  8692. FQueueDragDropFilesEx->DragDropControl = NULL;
  8693. }
  8694. //---------------------------------------------------------------------------
  8695. void __fastcall TCustomScpExplorerForm::FormShow(TObject * /*Sender*/)
  8696. {
  8697. SideEnter(FCurrentSide);
  8698. FEverShown = true;
  8699. }
  8700. //---------------------------------------------------------------------------
  8701. void __fastcall TCustomScpExplorerForm::DoFindFiles(
  8702. TTerminal * ATerminal, UnicodeString Directory, const TFileMasks & FileMask,
  8703. TFileFoundEvent OnFileFound, TFindingFileEvent OnFindingFile)
  8704. {
  8705. if (!NonVisualDataModule->Busy)
  8706. {
  8707. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8708. Configuration->Usage->Inc(L"FileFinds");
  8709. LockWindow(true);
  8710. NonVisualDataModule->StartBusy();
  8711. WinConfiguration->LockedInterface = true;
  8712. try
  8713. {
  8714. FTerminal->FilesFind(Directory, FileMask, OnFileFound, OnFindingFile);
  8715. }
  8716. __finally
  8717. {
  8718. WinConfiguration->LockedInterface = false;
  8719. NonVisualDataModule->EndBusy();
  8720. UnlockWindow();
  8721. }
  8722. }
  8723. }
  8724. //---------------------------------------------------------------------------
  8725. void __fastcall TCustomScpExplorerForm::DoFocusRemotePath(TTerminal * ATerminal, const UnicodeString & Path)
  8726. {
  8727. if (!NonVisualDataModule->Busy)
  8728. {
  8729. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8730. SetFocus();
  8731. RemoteDirView->Path = UnixExtractFilePath(Path);
  8732. TListItem * Item = RemoteDirView->FindFileItem(UnixExtractFileName(Path));
  8733. if (Item != NULL)
  8734. {
  8735. RemoteDirView->ItemFocused = Item;
  8736. RemoteDirView->ItemFocused->MakeVisible(false);
  8737. RemoteDirView->SetFocus();
  8738. }
  8739. }
  8740. }
  8741. //---------------------------------------------------------------------------
  8742. bool __fastcall TCustomScpExplorerForm::CanOperateOnFoundFiles(TTerminal * ATerminal)
  8743. {
  8744. bool Result = !NonVisualDataModule->Busy;
  8745. if (Result)
  8746. {
  8747. TTerminalManager::Instance()->ActiveTerminal = ATerminal;
  8748. }
  8749. return Result;
  8750. }
  8751. //---------------------------------------------------------------------------
  8752. void __fastcall TCustomScpExplorerForm::DoOperationOnFoundFiles(
  8753. TFileOperation Operation, TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8754. {
  8755. if (CanOperateOnFoundFiles(ATerminal))
  8756. {
  8757. TValueRestorer<TFileOperationFinishedEvent> OnFileOperationFinishedRestorer(FOnFileOperationFinished);
  8758. FOnFileOperationFinished = OnFileOperationFinished;
  8759. ExecuteFileOperation(Operation, osRemote, FileList, false, NULL);
  8760. }
  8761. }
  8762. //---------------------------------------------------------------------------
  8763. void __fastcall TCustomScpExplorerForm::DoDeleteFoundFiles(
  8764. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8765. {
  8766. DoOperationOnFoundFiles(foDelete, ATerminal, FileList, OnFileOperationFinished);
  8767. }
  8768. //---------------------------------------------------------------------------
  8769. void __fastcall TCustomScpExplorerForm::DoDownloadFoundFiles(
  8770. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8771. {
  8772. DoOperationOnFoundFiles(foCopy, ATerminal, FileList, OnFileOperationFinished);
  8773. }
  8774. //---------------------------------------------------------------------------
  8775. void __fastcall TCustomScpExplorerForm::DoEditFoundFiles(
  8776. TTerminal * ATerminal, TStrings * FileList, TFileOperationFinishedEvent OnFileOperationFinished)
  8777. {
  8778. if (CanOperateOnFoundFiles(ATerminal))
  8779. {
  8780. for (int Index = 0; Index < FileList->Count; Index++)
  8781. {
  8782. UnicodeString FileName = FileList->Strings[Index];
  8783. TRemoteFile * File = static_cast<TRemoteFile *>(FileList->Objects[Index]);
  8784. ExecuteRemoteFile(FileName, File, efDefaultEditor);
  8785. OnFileOperationFinished(FileName, true);
  8786. }
  8787. }
  8788. }
  8789. //---------------------------------------------------------------------------
  8790. void __fastcall TCustomScpExplorerForm::RemoteFindFiles()
  8791. {
  8792. FFileFindTerminal = Terminal;
  8793. ShowFileFindDialog(
  8794. Terminal, RemoteDirView->Path, DoFindFiles, DoFocusRemotePath, DoDeleteFoundFiles,
  8795. DoDownloadFoundFiles, DoEditFoundFiles);
  8796. }
  8797. //---------------------------------------------------------------------------
  8798. void __fastcall TCustomScpExplorerForm::UpdateTaskbarList(ITaskbarList3 * TaskbarList)
  8799. {
  8800. FTaskbarList = TaskbarList;
  8801. }
  8802. //---------------------------------------------------------------------------
  8803. void __fastcall TCustomScpExplorerForm::SessionsPageControlMouseDown(
  8804. TObject * /*Sender*/, TMouseButton Button, TShiftState /*Shift*/, int X, int Y)
  8805. {
  8806. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8807. if (Index >= 0)
  8808. {
  8809. if (Button == mbLeft)
  8810. {
  8811. // "Mouse down" is raised only after tab is switched.
  8812. // If switching tab (switching session) takes long enough for user
  8813. // to actually release the button, "mouse down" is still raised,
  8814. // but we do not get "mouse up" event, so dragging is not cancelled,
  8815. // prevent that by not beginning dragging in the first place.
  8816. if (IsKeyPressed(VK_LBUTTON))
  8817. {
  8818. // when user clicks the "+", we get mouse down only after the session
  8819. // is closed, when new session tab is already on X:Y, so dragging
  8820. // starts, prevent that
  8821. if (MilliSecondsBetween(Now(), FSessionsPageControlNewSessionTime) > 500)
  8822. {
  8823. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8824. if (Terminal != NULL)
  8825. {
  8826. SessionsPageControl->BeginDrag(false);
  8827. }
  8828. }
  8829. }
  8830. }
  8831. else if (Button == mbMiddle)
  8832. {
  8833. // ignore middle-click for "New session tab"
  8834. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8835. if (Terminal != NULL)
  8836. {
  8837. CloseSessionTab(Index);
  8838. }
  8839. }
  8840. }
  8841. }
  8842. //---------------------------------------------------------------------------
  8843. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragDrop(
  8844. TObject * /*Sender*/, TObject * /*Source*/, int X, int Y)
  8845. {
  8846. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8847. // do not allow dropping on the "+" tab
  8848. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8849. if ((TargetTerminal != NULL) &&
  8850. (SessionsPageControl->ActivePage->PageIndex != Index))
  8851. {
  8852. Configuration->Usage->Inc(L"SessionTabMoves");
  8853. // this is almost redundant as we would recreate tabs in DoTerminalListChanged,
  8854. // but we want to actually prevent that to avoid flicker
  8855. SessionsPageControl->ActivePage->PageIndex = Index;
  8856. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->ActivePage);
  8857. TTerminalManager::Instance()->Move(Terminal, TargetTerminal);
  8858. }
  8859. }
  8860. //---------------------------------------------------------------------------
  8861. void __fastcall TCustomScpExplorerForm::SessionsPageControlDragOver(
  8862. TObject * Sender, TObject * Source, int X, int Y,
  8863. TDragState /*State*/, bool & Accept)
  8864. {
  8865. Accept = (Sender == Source);
  8866. if (Accept)
  8867. {
  8868. int Index = SessionsPageControl->IndexOfTabAt(X, Y);
  8869. TTerminal * Terminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8870. // do not allow dragging to the "+" tab
  8871. Accept = (Terminal != NULL);
  8872. }
  8873. }
  8874. //---------------------------------------------------------------------------
  8875. void __fastcall TCustomScpExplorerForm::SessionsDDDragOver(int /*KeyState*/,
  8876. const TPoint & Point, int & Effect)
  8877. {
  8878. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  8879. if (Index < 0)
  8880. {
  8881. Effect = DROPEFFECT_None;
  8882. }
  8883. else
  8884. {
  8885. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8886. // do not allow dropping on the "+" tab
  8887. if (TargetTerminal == NULL)
  8888. {
  8889. Effect = DROPEFFECT_None;
  8890. }
  8891. else if ((TargetTerminal != Terminal) && (Effect == DROPEFFECT_Move))
  8892. {
  8893. Effect = DROPEFFECT_Copy;
  8894. }
  8895. }
  8896. }
  8897. //---------------------------------------------------------------------------
  8898. void __fastcall TCustomScpExplorerForm::SessionsDDProcessDropped(
  8899. TObject * /*Sender*/, int /*KeyState*/, const TPoint & Point, int Effect)
  8900. {
  8901. int Index = SessionsPageControl->IndexOfTabAt(Point.X, Point.Y);
  8902. // do not allow dropping on the "+" tab
  8903. TTerminal * TargetTerminal = GetSessionTabTerminal(SessionsPageControl->Pages[Index]);
  8904. if (TargetTerminal != NULL)
  8905. {
  8906. DebugAssert(!IsFileControl(DropSourceControl, osRemote));
  8907. if (!IsFileControl(DropSourceControl, osRemote))
  8908. {
  8909. TTerminalManager::Instance()->ActiveTerminal = TargetTerminal;
  8910. RemoteFileControlDragDropFileOperation(SessionsPageControl, Effect,
  8911. // Why don't we use Terminal->CurrentDirectory directly?
  8912. TTerminalManager::Instance()->ActiveTerminal->CurrentDirectory,
  8913. // do not force queue, drag drop
  8914. false, true);
  8915. }
  8916. }
  8917. }
  8918. //---------------------------------------------------------------------------
  8919. void __fastcall TCustomScpExplorerForm::QueueDDProcessDropped(
  8920. TObject * /*Sender*/, int /*KeyState*/, const TPoint & /*Point*/, int Effect)
  8921. {
  8922. // Downloads are handled in RemoteFileControlDDEnd
  8923. if (!IsFileControl(DropSourceControl, osRemote))
  8924. {
  8925. RemoteFileControlDragDropFileOperation(QueueView3, Effect,
  8926. Terminal->CurrentDirectory,
  8927. // force queue, drag drop
  8928. true, true);
  8929. }
  8930. }
  8931. //---------------------------------------------------------------------------
  8932. void __fastcall TCustomScpExplorerForm::FormClose(TObject * /*Sender*/, TCloseAction & /*Action*/)
  8933. {
  8934. FShowing = false;
  8935. // Do not save empty workspace
  8936. if (WinConfiguration->AutoSaveWorkspace && (Terminal != NULL))
  8937. {
  8938. std::unique_ptr<TObjectList> DataList(DoCollectWorkspace());
  8939. UnicodeString Name = WorkspaceName();
  8940. DoSaveWorkspace(
  8941. Name, DataList.get(),
  8942. !Configuration->DisablePasswordStoring &&
  8943. WinConfiguration->AutoSaveWorkspacePasswords, false);
  8944. WinConfiguration->LastStoredSession = Name;
  8945. }
  8946. }
  8947. //---------------------------------------------------------------------------
  8948. void __fastcall TCustomScpExplorerForm::RemoteDirViewRead(TObject * /*Sender*/)
  8949. {
  8950. TManagedTerminal * ManagedTerminal =
  8951. dynamic_cast<TManagedTerminal *>(RemoteDirView->Terminal);
  8952. if (ManagedTerminal != NULL)
  8953. {
  8954. ManagedTerminal->DirectoryLoaded = Now();
  8955. }
  8956. }
  8957. //---------------------------------------------------------------------------
  8958. void __fastcall TCustomScpExplorerForm::DirViewSelectItem(TObject * Sender,
  8959. TListItem * /*Item*/, bool /*Selected*/)
  8960. {
  8961. TCustomDirView * DirView = reinterpret_cast<TCustomDirView *>(Sender);
  8962. switch (DirView->LastSelectMethod)
  8963. {
  8964. case smKeyboard:
  8965. Configuration->Usage->Inc(L"KeyboardSelections");
  8966. break;
  8967. case smMouse:
  8968. Configuration->Usage->Inc(L"MouseSelections");
  8969. break;
  8970. }
  8971. }
  8972. //---------------------------------------------------------------------------
  8973. int __fastcall TCustomScpExplorerForm::AddSessionColor(TColor Color)
  8974. {
  8975. if (Color != 0)
  8976. {
  8977. return GetSessionColorImage(FSessionColors, Color, FSessionColorMaskImageIndex);
  8978. }
  8979. else
  8980. {
  8981. return FSessionTabImageIndex;
  8982. }
  8983. }
  8984. //---------------------------------------------------------------------------
  8985. int __fastcall TCustomScpExplorerForm::AddFixedSessionImage(int GlyphsSourceIndex)
  8986. {
  8987. TPngImageCollectionItem * Item =
  8988. GlyphsModule->ExplorerImages->PngImages->Items[GlyphsSourceIndex];
  8989. FSessionColors->AddPng(Item->PngImage);
  8990. return FSessionColors->Count - 1;
  8991. }
  8992. //---------------------------------------------------------------------------
  8993. void __fastcall TCustomScpExplorerForm::AddFixedSessionImages()
  8994. {
  8995. FSessionColors->SetSize(GlyphsModule->ExplorerImages->Width, GlyphsModule->ExplorerImages->Height);
  8996. FNewSessionTabImageIndex = AddFixedSessionImage(NonVisualDataModule->NewSessionAction->ImageIndex);
  8997. FSessionTabImageIndex = AddFixedSessionImage(SiteImageIndex);
  8998. FSessionColorMaskImageIndex = AddFixedSessionImage(SiteColorMaskImageIndex);
  8999. }
  9000. //---------------------------------------------------------------------------
  9001. void __fastcall TCustomScpExplorerForm::CollectItemsWithTextDisplayMode(TWinControl * Control)
  9002. {
  9003. for (int Index = 0; Index < Control->ControlCount; Index++)
  9004. {
  9005. TControl * AControl = Control->Controls[Index];
  9006. TWinControl * WinControl = dynamic_cast<TWinControl *>(AControl);
  9007. if (WinControl != NULL)
  9008. {
  9009. CollectItemsWithTextDisplayMode(WinControl);
  9010. }
  9011. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(AControl);
  9012. if (Toolbar != NULL)
  9013. {
  9014. // we care for top-level items only
  9015. TTBCustomItem * Items = Toolbar->Items;
  9016. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  9017. {
  9018. TTBCustomItem * Item = Items->Items[ItemIndex];
  9019. if (((Item->DisplayMode == nbdmImageAndText) ||
  9020. (dynamic_cast<TTBXLabelItem *>(Item) != NULL)) &&
  9021. EligibleForImageDisplayMode(Item))
  9022. {
  9023. FItemsWithTextDisplayMode.insert(Item);
  9024. }
  9025. }
  9026. }
  9027. }
  9028. }
  9029. //---------------------------------------------------------------------------
  9030. bool __fastcall TCustomScpExplorerForm::EligibleForImageDisplayMode(TTBCustomItem * /*Item*/)
  9031. {
  9032. return true;
  9033. }
  9034. //---------------------------------------------------------------------------
  9035. bool __fastcall TCustomScpExplorerForm::UpdateToolbarDisplayMode()
  9036. {
  9037. bool SelectiveToolbarText = WinConfiguration->SelectiveToolbarText;
  9038. TTBItemDisplayMode DisplayMode = (SelectiveToolbarText ? nbdmImageAndText : nbdmDefault);
  9039. typedef std::set<TTBCustomItem *> TItemsWithTextDisplayMode;
  9040. TItemsWithTextDisplayMode::iterator i = FItemsWithTextDisplayMode.begin();
  9041. bool Result = true;
  9042. while (Result && (i != FItemsWithTextDisplayMode.end()))
  9043. {
  9044. TTBCustomItem * Item = *i;
  9045. TTBXLabelItem * Label = dynamic_cast<TTBXLabelItem *>(Item);
  9046. if (Label != NULL)
  9047. {
  9048. // optimization
  9049. if (Label->Visible == SelectiveToolbarText)
  9050. {
  9051. Result = false;
  9052. }
  9053. else
  9054. {
  9055. Label->Visible = SelectiveToolbarText;
  9056. }
  9057. }
  9058. else
  9059. {
  9060. // optimization
  9061. if (Item->DisplayMode == DisplayMode)
  9062. {
  9063. Result = false;
  9064. }
  9065. else
  9066. {
  9067. Item->DisplayMode = DisplayMode;
  9068. }
  9069. }
  9070. ++i;
  9071. }
  9072. if (Result)
  9073. {
  9074. UpdateNewSessionTab();
  9075. }
  9076. return Result;
  9077. }
  9078. //---------------------------------------------------------------------------
  9079. void __fastcall TCustomScpExplorerForm::DisplaySystemContextMenu()
  9080. {
  9081. DebugFail();
  9082. }
  9083. //---------------------------------------------------------------------------
  9084. bool __fastcall TCustomScpExplorerForm::IsBusy()
  9085. {
  9086. // Among other, a lock level is non zero, while directory is being loaded,
  9087. // even when it happens as a result of dirview navigation,
  9088. // i.e. when TNonVisualDataModule is NOT FBusy.
  9089. // That's why the TNonVisualDataModule::GetBusy calls this method.
  9090. // Among other this prevents a panel auto update to occur while
  9091. // directory is changing.
  9092. return (FLockLevel > 0) || DirView(osCurrent)->IsEditing();
  9093. }
  9094. //---------------------------------------------------------------------------
  9095. Boolean __fastcall TCustomScpExplorerForm::AllowedAction(TAction * /*Action*/, TActionAllowed Allowed)
  9096. {
  9097. // While the window is disabled, we still seem to process menu shortcuts at least,
  9098. // so stop it at least here.
  9099. // See also TCustomScpExplorerForm::RemoteDirViewBusy
  9100. return
  9101. (Allowed == aaUpdate) ||
  9102. !NonVisualDataModule->Busy;
  9103. }
  9104. //---------------------------------------------------------------------------
  9105. void __fastcall TCustomScpExplorerForm::EditMenuItemPopup(TTBCustomItem * Sender, bool FromLink)
  9106. {
  9107. NonVisualDataModule->EditMenuItemPopup(Sender, FromLink);
  9108. }
  9109. //---------------------------------------------------------------------------
  9110. void __fastcall TCustomScpExplorerForm::DirViewBusy(TObject * /*Sender*/, int Busy, bool & State)
  9111. {
  9112. // This is somewhat redundant to LockWindow() call from
  9113. // TTerminalManager::TerminalReadDirectoryProgress.
  9114. // But disabling window is known not to block keyboard shorcuts
  9115. // (see TCustomScpExplorerForm::AllowedAction), this hopefully works.
  9116. if (Busy > 0)
  9117. {
  9118. if (NonVisualDataModule->Busy)
  9119. {
  9120. State = false;
  9121. }
  9122. else
  9123. {
  9124. NonVisualDataModule->StartBusy();
  9125. // Will be countered for TCustomDirView.DoExecute in DoDirViewExecFile
  9126. LockWindow();
  9127. }
  9128. }
  9129. else if (Busy < 0)
  9130. {
  9131. UnlockWindow();
  9132. NonVisualDataModule->EndBusy();
  9133. }
  9134. else
  9135. {
  9136. State = NonVisualDataModule->Busy;
  9137. }
  9138. }
  9139. //---------------------------------------------------------------------------
  9140. void __fastcall TCustomScpExplorerForm::SessionsPageControlContextPopup(TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  9141. {
  9142. int Index = SessionsPageControl->IndexOfTabAt(MousePos.X, MousePos.Y);
  9143. // no context menu for "New session tab"
  9144. if ((Index >= 0) && (GetSessionTabTerminal(SessionsPageControl->Pages[Index]) != NULL))
  9145. {
  9146. SessionsPageControl->ActivePageIndex = Index;
  9147. if (DebugAlwaysTrue(SessionTabSwitched()))
  9148. {
  9149. // copied from TControl.WMContextMenu
  9150. SendCancelMode(SessionsPageControl);
  9151. // explicit popup instead of using PopupMenu property
  9152. // to avoid menu to popup somewhere within SessionTabSwitched above,
  9153. // while connecting yet not-connected session and hence
  9154. // allowing an access to commands over not-completelly connected session
  9155. TPoint Point = SessionsPageControl->ClientToScreen(MousePos);
  9156. TPopupMenu * PopupMenu = NonVisualDataModule->SessionsPopup;
  9157. PopupMenu->PopupComponent = SessionsPageControl;
  9158. PopupMenu->Popup(Point.x, Point.y);
  9159. }
  9160. }
  9161. Handled = true;
  9162. }
  9163. //---------------------------------------------------------------------------
  9164. bool __fastcall TCustomScpExplorerForm::CanChangePassword()
  9165. {
  9166. return (Terminal != NULL) && Terminal->Active && Terminal->IsCapable[fcChangePassword];
  9167. }
  9168. //---------------------------------------------------------------------------
  9169. void __fastcall TCustomScpExplorerForm::ChangePassword()
  9170. {
  9171. Configuration->Usage->Inc(L"PasswordChanges");
  9172. std::unique_ptr<TSessionData> ChangePasswordData(Terminal->SessionData->Clone());
  9173. ChangePasswordData->DisableAuthentationsExceptPassword();
  9174. ChangePasswordData->ChangePassword = true;
  9175. std::unique_ptr<TTerminal> ChangePasswordSession(
  9176. Terminal->CreateSecondarySession(L"ChangePassword", ChangePasswordData.get()));
  9177. ChangePasswordSession->AutoReadDirectory = false;
  9178. if (TTerminalManager::Instance()->ConnectTerminal(ChangePasswordSession.get()))
  9179. {
  9180. MessageDialog(MainInstructions(LoadStr(PASSWORD_CHANGED)), qtInformation, qaOK, HELP_CHANGE_PASSWORD);
  9181. }
  9182. else
  9183. {
  9184. Configuration->Usage->Inc(L"PasswordChangesFailed");
  9185. }
  9186. }
  9187. //---------------------------------------------------------------------------
  9188. bool __fastcall TCustomScpExplorerForm::CanPrivateKeyUpload()
  9189. {
  9190. // No nice way to assert SSH2
  9191. return (Terminal != NULL) && Terminal->Active && (Terminal->FSProtocol == cfsSFTP);
  9192. }
  9193. //---------------------------------------------------------------------------
  9194. void __fastcall TCustomScpExplorerForm::PrivateKeyUpload()
  9195. {
  9196. TOperationVisualizer OperationVisualizer;
  9197. UnicodeString FileName = Terminal->SessionData->PublicKeyFile;
  9198. TTerminalManager::Instance()->UploadPublicKey(Terminal, NULL, FileName);
  9199. }
  9200. //---------------------------------------------------------------------------
  9201. void __fastcall TCustomScpExplorerForm::ChangeScale(int M, int D)
  9202. {
  9203. TForm::ChangeScale(M, D);
  9204. int APixelsPerInch = GetControlPixelsPerInch(this);
  9205. HANDLE ResourceModule = GUIConfiguration->ChangeToDefaultResourceModule();
  9206. try
  9207. {
  9208. GlyphsModule->SetPixelsPerInch(APixelsPerInch);
  9209. }
  9210. __finally
  9211. {
  9212. GUIConfiguration->ChangeResourceModule(ResourceModule);
  9213. }
  9214. }
  9215. //---------------------------------------------------------------------------
  9216. void __fastcall TCustomScpExplorerForm::DockContextPopup(TObject * Sender, TPoint & MousePos, bool & /*Handled*/)
  9217. {
  9218. NonVisualDataModule->ControlContextPopup(Sender, MousePos);
  9219. }
  9220. //---------------------------------------------------------------------
  9221. class TPublicControl : public TControl
  9222. {
  9223. friend class TCustomScpExplorerForm;
  9224. };
  9225. //---------------------------------------------------------------------------
  9226. void __fastcall TCustomScpExplorerForm::CopyPopup(TControl * DestControl, TControl * SourceControl)
  9227. {
  9228. static_cast<TPublicControl *>(DestControl)->PopupMenu = static_cast<TPublicControl *>(SourceControl)->PopupMenu;
  9229. static_cast<TPublicControl *>(DestControl)->OnContextPopup = static_cast<TPublicControl *>(SourceControl)->OnContextPopup;
  9230. }
  9231. //---------------------------------------------------------------------------
  9232. void __fastcall TCustomScpExplorerForm::DoBookmarkClick(TOperationSide Side, TObject * Sender)
  9233. {
  9234. TBookmark * Bookmark = DebugNotNull(reinterpret_cast<TBookmark *>(dynamic_cast<TTBCustomItem *>(Sender)->Tag));
  9235. OpenBookmark(Side, Bookmark);
  9236. }
  9237. //---------------------------------------------------------------------------
  9238. void __fastcall TCustomScpExplorerForm::LocalBookmarkClick(TObject * Sender)
  9239. {
  9240. DoBookmarkClick(osLocal, Sender);
  9241. }
  9242. //---------------------------------------------------------------------------
  9243. void __fastcall TCustomScpExplorerForm::RemoteBookmarkClick(TObject * Sender)
  9244. {
  9245. DoBookmarkClick(osRemote, Sender);
  9246. }
  9247. //---------------------------------------------------------------------------
  9248. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenuList(
  9249. TTBCustomItem * Menu, TOperationSide Side, TBookmarkList * BookmarkList)
  9250. {
  9251. if (BookmarkList != NULL)
  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. AddMenuSeparator(Menu);
  9335. }
  9336. }
  9337. //---------------------------------------------------------------------------
  9338. void __fastcall TCustomScpExplorerForm::CreateOpenDirMenu(TTBCustomItem * Menu, TOperationSide Side)
  9339. {
  9340. Menu->Clear();
  9341. std::unique_ptr<TTBCustomItem> Item;
  9342. Item.reset(new TTBXItem(Owner));
  9343. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalOpenDirAction : NonVisualDataModule->RemoteOpenDirAction;
  9344. Item->Options = Item->Options << tboDefault;
  9345. Menu->Add(Item.release());
  9346. AddMenuSeparator(Menu);
  9347. CreateOpenDirMenuList(Menu, Side, WinConfiguration->Bookmarks[Terminal->SessionData->SessionKey]);
  9348. CreateOpenDirMenuList(Menu, Side, WinConfiguration->SharedBookmarks);
  9349. Item.reset(new TTBXItem(Owner));
  9350. Item->Action = (Side == osLocal) ? NonVisualDataModule->LocalAddBookmarkAction : NonVisualDataModule->RemoteAddBookmarkAction;
  9351. Menu->Add(Item.release());
  9352. }
  9353. //---------------------------------------------------------------------------
  9354. bool __fastcall TCustomScpExplorerForm::TryOpenDirectory(TOperationSide Side, const UnicodeString & Path)
  9355. {
  9356. bool Result = true;
  9357. try
  9358. {
  9359. if (Side == osRemote)
  9360. {
  9361. Terminal->ExceptionOnFail = true;
  9362. }
  9363. try
  9364. {
  9365. DirView(Side)->Path = Path;
  9366. }
  9367. __finally
  9368. {
  9369. if (Side == osRemote)
  9370. {
  9371. Terminal->ExceptionOnFail = false;
  9372. }
  9373. }
  9374. }
  9375. catch (Exception & E)
  9376. {
  9377. if ((Side != osRemote) || Terminal->Active)
  9378. {
  9379. ShowExtendedExceptionEx(Terminal, &E);
  9380. Result = false;
  9381. }
  9382. else
  9383. {
  9384. throw;
  9385. }
  9386. }
  9387. return Result;
  9388. }
  9389. //---------------------------------------------------------------------------
  9390. void __fastcall TCustomScpExplorerForm::ReloadDirectory(TOperationSide Side)
  9391. {
  9392. // Make sure there some feedback even when loading tine local folders
  9393. TInstantOperationVisualizer Visualizer;
  9394. DirView(Side)->ReloadDirectory();
  9395. }
  9396. //---------------------------------------------------------------------------
  9397. void __fastcall TCustomScpExplorerForm::CloseSessionTab(int Index)
  9398. {
  9399. if (Index == SessionsPageControl->TabIndex)
  9400. {
  9401. CloseSession();
  9402. }
  9403. else
  9404. {
  9405. TTabSheet * TabSheet = SessionsPageControl->Pages[Index];
  9406. TTerminal * Terminal = GetSessionTabTerminal(TabSheet);
  9407. TTerminalManager * Manager = TTerminalManager::Instance();
  9408. if (!Terminal->Active)
  9409. {
  9410. Manager->FreeTerminal(Terminal);
  9411. }
  9412. else
  9413. {
  9414. TTerminalQueue * Queue = Manager->FindQueueForTerminal(Terminal);
  9415. if (CanCloseQueue(Queue))
  9416. {
  9417. Manager->FreeTerminal(Terminal);
  9418. }
  9419. }
  9420. }
  9421. }
  9422. //---------------------------------------------------------------------------
  9423. void __fastcall TCustomScpExplorerForm::SessionsPageControlCloseButtonClick(TPageControl * /*Sender*/, int Index)
  9424. {
  9425. CloseSessionTab(Index);
  9426. }
  9427. //---------------------------------------------------------------------------
  9428. void __fastcall TCustomScpExplorerForm::CopyFilesToClipboard(TOperationSide Side)
  9429. {
  9430. if (DebugAlwaysTrue(GetSide(Side) == osRemote))
  9431. {
  9432. TInstantOperationVisualizer Visualizer;
  9433. // To trigger ClipboardDataObjectRelease (in case the clipboard already contains our data)
  9434. // before we create a new data object
  9435. ClipboardClear();
  9436. DebugAssert(FClipboardFakeDirectory.IsEmpty());
  9437. DebugAssert(FClipboardFakeMonitors.get() == NULL);
  9438. FClipboardFakeDirectory = CreateFakeTransferDirectory();
  9439. FClipboardFakeMonitors.reset(StartCreationDirectoryMonitorsOnEachDrive(FILE_NOTIFY_CHANGE_DIR_NAME, ClipboardFakeCreated));
  9440. if (FClipboardDragDropFilesEx.get() == NULL)
  9441. {
  9442. FClipboardDragDropFilesEx.reset(new TDragDropFilesEx(this));
  9443. FClipboardDragDropFilesEx->OnDataObjectRelease = ClipboardDataObjectRelease;
  9444. }
  9445. FClipboardDragDropFilesEx->FileList->Clear();
  9446. FClipboardDragDropFilesEx->FileList->AddItem(NULL, FClipboardFakeDirectory);
  9447. FClipboardDragDropFilesEx->CopyToClipboard();
  9448. FClipboardTerminal = FTerminal;
  9449. // Need full paths, as cwd can be different once files are pasted
  9450. FClipboardFileList.reset(TRemoteFileList::CloneStrings(RemoteDirView->CreateFileList(false, true)));
  9451. for (int Index = 0; Index < FClipboardFileList->Count; Index++)
  9452. {
  9453. FClipboardFileList->Strings[Index] = UnixExcludeTrailingBackslash(FClipboardFileList->Strings[Index]);
  9454. }
  9455. }
  9456. }
  9457. //---------------------------------------------------------------------------
  9458. void __fastcall TCustomScpExplorerForm::ClipboardClear()
  9459. {
  9460. if (OpenClipboard(0))
  9461. {
  9462. EmptyClipboard(); // Calls ClipboardDataObjectRelease, if clipboard contains our data
  9463. CloseClipboard();
  9464. }
  9465. }
  9466. //---------------------------------------------------------------------------
  9467. bool __fastcall TCustomScpExplorerForm::DoesClipboardContainOurFiles()
  9468. {
  9469. return !FClipboardFakeDirectory.IsEmpty();
  9470. }
  9471. //---------------------------------------------------------------------------
  9472. void __fastcall TCustomScpExplorerForm::ClipboardStop()
  9473. {
  9474. RemoveDir(ApiPath(FClipboardFakeDirectory));
  9475. FClipboardFakeDirectory = UnicodeString();
  9476. FClipboardTerminal = NULL;
  9477. // Also called in Idle()
  9478. FClipboardFakeMonitors.reset(NULL);
  9479. }
  9480. //---------------------------------------------------------------------------
  9481. void __fastcall TCustomScpExplorerForm::ClipboardDataObjectRelease(TObject * /*Sender*/)
  9482. {
  9483. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()))
  9484. {
  9485. ClipboardStop();
  9486. }
  9487. }
  9488. //---------------------------------------------------------------------------
  9489. void __fastcall TCustomScpExplorerForm::ClipboardDownload(const UnicodeString & TargetDirectory, bool NoConfirmation, bool DragDrop)
  9490. {
  9491. TTerminalManager * Manager = TTerminalManager::Instance();
  9492. Manager->ActiveTerminal = FClipboardTerminal;
  9493. TTransferOperationParam Params;
  9494. Params.TargetDirectory = TargetDirectory;
  9495. Params.DragDrop = DragDrop;
  9496. ExecuteFileOperation(foCopy, osRemote, FClipboardFileList.get(), NoConfirmation, &Params);
  9497. }
  9498. //---------------------------------------------------------------------------
  9499. void __fastcall TCustomScpExplorerForm::ClipboardFakeCreated(TObject * /*Sender*/, const UnicodeString FileName)
  9500. {
  9501. if (DebugAlwaysTrue(!FClipboardFakeDirectory.IsEmpty()) &&
  9502. SameText(ExtractFileName(FileName), ExtractFileName(FClipboardFakeDirectory)))
  9503. {
  9504. RemoveDir(ApiPath(FileName));
  9505. if (!NonVisualDataModule->Busy)
  9506. {
  9507. bool NoConfirmation = (WinConfiguration->DDTransferConfirmation == asOff);
  9508. ClipboardDownload(ExtractFilePath(FileName), NoConfirmation, true);
  9509. }
  9510. }
  9511. }
  9512. //---------------------------------------------------------------------------