CustomScpExplorer.cpp 287 KB

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