CustomScpExplorer.cpp 286 KB

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