CustomScpExplorer.cpp 285 KB

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