DriveView.pas 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851
  1. unit DriveView;
  2. {==================================================================
  3. Component TDriveView / Version 2.6, January 2000
  4. ==================================================================
  5. Description:
  6. ============
  7. Displays the the directory structure of all drives as treeview
  8. with shell icons. Complete drag&Drop support for files and
  9. directories.
  10. Author:
  11. =======
  12. (c) Ingo Eckel 1998, 1999
  13. Sodener Weg 38
  14. 65812 Bad Soden
  15. Germany
  16. Modifications (for WinSCP):
  17. ===========================
  18. (c) Martin Prikryl 2004
  19. V2.6:
  20. - Shows "shared"-symbol with directories
  21. - Delphi5 compatible
  22. For detailed documentation and history see TDriveView.htm.
  23. {==================================================================}
  24. interface
  25. { Define ENHVALIDATE to scan all existing directories on a detected filesystem change:}
  26. {.$DEFINE ENHVALIDATE}
  27. {Required compiler options for TDriveView:}
  28. {$A+,B-,X+,H+,P+}
  29. {$WARN SYMBOL_PLATFORM OFF}
  30. uses
  31. Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, ComObj,
  32. Dialogs, ComCtrls, ShellApi, CommCtrl, ExtCtrls, ActiveX, ShlObj,
  33. DirView, ShellDialogs, DragDrop, DragDropFilesEx, FileChanges, FileOperator,
  34. DiscMon, IEDriveInfo, IEListView, PIDL, BaseUtils, ListExt, CustomDirView,
  35. CustomDriveView, System.Generics.Collections;
  36. const
  37. msThreadChangeDelay = 50;
  38. ErrorNodeNA = '%s: Node not assigned';
  39. {Flags used by TDriveView.RefreshRootNodes:}
  40. dvdsFloppy = 8; {Include floppy drives}
  41. dvdsRereadAllways = 16; {Refresh drivestatus in any case}
  42. WM_USER_SHCHANGENOTIFY = WM_USER + $2000 + 13;
  43. type
  44. EInvalidDirName = class(Exception);
  45. ENodeNotAssigned = class(Exception);
  46. TDriveStatus = class
  47. Scanned: Boolean; {Drive allready scanned?}
  48. Verified: Boolean; {Drive completly scanned?}
  49. RootNode: TTreeNode; {Rootnode to drive}
  50. RootNodeIndex: Integer;
  51. DiscMonitor: TDiscMonitor; {Monitor thread}
  52. ChangeTimer: TTimer; {Change timer for the monitor thread}
  53. DefaultDir: string; {Current directory}
  54. DriveHandle: THandle;
  55. NotificationHandle: HDEVNOTIFY;
  56. end;
  57. TDriveStatusPair = TPair<string, TDriveStatus>;
  58. TScanDirInfo = record
  59. SearchNewDirs: Boolean;
  60. StartNode: TTreeNode;
  61. DriveType: Integer;
  62. end;
  63. PScanDirInfo = ^TScanDirInfo;
  64. TDriveView = class;
  65. TNodeData = class
  66. private
  67. FDirName: string;
  68. FShortName: string;
  69. FAttr: Integer;
  70. FScanned: Boolean;
  71. FData: Pointer;
  72. FIsRecycleBin: Boolean;
  73. FIconEmpty: Boolean;
  74. public
  75. shAttr: ULONG;
  76. PIDL: PItemIDList;
  77. ShellFolder: IShellFolder;
  78. constructor Create;
  79. destructor Destroy; override;
  80. property DirName: string read FDirName write FDirName;
  81. property ShortName: string read FShortName write FShortName;
  82. property Attr: Integer read FAttr write FAttr;
  83. property Scanned: Boolean read FScanned write FScanned;
  84. property Data: Pointer read FData write FData;
  85. property IsRecycleBin: Boolean read FIsRecycleBin;
  86. property IconEmpty: Boolean read FIconEmpty write FIconEmpty;
  87. end;
  88. TDriveTreeNode = class(TTreeNode)
  89. procedure Assign(Source: TPersistent); override;
  90. end;
  91. TDriveView = class(TCustomDriveView)
  92. private
  93. FDriveStatus: TObjectDictionary<string, TDriveStatus>;
  94. FConfirmDelete: Boolean;
  95. FConfirmOverwrite: Boolean;
  96. FWatchDirectory: Boolean;
  97. FDirectory: string;
  98. FFullDriveScan: Boolean;
  99. FShowVolLabel: Boolean;
  100. FVolDisplayStyle: TVolumeDisplayStyle;
  101. FChangeFlag: Boolean;
  102. FLastDir: string;
  103. FValidateFlag: Boolean;
  104. FCreating: Boolean;
  105. FForceRename: Boolean;
  106. FRenameNode: TTreeNode;
  107. FLastRenameName: string;
  108. FInternalWindowHandle: HWND;
  109. FChangeNotify: ULONG;
  110. FPrevSelected: TTreeNode;
  111. FPrevSelectedIndex: Integer;
  112. FChangeTimerSuspended: Integer;
  113. FDesktop: IShellFolder;
  114. {Additional events:}
  115. FOnDisplayContextMenu: TNotifyEvent;
  116. FOnRefreshDrives: TNotifyEvent;
  117. FOnNeedHiddenDirectories: TNotifyEvent;
  118. {used components:}
  119. FDirView: TDirView;
  120. FFileOperator: TFileOperator;
  121. FChangeInterval: Cardinal;
  122. {Drag&drop:}
  123. FLastPathCut: string;
  124. {Drag&drop helper functions:}
  125. procedure SignalDirDelete(Sender: TObject; Files: TStringList);
  126. function CheckForSubDirs(Path: string): Boolean;
  127. function ReadSubDirs(Node: TTreeNode; DriveType: Integer): Boolean;
  128. {Callback-functions used by iteratesubtree:}
  129. function CallBackValidateDir(var Node: TTreeNode; Data: Pointer): Boolean;
  130. { Notification procedures used by component TDiscMonitor: }
  131. procedure ChangeDetected(Sender: TObject; const Directory: string;
  132. var SubdirsChanged: Boolean);
  133. procedure ChangeInvalid(Sender: TObject; const Directory: string; const ErrorStr: string);
  134. {Notification procedure used by component TTimer:}
  135. procedure ChangeTimerOnTimer(Sender: TObject);
  136. protected
  137. procedure SetSelected(Node: TTreeNode);
  138. procedure SetFullDriveScan(DoFullDriveScan: Boolean);
  139. procedure SetWatchDirectory(Value: Boolean);
  140. procedure SetShowVolLabel(ShowIt: Boolean);
  141. procedure SetDirView(Value: TDirView);
  142. procedure SetDirectory(Value: string); override;
  143. procedure GetNodeShellAttr(ParentNode: TTreeNode; NodeData: TNodeData; GetAttr: Boolean);
  144. function DoScanDir(FromNode: TTreeNode): Boolean;
  145. function AddChildNode(ParentNode: TTreeNode; SRec: TSearchRec): TTreeNode;
  146. procedure CreateWatchThread(Drive: string);
  147. function NodeWatched(Node: TTreeNode): Boolean;
  148. procedure TerminateWatchThread(Drive: string);
  149. function WatchThreadActive: Boolean; overload;
  150. function WatchThreadActive(Drive: string): Boolean; overload;
  151. procedure InternalWndProc(var Msg: TMessage);
  152. procedure UpdateDriveNotifications(Drive: string);
  153. procedure DriveRemoved(Drive: string);
  154. procedure DriveRemoving(Drive: string);
  155. procedure CancelDriveRefresh;
  156. procedure ScheduleDriveRefresh;
  157. function DirAttrMask: Integer;
  158. function CreateDriveStatus: TDriveStatus;
  159. procedure ValidateDirectoryEx(Node: TTreeNode; Recurse: TRecursiveScan;
  160. NewDirs: Boolean); override;
  161. procedure RebuildTree; override;
  162. procedure SetLastPathCut(Path: string);
  163. function GetCanUndoCopyMove: Boolean; virtual;
  164. procedure CreateWnd; override;
  165. procedure DestroyWnd; override;
  166. procedure Edit(const Item: TTVItem); override;
  167. procedure WMUserRename(var Message: TMessage); message WM_USER_RENAME;
  168. function GetCustomDirView: TCustomDirView; override;
  169. procedure SetCustomDirView(Value: TCustomDirView); override;
  170. function NodePath(Node: TTreeNode): string; override;
  171. function NodeIsRecycleBin(Node: TTreeNode): Boolean; override;
  172. function NodePathExists(Node: TTreeNode): Boolean; override;
  173. function NodeColor(Node: TTreeNode): TColor; override;
  174. function FindPathNode(Path: string): TTreeNode; override;
  175. function DoFindNodeToPath(Path: string; ExistingOnly: Boolean): TTreeNode;
  176. function CreateNode: TTreeNode; override;
  177. function DDSourceEffects: TDropEffectSet; override;
  178. procedure DDChooseEffect(KeyState: Integer; var Effect: Integer; PreferredEffect: Integer); override;
  179. function DragCompleteFileList: Boolean; override;
  180. function DDExecute: TDragResult; override;
  181. public
  182. property Images;
  183. property StateImages;
  184. property Items stored False;
  185. property Selected Write SetSelected stored False;
  186. property DragImageList: TDragImageList read FDragImageList;
  187. property CanUndoCopyMove: Boolean read GetCanUndoCopyMove;
  188. property DDFileOperator: TFileOperator read FFileOperator;
  189. property LastPathCut: string read FLastPathCut write SetLastPathCut;
  190. function UndoCopyMove: Boolean; dynamic;
  191. procedure EmptyClipboard; dynamic;
  192. function CopyToClipBoard(Node: TTreeNode): Boolean; dynamic;
  193. function CutToClipBoard(Node: TTreeNode): Boolean; dynamic;
  194. function CanPasteFromClipBoard: Boolean; dynamic;
  195. function PasteFromClipBoard(TargetPath: string = ''): Boolean; dynamic;
  196. procedure PerformDragDropFileOperation(Node: TTreeNode; Effect: Integer); override;
  197. {Drive handling:}
  198. function GetDriveStatus(Drive: string): TDriveStatus;
  199. function GetDriveTypetoNode(Node: TTreeNode): Integer; {Returns DRIVE_CDROM etc..}
  200. function GetDriveToNode(Node: TTreeNode): string;
  201. function GetDriveText(Drive: string): string;
  202. procedure ScanDrive(Drive: string);
  203. procedure RefreshRootNodes(dsFlags: Integer);
  204. function GetDrives: TStrings;
  205. {Node handling:}
  206. procedure SetImageIndex(Node: TTreeNode); virtual;
  207. function FindNodeToPath(Path: string): TTreeNode;
  208. function TryFindNodeToPath(Path: string): TTreeNode;
  209. function RootNode(Node: TTreeNode): TTreeNode;
  210. function GetDirName(Node: TTreeNode): string;
  211. function GetDisplayName(Node: TTreeNode): string;
  212. function NodePathName(Node: TTreeNode): string; override;
  213. function GetFQPIDL(Node: TTreeNode): PItemIDList;
  214. constructor Create(AOwner: TComponent); override;
  215. destructor Destroy; override;
  216. {Menu-handling:}
  217. procedure DisplayContextMenu(Node: TTreeNode; Point: TPoint); override;
  218. procedure DisplayPropertiesMenu(Node: TTreeNode); override;
  219. {Watchthread handling:}
  220. procedure StartWatchThread;
  221. procedure StopWatchThread;
  222. procedure SuspendChangeTimer;
  223. procedure ResumeChangeTimer;
  224. procedure StartAllWatchThreads;
  225. procedure StopAllWatchThreads;
  226. procedure ValidateCurrentDirectoryIfNotMonitoring;
  227. (* Modified Events: *)
  228. procedure GetImageIndex(Node: TTreeNode); override;
  229. function CanEdit(Node: TTreeNode): Boolean; override;
  230. function CanChange(Node: TTreeNode): Boolean; override;
  231. function CanExpand(Node: TTreeNode): Boolean; override;
  232. procedure Delete(Node: TTreeNode); override;
  233. procedure Loaded; override;
  234. procedure KeyPress(var Key: Char); override;
  235. procedure Change(Node: TTreeNode); override;
  236. published
  237. {Additional properties:}
  238. {Current selected directory:}
  239. property Directory;
  240. {Confirm deleting directories:}
  241. property ConfirmDelete: Boolean read FConfirmDelete write FConfirmDelete default True;
  242. {Confirm overwriting directories:}
  243. property ConfirmOverwrite: Boolean read FConfirmOverwrite write FConfirmOverwrite default True;
  244. {Scan all directories in method ScanDrive:}
  245. property FullDriveScan: Boolean read FFullDriveScan write SetFullDriveScan default False;
  246. {Enable automatic update on filesystem changes:}
  247. property WatchDirectory: Boolean read FWatchDirectory write SetWatchDirectory default False;
  248. {Linked component TDirView:}
  249. property DirView: TDirView read FDirView write SetDirView;
  250. {Show the volume labels of drives:}
  251. property ShowVolLabel: Boolean read FShowVolLabel write SetShowVolLabel default True;
  252. {Additional events:}
  253. property OnDisplayContextMenu: TNotifyEvent read FOnDisplayContextMenu
  254. write FOnDisplayContextMenu;
  255. property OnRefreshDrives: TNotifyEvent read FOnRefreshDrives
  256. write FOnRefreshDrives;
  257. property OnBusy;
  258. property DDLinkOnExeDrag;
  259. property TargetPopUpMenu;
  260. property OnDDDragEnter;
  261. property OnDDDragLeave;
  262. property OnDDDragOver;
  263. property OnDDDrop;
  264. property OnDDQueryContinueDrag;
  265. property OnDDGiveFeedback;
  266. property OnDDDragDetect;
  267. property OnDDProcessDropped;
  268. property OnDDError;
  269. property OnDDExecuted;
  270. property OnDDFileOperation;
  271. property OnDDFileOperationExecuted;
  272. property Align;
  273. property Anchors;
  274. property AutoExpand;
  275. property BiDiMode;
  276. property BorderStyle;
  277. property BorderWidth;
  278. property ChangeDelay;
  279. property Color;
  280. property Ctl3D;
  281. property Constraints;
  282. property DoubleBuffered;
  283. {Delphi's drag&drop is not compatible with the OLE windows drag&drop:}
  284. property DragKind;
  285. property DragCursor;
  286. property DragMode Default dmAutomatic;
  287. property OnDragDrop;
  288. property OnDragOver;
  289. property Enabled;
  290. property Font;
  291. property HideSelection;
  292. property HotTrack;
  293. property Indent;
  294. property ParentBiDiMode;
  295. property ParentColor;
  296. property ParentCtl3D;
  297. property ParentDoubleBuffered;
  298. property ParentFont;
  299. property ParentShowHint;
  300. property PopupMenu;
  301. property ReadOnly;
  302. property RightClickSelect;
  303. property RowSelect;
  304. property ShowButtons;
  305. property ShowHint;
  306. property ShowLines;
  307. property TabOrder;
  308. property TabStop default True;
  309. property ToolTips;
  310. property Visible;
  311. property OnChange;
  312. property OnChanging;
  313. property OnClick;
  314. property OnCollapsing;
  315. property OnCollapsed;
  316. property OnCompare;
  317. property OnDblClick;
  318. property OnDeletion;
  319. property OnEdited;
  320. property OnEditing;
  321. property OnEndDock;
  322. property OnEndDrag;
  323. property OnEnter;
  324. property OnExit;
  325. property OnExpanding;
  326. property OnExpanded;
  327. property OnGetImageIndex;
  328. property OnGetSelectedIndex;
  329. property OnKeyDown;
  330. property OnKeyPress;
  331. property OnKeyUp;
  332. property OnMouseDown;
  333. property OnMouseMove;
  334. property OnMouseUp;
  335. property OnStartDock;
  336. property OnStartDrag;
  337. property OnNeedHiddenDirectories: TNotifyEvent read FOnNeedHiddenDirectories write FOnNeedHiddenDirectories;
  338. end;
  339. procedure Register;
  340. implementation
  341. uses
  342. CompThread, PasTools, UITypes, Types, OperationWithTimeout, System.Generics.Defaults;
  343. type
  344. PInt = ^Integer;
  345. procedure Register;
  346. begin
  347. RegisterComponents('DriveDir', [TDriveView]);
  348. end; {Register}
  349. constructor TNodeData.Create;
  350. begin
  351. inherited;
  352. FAttr := 0;
  353. FScanned := False;
  354. FDirName := '';
  355. FShortName := '';
  356. FIsRecycleBin := False;
  357. FIconEmpty := True;
  358. shAttr := 0;
  359. PIDL := nil;
  360. ShellFolder := nil;
  361. end; {TNodeData.Create}
  362. destructor TNodeData.Destroy;
  363. begin
  364. SetLength(FDirName, 0);
  365. if Assigned(PIDL) then
  366. FreePIDL(PIDL);
  367. inherited;
  368. end; {TNodeData.Destroy}
  369. { TDriveTreeNode }
  370. procedure TDriveTreeNode.Assign(Source: TPersistent);
  371. var
  372. SourceData: TNodeData;
  373. NewData: TNodeData;
  374. begin
  375. inherited Assign(Source);
  376. if not Deleting and (Source is TTreeNode) then
  377. begin
  378. SourceData := TNodeData(TTreeNode(Source).Data);
  379. NewData := TNodeData.Create();
  380. NewData.DirName := SourceData.DirName;
  381. NewData.ShortName := SourceData.ShortName;
  382. NewData.Attr := SourceData.Attr;
  383. NewData.Scanned := SourceData.Scanned;
  384. NewData.Data := SourceData.Data;
  385. NewData.FIsRecycleBin := SourceData.FIsRecycleBin;
  386. NewData.IconEmpty := SourceData.IconEmpty;
  387. TTreeNode(Source).Data := NewData;
  388. end;
  389. end;
  390. { TDriveView }
  391. constructor TDriveView.Create(AOwner: TComponent);
  392. var
  393. Drive: TRealDrive;
  394. ChangeNotifyEntry: TSHChangeNotifyEntry;
  395. Dummy: string;
  396. begin
  397. inherited;
  398. FCreating := True;
  399. FDriveStatus := TObjectDictionary<string, TDriveStatus>.Create([doOwnsValues]);
  400. FChangeInterval := MSecsPerSec;
  401. for Drive := FirstDrive to LastDrive do
  402. begin
  403. FDriveStatus.Add(Drive, CreateDriveStatus);
  404. end;
  405. FFileOperator := TFileOperator.Create(Self);
  406. FShowVolLabel := True;
  407. FChangeFlag := False;
  408. FLastDir := EmptyStr;
  409. FValidateFlag := False;
  410. FConfirmDelete := True;
  411. FDirectory := EmptyStr;
  412. FForceRename := False;
  413. FLastRenameName := '';
  414. FRenameNode := nil;
  415. FPrevSelected := nil;
  416. FPrevSelectedIndex := -1;
  417. FChangeTimerSuspended := 0;
  418. FConfirmOverwrite := True;
  419. FLastPathCut := '';
  420. FStartPos.X := -1;
  421. FStartPos.Y := -1;
  422. FDragPos := FStartPos;
  423. FInternalWindowHandle := Classes.AllocateHWnd(InternalWndProc);
  424. // Source: petr.solin 2022-02-25
  425. FChangeNotify := 0;
  426. if SpecialFolderLocation(CSIDL_DESKTOP, Dummy, ChangeNotifyEntry.pidl) then
  427. begin
  428. ChangeNotifyEntry.fRecursive := False;
  429. FChangeNotify :=
  430. SHChangeNotifyRegister(
  431. FInternalWindowHandle, SHCNRF_ShellLevel or SHCNRF_NewDelivery,
  432. SHCNE_RENAMEFOLDER or SHCNE_MEDIAINSERTED or SHCNE_MEDIAREMOVED,
  433. WM_USER_SHCHANGENOTIFY, 1, ChangeNotifyEntry);
  434. end;
  435. with FDragDropFilesEx do
  436. begin
  437. ShellExtensions.DragDropHandler := True;
  438. end;
  439. end; {Create}
  440. destructor TDriveView.Destroy;
  441. var
  442. DriveStatusPair: TDriveStatusPair;
  443. begin
  444. if FChangeNotify <> 0 then SHChangeNotifyDeregister(FChangeNotify);
  445. Classes.DeallocateHWnd(FInternalWindowHandle);
  446. for DriveStatusPair in FDriveStatus do
  447. begin
  448. with DriveStatusPair.Value do
  449. begin
  450. if Assigned(DiscMonitor) then
  451. FreeAndNil(DiscMonitor);
  452. if Assigned(ChangeTimer) then
  453. FreeAndNil(ChangeTimer);
  454. end;
  455. UpdateDriveNotifications(DriveStatusPair.Key);
  456. end;
  457. FDriveStatus.Free;
  458. if Assigned(FFileOperator) then
  459. FFileOperator.Free;
  460. inherited Destroy;
  461. end; {Destroy}
  462. function TDriveView.CreateDriveStatus: TDriveStatus;
  463. begin
  464. Result := TDriveStatus.Create;
  465. with Result do
  466. begin
  467. Scanned := False;
  468. Verified := False;
  469. RootNode := nil;
  470. RootNodeIndex := -1;
  471. DiscMonitor := nil;
  472. DefaultDir := EmptyStr;
  473. {ChangeTimer: }
  474. ChangeTimer := TTimer.Create(Self);
  475. ChangeTimer.Interval := 0;
  476. ChangeTimer.Enabled := False;
  477. ChangeTimer.OnTimer := ChangeTimerOnTimer;
  478. DriveHandle := INVALID_HANDLE_VALUE;
  479. NotificationHandle := nil;
  480. end;
  481. end;
  482. procedure TDriveView.DriveRemoving(Drive: string);
  483. begin
  484. DriveRemoved(Drive);
  485. TerminateWatchThread(Drive);
  486. end;
  487. type
  488. PDevBroadcastHdr = ^TDevBroadcastHdr;
  489. TDevBroadcastHdr = record
  490. dbch_size: DWORD;
  491. dbch_devicetype: DWORD;
  492. dbch_reserved: DWORD;
  493. end;
  494. PDevBroadcastVolume = ^TDevBroadcastVolume;
  495. TDevBroadcastVolume = record
  496. dbcv_size: DWORD;
  497. dbcv_devicetype: DWORD;
  498. dbcv_reserved: DWORD;
  499. dbcv_unitmask: DWORD;
  500. dbcv_flags: WORD;
  501. end;
  502. PDEV_BROADCAST_HANDLE = ^DEV_BROADCAST_HANDLE;
  503. DEV_BROADCAST_HANDLE = record
  504. dbch_size : DWORD;
  505. dbch_devicetype : DWORD;
  506. dbch_reserved : DWORD;
  507. dbch_handle : THandle;
  508. dbch_hdevnotify : HDEVNOTIFY ;
  509. dbch_eventguid : TGUID;
  510. dbch_nameoffset : LongInt;
  511. dbch_data : Byte;
  512. end;
  513. PPItemIDList = ^PItemIDList;
  514. const
  515. DBT_DEVTYP_HANDLE = $00000006;
  516. DBT_CONFIGCHANGED = $0018;
  517. DBT_DEVICEARRIVAL = $8000;
  518. DBT_DEVICEQUERYREMOVE = $8001;
  519. DBT_DEVICEREMOVEPENDING = $8003;
  520. DBT_DEVICEREMOVECOMPLETE = $8004;
  521. DBT_DEVTYP_VOLUME = $00000002;
  522. // WORKAROUND Declaration in Winapi.ShlObj.pas is wrong
  523. function SHChangeNotification_Lock(hChange: THandle; dwProcId: DWORD;
  524. var PPidls: PPItemIDList; var plEvent: Longint): THANDLE; stdcall;
  525. external 'shell32.dll' name 'SHChangeNotification_Lock';
  526. procedure TDriveView.InternalWndProc(var Msg: TMessage);
  527. var
  528. DeviceType: DWORD;
  529. UnitMask: DWORD;
  530. DeviceHandle: THandle;
  531. Drive: Char;
  532. DriveStatusPair: TDriveStatusPair;
  533. PPIDL: PPItemIDList;
  534. Event: LONG;
  535. Lock: THandle;
  536. begin
  537. with Msg do
  538. begin
  539. if Msg = WM_USER_SHCHANGENOTIFY then
  540. begin
  541. Lock := SHChangeNotification_Lock(wParam, lParam, PPIDL, Event);
  542. try
  543. if (Event = SHCNE_RENAMEFOLDER) or // = drive rename
  544. (Event = SHCNE_MEDIAINSERTED) or // also bitlocker drive unlock (also sends SHCNE_UPDATEDIR)
  545. (Event = SHCNE_MEDIAREMOVED) then
  546. begin
  547. ScheduleDriveRefresh;
  548. end;
  549. finally
  550. SHChangeNotification_Unlock(Lock);
  551. end;
  552. end
  553. else
  554. if Msg = WM_DEVICECHANGE then
  555. begin
  556. if (wParam = DBT_CONFIGCHANGED) or
  557. (wParam = DBT_DEVICEARRIVAL) or
  558. (wParam = DBT_DEVICEREMOVECOMPLETE) then
  559. begin
  560. ScheduleDriveRefresh;
  561. end
  562. else
  563. if (wParam = DBT_DEVICEQUERYREMOVE) or
  564. (wParam = DBT_DEVICEREMOVEPENDING) then
  565. begin
  566. DeviceType := PDevBroadcastHdr(lParam)^.dbch_devicetype;
  567. // This is specifically for VeraCrypt.
  568. // For normal drives, see DBT_DEVTYP_HANDLE below
  569. // (and maybe now that we have generic implementation, this specific code for VeraCrypt might not be needed anymore)
  570. if DeviceType = DBT_DEVTYP_VOLUME then
  571. begin
  572. UnitMask := PDevBroadcastVolume(lParam)^.dbcv_unitmask;
  573. Drive := FirstDrive;
  574. while UnitMask > 0 do
  575. begin
  576. if UnitMask and $01 <> 0 then
  577. begin
  578. DriveRemoving(Drive);
  579. end;
  580. UnitMask := UnitMask shr 1;
  581. Drive := Chr(Ord(Drive) + 1);
  582. end;
  583. end
  584. else
  585. if DeviceType = DBT_DEVTYP_HANDLE then
  586. begin
  587. DeviceHandle := PDEV_BROADCAST_HANDLE(lParam)^.dbch_handle;
  588. for DriveStatusPair in FDriveStatus do
  589. if DriveStatusPair.Value.DriveHandle = DeviceHandle then
  590. begin
  591. DriveRemoving(DriveStatusPair.Key);
  592. end;
  593. end;
  594. end;
  595. end
  596. else
  597. if Msg = WM_TIMER then
  598. begin
  599. CancelDriveRefresh;
  600. try
  601. //DriveInfo.Load;
  602. RefreshRootNodes(dsAll or dvdsRereadAllways);
  603. if Assigned(OnRefreshDrives) then
  604. OnRefreshDrives(Self);
  605. except
  606. Application.HandleException(Self);
  607. end;
  608. end;
  609. Result := DefWindowProc(FInternalWindowHandle, Msg, wParam, lParam);
  610. end;
  611. end;
  612. procedure TDriveView.CancelDriveRefresh;
  613. begin
  614. KillTimer(FInternalWindowHandle, 1);
  615. end;
  616. procedure TDriveView.ScheduleDriveRefresh;
  617. begin
  618. CancelDriveRefresh;
  619. // Delay refreshing drives for a sec.
  620. // Particularly with CD/DVD drives, if we query display name
  621. // immediately after receiving DBT_DEVICEARRIVAL, we do not get media label.
  622. // Actually one sec does not help usually, but we do not want to wait any longer,
  623. // because we want to add USB drives asap.
  624. // And this problem might be solved now by SHChangeNotifyRegister/SHCNE_RENAMEFOLDER.
  625. SetTimer(FInternalWindowHandle, 1, MSecsPerSec, nil);
  626. end;
  627. procedure TDriveView.CreateWnd;
  628. var
  629. DriveStatus: TDriveStatus;
  630. begin
  631. inherited;
  632. if Assigned(PopupMenu) then
  633. PopupMenu.Autopopup := False;
  634. OLECheck(SHGetDesktopFolder(FDesktop));
  635. FDragDropFilesEx.SourceEffects := [deCopy, deMove, deLink];
  636. FDragDropFilesEx.TargetEffects := [deCopy, deMove, deLink];
  637. if FPrevSelectedIndex >= 0 then
  638. begin
  639. FPrevSelected := Items[FPrevSelectedIndex];
  640. FPrevSelectedIndex := -1;
  641. end;
  642. for DriveStatus in FDriveStatus.Values do
  643. with DriveStatus do
  644. begin
  645. if RootNodeIndex >= 0 then
  646. begin
  647. RootNode := Items[RootNodeIndex];
  648. RootNodeIndex := -1;
  649. end;
  650. end;
  651. end; {CreateWnd}
  652. procedure TDriveView.DestroyWnd;
  653. var
  654. DriveStatus: TDriveStatus;
  655. begin
  656. if CreateWndRestores and (Items.Count > 0) and (csRecreating in ControlState) then
  657. begin
  658. FPrevSelectedIndex := -1;
  659. if Assigned(FPrevSelected) then
  660. begin
  661. FPrevSelectedIndex := FPrevSelected.AbsoluteIndex;
  662. FPrevSelected := nil;
  663. end;
  664. for DriveStatus in FDriveStatus.Values do
  665. with DriveStatus do
  666. begin
  667. RootNodeIndex := -1;
  668. if Assigned(RootNode) then
  669. begin
  670. RootNodeIndex := RootNode.AbsoluteIndex;
  671. RootNode := nil;
  672. end;
  673. end;
  674. end;
  675. inherited;
  676. end;
  677. function TDriveView.GetFQPIDL(Node: TTreeNode): PItemIDList;
  678. var
  679. Eaten: ULONG;
  680. shAttr: ULONG;
  681. begin
  682. Result := nil;
  683. if Assigned(Node) then
  684. begin
  685. shAttr := 0;
  686. FDesktop.ParseDisplayName(FParentForm.Handle, nil, PChar(NodePathName(Node)), Eaten,
  687. Result, shAttr);
  688. end;
  689. end; {GetFQPIDL}
  690. function TDriveView.NodeColor(Node: TTreeNode): TColor;
  691. begin
  692. Result := clDefaultItemColor;
  693. with TNodeData(Node.Data) do
  694. if not Node.Selected then
  695. begin
  696. {Colored display of compressed directories:}
  697. if (Attr and FILE_ATTRIBUTE_COMPRESSED) <> 0 then
  698. begin
  699. if SupportsDarkMode and DarkMode then Result := clSkyBlue
  700. else Result := clBlue;
  701. end
  702. else
  703. {Dimmed display, if hidden-atrribut set:}
  704. if FDimmHiddenDirs and ((Attr and FILE_ATTRIBUTE_HIDDEN) <> 0) then
  705. Result := clGrayText
  706. end;
  707. end;
  708. function TDriveView.GetCustomDirView: TCustomDirView;
  709. begin
  710. Result := DirView;
  711. end;
  712. procedure TDriveView.SetCustomDirView(Value: TCustomDirView);
  713. begin
  714. DirView := Value as TDirView;
  715. end;
  716. function TDriveView.NodePath(Node: TTreeNode): string;
  717. var
  718. ParentNode: TTreeNode;
  719. begin
  720. if not Assigned(Node) then
  721. raise ENodeNotAssigned.Create(Format(ErrorNodeNA, ['GetDirPath']));
  722. Result := GetDirName(Node);
  723. ParentNode := Node.Parent;
  724. while (ParentNode <> nil) and (ParentNode.Level >= 0) do
  725. begin
  726. if ParentNode.Level > 0 then
  727. Result := GetDirName(ParentNode) + '\' + Result
  728. else
  729. Result := GetDirName(ParentNode) + Result;
  730. ParentNode := ParentNode.Parent;
  731. end;
  732. if IsRootPath(Result) then
  733. Result := ExcludeTrailingBackslash(Result);
  734. end;
  735. {NodePathName: Returns the complete path to Node with trailing backslash on rootnodes:
  736. C:\ ,C:\WINDOWS, C:\WINDOWS\SYSTEM }
  737. function TDriveView.NodePathName(Node: TTreeNode): string;
  738. begin
  739. Result := NodePath(Node);
  740. if IsRootPath(Result) then
  741. Result := IncludeTrailingBackslash(Result);
  742. end; {NodePathName}
  743. function TDriveView.NodeIsRecycleBin(Node: TTreeNode): Boolean;
  744. begin
  745. Result := TNodeData(Node.Data).IsRecycleBin;
  746. end;
  747. function TDriveView.NodePathExists(Node: TTreeNode): Boolean;
  748. begin
  749. Result := DirectoryExists(NodePathName(Node));
  750. end;
  751. function TDriveView.CanEdit(Node: TTreeNode): Boolean;
  752. begin
  753. Result := inherited CanEdit(Node) or FForceRename;
  754. if Result then
  755. begin
  756. Result := Assigned(Node.Parent) and
  757. (not TNodeData(Node.Data).IsRecycleBin) and
  758. (not ReadOnly) and
  759. (FDragDropFilesEx.DragDetectStatus <> ddsDrag) and
  760. ((TNodeData(Node.Data).Attr and (faReadOnly or faSysFile)) = 0) and
  761. (UpperCase(Node.Text) = UpperCase(GetDirName(Node)));
  762. end;
  763. FForceRename := False;
  764. end; {CanEdit}
  765. procedure TDriveView.Edit(const Item: TTVItem);
  766. var
  767. SRec: TSearchRec;
  768. Node: TTreeNode;
  769. Info: string;
  770. i: Integer;
  771. begin
  772. Node := GetNodeFromHItem(Item);
  773. if (Length(Item.pszText) > 0) and (Item.pszText <> Node.Text) then
  774. begin
  775. if StrContains(coInvalidDosChars, Item.pszText) then
  776. begin
  777. Info := coInvalidDosChars;
  778. for i := Length(Info) downto 1 do
  779. System.Insert(Space, Info, i);
  780. if Length(Item.pszText) > 0 then
  781. raise EInvalidDirName.Create(SErrorInvalidName + Space + Info);
  782. Exit;
  783. end;
  784. StopWatchThread;
  785. if Assigned(DirView) then
  786. DirView.StopWatchThread;
  787. with FFileOperator do
  788. begin
  789. Flags := FileOperatorDefaultFlags + [foNoConfirmation];
  790. Operation := foRename;
  791. OperandFrom.Clear;
  792. OperandTo.Clear;
  793. OperandFrom.Add(NodePath(Node));
  794. OperandTo.Add(IncludeTrailingBackslash(NodePath(Node.Parent)) + Item.pszText);
  795. end;
  796. try
  797. if FFileOperator.Execute then
  798. begin
  799. Node.Text := Item.pszText;
  800. TNodeData(Node.Data).DirName := Item.pszText;
  801. if FindFirst(ApiPath(IncludeTrailingBackslash(NodePath(Node.Parent)) + Item.pszText),
  802. faAnyFile, SRec) = 0 then
  803. begin
  804. TNodeData(Node.Data).ShortName := string(SRec.FindData.cAlternateFileName);
  805. end;
  806. FindClose(SRec);
  807. SortChildren(Node.Parent, False);
  808. inherited;
  809. end
  810. else
  811. begin
  812. if FileOrDirExists(IncludeTrailingBackslash(NodePath(Node.Parent)) + Item.pszText) then
  813. Info := SErrorRenameFileExists + Item.pszText
  814. else
  815. Info := SErrorRenameFile + Item.pszText;
  816. MessageBeep(MB_ICONHAND);
  817. if MessageDlg(FormatLastOSError(Info), mtError, [mbOK, mbAbort], 0) = mrOK then
  818. begin
  819. FLastRenameName := Item.pszText;
  820. FRenameNode := Node;
  821. PostMessage(Self.Handle, WM_USER_RENAME, 0, 0);
  822. end;
  823. end;
  824. finally
  825. StartWatchThread;
  826. if Assigned(DirView) then
  827. begin
  828. DirView.Reload2;
  829. DirView.StartWatchThread;
  830. end;
  831. end;
  832. end;
  833. end; {Edit}
  834. procedure TDriveView.WMUserRename(var Message: TMessage);
  835. begin
  836. if Assigned(FRenameNode) then
  837. begin
  838. FForceRename := True;
  839. TreeView_EditLabel(Handle, FRenameNode.ItemID);
  840. SetWindowText(TreeView_GetEditControl(Self.Handle), PChar(FLastRenameName));
  841. FRenameNode := nil;
  842. end;
  843. end; {WMUserRename}
  844. function TDriveView.CanExpand(Node: TTreeNode): Boolean;
  845. var
  846. SubNode: TTreeNode;
  847. Drive: string;
  848. SaveCursor: TCursor;
  849. begin
  850. Result := inherited CanExpand(Node);
  851. Drive := GetDriveToNode(Node);
  852. if Node.HasChildren then
  853. begin
  854. if (Node.Level = 0) and
  855. (not GetDriveStatus(Drive).Scanned) and
  856. DriveInfo.IsFixedDrive(Drive) then
  857. begin
  858. SubNode := Node.GetFirstChild;
  859. if not Assigned(SubNode) then
  860. begin
  861. ScanDrive(Drive);
  862. SubNode := Node.GetFirstChild;
  863. Node.HasChildren := Assigned(SubNode);
  864. Result := Node.HasChildren;
  865. if not Assigned(GetDriveStatus(Drive).DiscMonitor) then
  866. CreateWatchThread(Drive);
  867. end;
  868. end
  869. else
  870. begin
  871. SaveCursor := Screen.Cursor;
  872. Screen.Cursor := crHourGlass;
  873. try
  874. if (not TNodeData(Node.Data).Scanned) and DoScanDir(Node) then
  875. begin
  876. ReadSubDirs(Node, DriveInfo.Get(Drive).DriveType);
  877. end;
  878. finally
  879. Screen.Cursor := SaveCursor;
  880. end;
  881. end;
  882. end;
  883. end; {CanExpand}
  884. procedure TDriveView.GetImageIndex(Node: TTreeNode);
  885. begin
  886. if TNodeData(Node.Data).IconEmpty then
  887. SetImageIndex(Node);
  888. inherited;
  889. end; {GetImageIndex}
  890. procedure TDriveView.Loaded;
  891. begin
  892. inherited;
  893. {Create the drive nodes:}
  894. RefreshRootNodes(dsDisplayName or dvdsFloppy);
  895. {Set the initial directory:}
  896. if (Length(FDirectory) > 0) and DirectoryExists(FDirectory) then
  897. Directory := FDirectory;
  898. FCreating := False;
  899. end; {Loaded}
  900. function TDriveView.CreateNode: TTreeNode;
  901. begin
  902. Result := TDriveTreeNode.Create(Items);
  903. end;
  904. procedure TDriveView.Delete(Node: TTreeNode);
  905. var
  906. NodeData: TNodeData;
  907. begin
  908. if Node = FPrevSelected then
  909. FPrevSelected := nil;
  910. NodeData := nil;
  911. if Assigned(Node) and Assigned(Node.Data) then
  912. NodeData := TNodeData(Node.Data);
  913. Node.Data := nil;
  914. inherited;
  915. if Assigned(NodeData) and not (csRecreating in ControlState) then
  916. begin
  917. NodeData.Destroy;
  918. end;
  919. end; {OnDelete}
  920. procedure TDriveView.KeyPress(var Key: Char);
  921. begin
  922. inherited;
  923. if Assigned(Selected) then
  924. begin
  925. if Pos(Key, coInvalidDosChars) <> 0 then
  926. begin
  927. Beep;
  928. Key := #0;
  929. end;
  930. end;
  931. end; {KeyPress}
  932. function TDriveView.CanChange(Node: TTreeNode): Boolean;
  933. var
  934. Path: string;
  935. Drive: string;
  936. begin
  937. Result := inherited CanChange(Node);
  938. if not Reading and not (csRecreating in ControlState) then
  939. begin
  940. if Result and Assigned(Node) then
  941. begin
  942. Path := NodePathName(Node);
  943. if Path <> FLastDir then
  944. begin
  945. Drive := DriveInfo.GetDriveKey(Path);
  946. DriveInfo.ReadDriveStatus(Drive, dsSize or dsImageIndex);
  947. if not DriveInfo.Get(Drive).DriveReady then
  948. begin
  949. MessageDlg(Format(SDriveNotReady, [Drive]), mtError, [mbOK], 0);
  950. Result := False;
  951. end
  952. else
  953. try
  954. CheckCanOpenDirectory(Path);
  955. except
  956. Application.HandleException(Self);
  957. Result := False;
  958. end;
  959. end;
  960. end;
  961. if Result and (csDestroying in ComponentState) then
  962. begin
  963. Result := False;
  964. end;
  965. if Result and
  966. (not FCanChange) and
  967. Assigned(Node) and
  968. Assigned(Node.Data) and
  969. Assigned(Selected) and
  970. Assigned(Selected.Data) then
  971. begin
  972. DropTarget := Node;
  973. Result := False;
  974. end
  975. else
  976. begin
  977. DropTarget := nil;
  978. end;
  979. end;
  980. end; {CanChange}
  981. procedure TDriveView.Change(Node: TTreeNode);
  982. var
  983. Drive: string;
  984. OldSerial: DWORD;
  985. NewDir: string;
  986. PrevDrive: string;
  987. begin
  988. if not Reading and not (csRecreating in ControlState) then
  989. begin
  990. if Assigned(Node) then
  991. begin
  992. NewDir := NodePathName(Node);
  993. if NewDir <> FLastDir then
  994. begin
  995. Drive := DriveInfo.GetDriveKey(NewDir);
  996. if Length(FLastDir) > 0 then
  997. PrevDrive := DriveInfo.GetDriveKey(FLastDir)
  998. else
  999. PrevDrive := '';
  1000. FChangeFlag := True;
  1001. FLastDir := NewDir;
  1002. OldSerial := DriveInfo.Get(Drive).DriveSerial;
  1003. DriveInfo.ReadDriveStatus(Drive, dsSize or dsImageIndex);
  1004. with DriveInfo.Get(Drive) do
  1005. begin
  1006. if Assigned(FDirView) and (FDirView.Path <> NewDir) then
  1007. FDirView.Path := NewDir;
  1008. if DriveReady then
  1009. begin
  1010. if not DirectoryExists(NewDir) then
  1011. begin
  1012. ValidateDirectory(GetDriveStatus(Drive).RootNode);
  1013. Exit;
  1014. end;
  1015. GetDriveStatus(Drive).DefaultDir := IncludeTrailingBackslash(NewDir);
  1016. if PrevDrive <> Drive then
  1017. begin
  1018. if (PrevDrive <> '') and
  1019. (DriveInfo.Get(PrevDrive).DriveType = DRIVE_REMOVABLE) then
  1020. begin
  1021. TerminateWatchThread(PrevDrive);
  1022. end;
  1023. {Drive serial has changed or is missing: allways reread the drive:}
  1024. if (DriveSerial <> OldSerial) or (DriveSerial = 0) then
  1025. begin
  1026. if TNodeData(GetDriveStatus(Drive).RootNode.Data).Scanned then
  1027. ScanDrive(Drive);
  1028. end;
  1029. end;
  1030. StartWatchThread;
  1031. end
  1032. else {Drive not ready:}
  1033. begin
  1034. GetDriveStatus(Drive).RootNode.DeleteChildren;
  1035. GetDriveStatus(Drive).DefaultDir := EmptyStr;
  1036. end;
  1037. end;
  1038. end;
  1039. if (not Assigned(FPrevSelected)) or (not FPrevSelected.HasAsParent(Node)) then
  1040. Node.Expand(False);
  1041. FPrevSelected := Node;
  1042. ValidateCurrentDirectoryIfNotMonitoring;
  1043. end;
  1044. end;
  1045. inherited;
  1046. end; {Change}
  1047. procedure TDriveView.SetImageIndex(Node: TTreeNode);
  1048. var
  1049. FileInfo: TShFileInfo;
  1050. Drive, NodePath: string;
  1051. begin
  1052. if Assigned(Node) and TNodeData(Node.Data).IconEmpty then
  1053. begin
  1054. NodePath := NodePathName(Node);
  1055. Drive := DriveInfo.GetDriveKey(NodePath);
  1056. if Node.Level = 0 then
  1057. begin
  1058. with DriveInfo.Get(Drive) do
  1059. begin
  1060. if ImageIndex = 0 then
  1061. begin
  1062. DriveInfo.ReadDriveStatus(Drive, dsImageIndex);
  1063. Node.ImageIndex := DriveInfo.Get(Drive).ImageIndex;
  1064. end
  1065. else Node.ImageIndex := ImageIndex;
  1066. Node.SelectedIndex := Node.ImageIndex;
  1067. end;
  1068. end
  1069. else
  1070. begin
  1071. if DriveInfo.Get(Drive).DriveType = DRIVE_REMOTE then
  1072. begin
  1073. Node.ImageIndex := StdDirIcon;
  1074. Node.SelectedIndex := StdDirSelIcon;
  1075. end
  1076. else
  1077. begin
  1078. try
  1079. SHGetFileInfo(PChar(NodePath), 0, FileInfo, SizeOf(FileInfo),
  1080. SHGFI_SYSICONINDEX or SHGFI_SMALLICON);
  1081. if (FileInfo.iIcon < Images.Count) and (FileInfo.iIcon > 0) then
  1082. begin
  1083. Node.ImageIndex := FileInfo.iIcon;
  1084. SHGetFileInfo(PChar(NodePath), 0, FileInfo, SizeOf(FileInfo),
  1085. SHGFI_SYSICONINDEX or SHGFI_SMALLICON or SHGFI_OPENICON);
  1086. Node.SelectedIndex := FileInfo.iIcon;
  1087. end
  1088. else
  1089. begin
  1090. Node.ImageIndex := StdDirIcon;
  1091. Node.SelectedIndex := StdDirSelIcon;
  1092. end;
  1093. except
  1094. Node.ImageIndex := StdDirIcon;
  1095. Node.SelectedIndex := StdDirSelIcon;
  1096. end;
  1097. end;
  1098. end;
  1099. end; {IconEmpty}
  1100. TNodeData(Node.Data).IconEmpty := False;
  1101. end; {SetImageIndex}
  1102. function TDriveView.GetDriveText(Drive: string): string;
  1103. begin
  1104. if FShowVolLabel and (Length(DriveInfo.GetPrettyName(Drive)) > 0) then
  1105. begin
  1106. case FVolDisplayStyle of
  1107. doPrettyName: Result := DriveInfo.GetPrettyName(Drive);
  1108. doDisplayName: Result := DriveInfo.GetDisplayName(Drive);
  1109. end; {Case}
  1110. end
  1111. else
  1112. begin
  1113. Result := DriveInfo.GetSimpleName(Drive);
  1114. end;
  1115. end; {GetDriveText}
  1116. procedure TDriveView.GetNodeShellAttr(ParentNode: TTreeNode; NodeData: TNodeData; GetAttr: Boolean);
  1117. var
  1118. ParentFolder: IShellFolder;
  1119. ParentData: TNodeData;
  1120. begin
  1121. NodeData.shAttr := 0;
  1122. if GetAttr then
  1123. begin
  1124. if Assigned(ParentNode) then
  1125. begin
  1126. ParentData := TNodeData(ParentNode.Data);
  1127. if not Assigned(ParentData) then
  1128. begin
  1129. Assert(False);
  1130. ParentFolder := nil;
  1131. end
  1132. else
  1133. begin
  1134. if not Assigned(ParentData.ShellFolder) then
  1135. begin
  1136. GetNodeShellAttr(ParentNode.Parent, ParentData, GetAttr);
  1137. end;
  1138. ParentFolder := ParentData.ShellFolder;
  1139. end;
  1140. end
  1141. else
  1142. begin
  1143. ParentFolder := FDesktop;
  1144. end;
  1145. if Assigned(ParentFolder) and Assigned(NodeData) then
  1146. begin
  1147. if not Assigned(NodeData.PIDL) then
  1148. NodeData.PIDL := PIDL_GetFromParentFolder(ParentFolder, PChar(NodeData.DirName));
  1149. if Assigned(NodeData.PIDL) then
  1150. begin
  1151. NodeData.shAttr := SFGAO_CONTENTSMASK;
  1152. // Previously we would also make use of SFGAO_SHARE to display a share overlay.
  1153. // But for directories, Windows File Explorer does not display the overlay anymore (probably since Vista).
  1154. // And for drives (where Explorer does display the overlay), it did not work ever since we use "desktop"
  1155. // (and not "workspace" as before) to resolve drive interface (see Bug 1717).
  1156. if not Succeeded(ShellFolderGetAttributesOfWithTimeout(ParentFolder, 1, NodeData.PIDL, NodeData.shAttr, MSecsPerSec)) then
  1157. begin
  1158. NodeData.shAttr := 0;
  1159. end;
  1160. if not Assigned(NodeData.ShellFolder) then
  1161. begin
  1162. ParentFolder.BindToObject(NodeData.PIDL, nil, IID_IShellFolder, Pointer(NodeData.ShellFolder));
  1163. end;
  1164. end
  1165. end;
  1166. end;
  1167. if NodeData.shAttr = 0 then
  1168. begin
  1169. // If we cannot resolve attrs, we do not want to assume that the folder has no subfolders,
  1170. // as that will make us scan the folder.
  1171. NodeData.shAttr := SFGAO_HASSUBFOLDER;
  1172. end;
  1173. end; {GetNodeAttr}
  1174. function CompareDrive(List: TStringList; Index1, Index2: Integer): Integer;
  1175. var
  1176. Drive1, Drive2: string;
  1177. RealDrive1, RealDrive2: Boolean;
  1178. begin
  1179. Drive1 := List[Index1];
  1180. Drive2 := List[Index2];
  1181. RealDrive1 := DriveInfo.IsRealDrive(Drive1);
  1182. RealDrive2 := DriveInfo.IsRealDrive(Drive2);
  1183. if RealDrive1 = RealDrive2 then
  1184. begin
  1185. Result := CompareText(Drive1, Drive2);
  1186. end
  1187. else
  1188. if RealDrive1 and (not RealDrive2) then
  1189. begin
  1190. Result := -1;
  1191. end
  1192. else
  1193. begin
  1194. Result := 1;
  1195. end;
  1196. end;
  1197. function TDriveView.GetDrives: TStrings;
  1198. var
  1199. DriveStatusPair: TDriveStatusPair;
  1200. Drives: TStringList;
  1201. begin
  1202. Drives := TStringList.Create;
  1203. { We could iterate only .Keys here, but that crashes IDE for some reason }
  1204. for DriveStatusPair in FDriveStatus do
  1205. begin
  1206. Drives.Add(DriveStatusPair.Key);
  1207. end;
  1208. Drives.CustomSort(CompareDrive);
  1209. Result := Drives;
  1210. end;
  1211. procedure TDriveView.DriveRemoved(Drive: string);
  1212. var
  1213. NewDrive: Char;
  1214. begin
  1215. if (Directory <> '') and (Directory[1] = Drive) then
  1216. begin
  1217. if DriveInfo.IsRealDrive(Drive) then NewDrive := Drive[1]
  1218. else NewDrive := SystemDrive;
  1219. repeat
  1220. if NewDrive < SystemDrive then NewDrive := SystemDrive
  1221. else
  1222. if NewDrive = SystemDrive then NewDrive := LastDrive
  1223. else Dec(NewDrive);
  1224. DriveInfo.ReadDriveStatus(NewDrive, dsSize or dsImageIndex);
  1225. if NewDrive = Drive then
  1226. begin
  1227. Break;
  1228. end;
  1229. if DriveInfo.Get(NewDrive).Valid and DriveInfo.Get(NewDrive).DriveReady and Assigned(GetDriveStatus(NewDrive).RootNode) then
  1230. begin
  1231. Directory := NodePathName(GetDriveStatus(NewDrive).RootNode);
  1232. break;
  1233. end;
  1234. until False;
  1235. if not Assigned(Selected) then
  1236. begin
  1237. Directory := NodePathName(GetDriveStatus(SystemDrive).RootNode);
  1238. end;
  1239. end;
  1240. end;
  1241. procedure TDriveView.RefreshRootNodes(dsFlags: Integer);
  1242. var
  1243. Drives: TStrings;
  1244. NewText: string;
  1245. SaveCursor: TCursor;
  1246. WasValid: Boolean;
  1247. NodeData: TNodeData;
  1248. DriveStatus: TDriveStatus;
  1249. NextDriveNode: TTreeNode;
  1250. Index: Integer;
  1251. Drive: string;
  1252. GetAttr: Boolean;
  1253. begin
  1254. SaveCursor := Screen.Cursor;
  1255. Screen.Cursor := crHourGlass;
  1256. Drives := nil;
  1257. try
  1258. Drives := GetDrives;
  1259. NextDriveNode := nil;
  1260. for Index := Drives.Count - 1 downto 0 do
  1261. begin
  1262. Drive := Drives[Index];
  1263. DriveStatus := GetDriveStatus(Drive);
  1264. if ((dsFlags and dvdsFloppy) <> 0) or DriveInfo.IsFixedDrive(Drive) then
  1265. begin
  1266. with DriveInfo.Get(Drive) do
  1267. begin
  1268. WasValid := Assigned(DriveStatus.RootNode);
  1269. end;
  1270. if ((dsFlags and dvdsReReadAllways) = 0) and
  1271. (Length(DriveInfo.Get(Drive).DisplayName) > 0) then
  1272. dsFlags := dsFlags and (not dsDisplayName);
  1273. DriveInfo.ReadDriveStatus(Drive, dsFlags);
  1274. with DriveInfo.Get(Drive), DriveStatus do
  1275. begin
  1276. if Valid then
  1277. begin
  1278. if not WasValid then
  1279. {New drive has arrived: insert new rootnode:}
  1280. begin
  1281. { Create root directory node }
  1282. NodeData := TNodeData.Create;
  1283. NodeData.DirName := DriveInfo.GetDriveRoot(Drive);
  1284. NodeData.ShortName := NodeData.DirName;
  1285. {Get the shared attributes:}
  1286. GetAttr :=
  1287. DriveInfo.IsFixedDrive(Drive) and (DriveType <> DRIVE_REMOVABLE) and
  1288. ((DriveType <> DRIVE_REMOTE) or GetNetWorkConnected(Drive));
  1289. GetNodeShellAttr(nil, NodeData, GetAttr);
  1290. if Assigned(NextDriveNode) then
  1291. RootNode := Items.InsertObject(NextDriveNode, '', NodeData)
  1292. else
  1293. RootNode := Items.AddObject(nil, '', NodeData);
  1294. RootNode.Text := GetDisplayName(RootNode);
  1295. RootNode.HasChildren := True;
  1296. Scanned := False;
  1297. Verified := False;
  1298. end
  1299. else
  1300. if RootNode.ImageIndex <> DriveInfo.Get(Drive).ImageIndex then
  1301. begin {WasValid = True}
  1302. RootNode.ImageIndex := DriveInfo.Get(Drive).ImageIndex;
  1303. RootNode.SelectedIndex := DriveInfo.Get(Drive).ImageIndex;
  1304. end;
  1305. if Assigned(RootNode) then
  1306. begin
  1307. NewText := GetDisplayName(RootNode);
  1308. if RootNode.Text <> NewText then
  1309. RootNode.Text := NewText;
  1310. end;
  1311. end
  1312. else
  1313. if WasValid then
  1314. {Drive has been removed => delete rootnode:}
  1315. begin
  1316. DriveRemoved(Drive);
  1317. Scanned := False;
  1318. Verified := False;
  1319. RootNode.Delete;
  1320. RootNode := nil;
  1321. end;
  1322. end;
  1323. end;
  1324. if Assigned(DriveStatus.RootNode) then
  1325. NextDriveNode := DriveStatus.RootNode;
  1326. end;
  1327. finally
  1328. Screen.Cursor := SaveCursor;
  1329. Drives.Free;
  1330. end;
  1331. end; {RefreshRootNodes}
  1332. function TDriveView.AddChildNode(ParentNode: TTreeNode; SRec: TSearchRec): TTreeNode;
  1333. var
  1334. NewNode: TTreeNode;
  1335. NodeData: TNodeData;
  1336. GetAttr: Boolean;
  1337. begin
  1338. NodeData := TNodeData.Create;
  1339. NodeData.Attr := SRec.Attr;
  1340. NodeData.DirName := SRec.Name;
  1341. NodeData.ShortName := SRec.FindData.cAlternateFileName;
  1342. NodeData.FIsRecycleBin :=
  1343. (SRec.Attr and faSysFile <> 0) and
  1344. (ParentNode.Level = 0) and
  1345. (SameText(SRec.Name, 'RECYCLED') or
  1346. SameText(SRec.Name, 'RECYCLER') or
  1347. SameText(SRec.Name, '$RECYCLE.BIN'));
  1348. { query content attributes ("has subfolder") only if tree view is visible }
  1349. { to avoid unnecessary scan of subfolders (which may take some time) }
  1350. { if tree view is not visible anyway }
  1351. GetAttr :=
  1352. Visible and
  1353. (GetDriveTypeToNode(ParentNode) <> DRIVE_REMOTE);
  1354. GetNodeShellAttr(ParentNode, NodeData, GetAttr);
  1355. NewNode := Self.Items.AddChildObject(ParentNode, '', NodeData);
  1356. NewNode.Text := GetDisplayName(NewNode);
  1357. Result := NewNode;
  1358. end; {AddChildNode}
  1359. function TDriveView.GetDriveStatus(Drive: string): TDriveStatus;
  1360. begin
  1361. if not FDriveStatus.TryGetValue(Drive, Result) then
  1362. begin
  1363. Result := CreateDriveStatus;
  1364. FDriveStatus.Add(Drive, Result);
  1365. RefreshRootNodes(dsAll or dvdsRereadAllways);
  1366. if Assigned(OnRefreshDrives) then
  1367. OnRefreshDrives(Self);
  1368. end;
  1369. end; {GetDriveStatus}
  1370. function TDriveView.DoScanDir(FromNode: TTreeNode): Boolean;
  1371. begin
  1372. Result := not TNodeData(FromNode.Data).IsRecycleBin;
  1373. end; {DoScanDir}
  1374. function TDriveView.DirAttrMask: Integer;
  1375. begin
  1376. Result := faDirectory or faSysFile;
  1377. if ShowHiddenDirs then
  1378. Result := Result or faHidden;
  1379. end;
  1380. procedure TDriveView.ScanDrive(Drive: string);
  1381. var
  1382. DosError: Integer;
  1383. RootNode: TTreeNode;
  1384. SaveCursor: TCursor;
  1385. procedure ScanPath(const Path: string; ParentNode: TTreeNode);
  1386. var
  1387. SRec: TSearchRec;
  1388. SubNode: TTreeNode;
  1389. begin
  1390. if not DoScanDir(ParentNode) then
  1391. Exit;
  1392. DosError := FindFirst(ApiPath(Path), DirAttrMask, Srec);
  1393. while DosError = 0 do
  1394. begin
  1395. if (SRec.Name <> '.') and
  1396. (SRec.Name <> '..') and
  1397. (SRec.Attr and faDirectory <> 0) then
  1398. begin
  1399. if (SRec.Attr And faDirectory) <> 0 then
  1400. begin { Scan subdirectory }
  1401. SubNode := AddChildNode(ParentNode, SRec);
  1402. TNodeData(SubNode.Data).Scanned := True;
  1403. ScanPath(ExtractFilePath(Path) + SRec.Name + '\*.*', SubNode);
  1404. if not FContinue then
  1405. Break;
  1406. end;
  1407. end;
  1408. DosError := FindNext(SRec);
  1409. end;
  1410. FindClose(Srec);
  1411. if (Items.Count mod 10) = 0 then
  1412. Application.ProcessMessages;
  1413. if not FContinue then
  1414. Exit;
  1415. end; {ScanPath}
  1416. begin {ScanDrive}
  1417. with Self.Items do
  1418. begin
  1419. FContinue := True;
  1420. if not FFullDriveScan then
  1421. begin
  1422. ValidateDirectory(FindNodeToPath(DriveInfo.GetDriveRoot(Drive)));
  1423. GetDriveStatus(Drive).Scanned := True;
  1424. GetDriveStatus(Drive).Verified := False;
  1425. end
  1426. else
  1427. begin
  1428. SaveCursor := Screen.Cursor;
  1429. Screen.Cursor := crHourGlass;
  1430. Items.BeginUpdate;
  1431. try
  1432. RootNode := GetDriveStatus(Drive).RootNode;
  1433. if not Assigned(RootNode) then Exit;
  1434. iF RootNode.HasChildren then
  1435. RootNode.DeleteChildren;
  1436. ScanPath(DriveInfo.GetDriveRoot(Drive) + '*.*', RootNode); { scan subdirectories of rootdir}
  1437. TNodeData(RootNode.Data).Scanned := True;
  1438. GetDriveStatus(Drive).Scanned := True;
  1439. GetDriveStatus(Drive).Verified := True;
  1440. finally
  1441. SortChildren(GetDriveStatus(Drive).RootNode, True);
  1442. EndUpdate;
  1443. end;
  1444. RootNode.Expand(False);
  1445. Screen.Cursor := SaveCursor;
  1446. end;
  1447. end;
  1448. end; {ScanDrive}
  1449. function TDriveView.DoFindNodeToPath(Path: string; ExistingOnly: Boolean): TTreeNode;
  1450. function SearchSubDirs(ParentNode: TTreeNode; Path: string): TTreeNode; forward;
  1451. function DoSearchSubDirs(ParentNode: TTreeNode; Path: string): TTreeNode;
  1452. var
  1453. i: Integer;
  1454. Node: TTreeNode;
  1455. Dir: string;
  1456. begin
  1457. {Extract first directory from path:}
  1458. i := Pos('\', Path);
  1459. if i = 0 then
  1460. i := Length(Path);
  1461. Dir := System.Copy(Path, 1, i);
  1462. System.Delete(Path, 1, i);
  1463. if Dir[Length(Dir)] = '\' then
  1464. SetLength(Dir, Pred(Length(Dir)));
  1465. Node := ParentNode.GetFirstChild;
  1466. if (not Assigned(Node)) and (not ExistingOnly) then
  1467. begin
  1468. ValidateDirectoryEx(ParentNode, rsRecursiveExisting, True);
  1469. Node := ParentNode.GetFirstChild;
  1470. end;
  1471. Result := nil;
  1472. while Assigned(Node) do
  1473. begin
  1474. if (UpperCase(GetDirName(Node)) = Dir) or (TNodeData(Node.Data).ShortName = Dir) then
  1475. begin
  1476. if Length(Path) > 0 then
  1477. begin
  1478. Result := SearchSubDirs(Node, Path)
  1479. end
  1480. else
  1481. begin
  1482. Result := Node;
  1483. end;
  1484. Exit;
  1485. end;
  1486. Node := ParentNode.GetNextChild(Node);
  1487. end;
  1488. end;
  1489. function SearchSubDirs(ParentNode: TTreeNode; Path: string): TTreeNode;
  1490. begin
  1491. Result := nil;
  1492. if Length(Path) > 0 then
  1493. begin
  1494. if (not TNodeData(ParentNode.Data).Scanned) and (not ExistingOnly) then
  1495. begin
  1496. ReadSubDirs(ParentNode, GetDriveTypetoNode(ParentNode));
  1497. end;
  1498. // Factored out of DoSearchSubDirs is remnant of Bug 956 superceded by Bug 1320
  1499. Result := DoSearchSubDirs(ParentNode, Path);
  1500. end;
  1501. end; {SearchSubDirs}
  1502. var
  1503. Drive: string;
  1504. P: Integer;
  1505. begin {FindNodeToPath}
  1506. Result := nil;
  1507. if Length(Path) < 3 then
  1508. Exit;
  1509. // Particularly when used by TDirView to delegate browsing to
  1510. // hidden drive view, the handle may not be created
  1511. HandleNeeded;
  1512. Drive := DriveInfo.GetDriveKey(Path);
  1513. if Assigned(GetDriveStatus(Drive).RootNode) then
  1514. begin
  1515. if DriveInfo.IsRealDrive(Drive) then
  1516. begin
  1517. System.Delete(Path, 1, 3);
  1518. end
  1519. else
  1520. if IsUncPath(Path) then
  1521. begin
  1522. System.Delete(Path, 1, 2);
  1523. P := Pos('\', Path);
  1524. if P = 0 then
  1525. begin
  1526. Path := '';
  1527. end
  1528. else
  1529. begin
  1530. System.Delete(Path, 1, P);
  1531. P := Pos('\', Path);
  1532. if P = 0 then
  1533. begin
  1534. Path := '';
  1535. end
  1536. else
  1537. begin
  1538. System.Delete(Path, 1, P);
  1539. end;
  1540. end;
  1541. end
  1542. else
  1543. begin
  1544. raise EConvertError.Create(Format(ErrorInvalidDrive, [Path]))
  1545. end;
  1546. if Length(Path) > 0 then
  1547. begin
  1548. if (not GetDriveStatus(Drive).Scanned) and (not ExistingOnly) then
  1549. begin
  1550. ScanDrive(Drive);
  1551. end;
  1552. Result := SearchSubDirs(GetDriveStatus(Drive).RootNode, UpperCase(Path));
  1553. end
  1554. else Result := GetDriveStatus(Drive).RootNode;
  1555. end;
  1556. end; {FindNodetoPath}
  1557. function TDriveView.FindNodeToPath(Path: string): TTreeNode;
  1558. begin
  1559. Result := DoFindNodeToPath(Path, False);
  1560. end;
  1561. function TDriveView.TryFindNodeToPath(Path: string): TTreeNode;
  1562. begin
  1563. Result := DoFindNodeToPath(Path, True);
  1564. end;
  1565. function TDriveView.CheckForSubDirs(Path: string): Boolean;
  1566. var
  1567. DosError: Integer;
  1568. SRec: TSearchRec;
  1569. begin
  1570. Result := False;
  1571. DosError := FindFirst(ApiPath(IncludeTrailingBackslash(Path) + '*.'), DirAttrMask, SRec);
  1572. while DosError = 0 do
  1573. begin
  1574. if (SRec.Name <> '.' ) and
  1575. (SRec.Name <> '..') and
  1576. (SRec.Attr and faDirectory <> 0) then
  1577. begin
  1578. Result := True;
  1579. Break;
  1580. end;
  1581. DosError := FindNext(SRec);
  1582. end;
  1583. FindClose(SRec);
  1584. end; {CheckForSubDirs}
  1585. function TDriveView.ReadSubDirs(Node: TTreeNode; DriveType: Integer): Boolean;
  1586. var
  1587. DosError: Integer;
  1588. SRec: TSearchRec;
  1589. NewNode: TTreeNode;
  1590. begin
  1591. Result := False;
  1592. DosError := FindFirst(ApiPath(IncludeTrailingBackslash(NodePath(Node)) + '*.*'), DirAttrMask, SRec);
  1593. while DosError = 0 do
  1594. begin
  1595. if (SRec.Name <> '.' ) and
  1596. (SRec.Name <> '..') and
  1597. (SRec.Attr and faDirectory <> 0) then
  1598. begin
  1599. NewNode := AddChildNode(Node, SRec);
  1600. if DoScanDir(NewNode) then
  1601. begin
  1602. // We have seen the SFGAO_HASSUBFOLDER to be absent on C: drive $Recycle.Bin
  1603. NewNode.HasChildren := Bool(TNodeData(NewNode.Data).shAttr and SFGAO_HASSUBFOLDER);
  1604. TNodeData(NewNode.Data).Scanned := not NewNode.HasChildren;
  1605. end
  1606. else
  1607. begin
  1608. NewNode.HasChildren := False;
  1609. TNodeData(NewNode.Data).Scanned := True;
  1610. end;
  1611. Result := True;
  1612. end;
  1613. DosError := FindNext(SRec);
  1614. end; {While DosError = 0}
  1615. FindClose(Srec);
  1616. TNodeData(Node.Data).Scanned := True;
  1617. if Result then SortChildren(Node, False)
  1618. else Node.HasChildren := False;
  1619. Application.ProcessMessages;
  1620. end; {ReadSubDirs}
  1621. function TDriveView.CallBackValidateDir(var Node: TTreeNode; Data: Pointer): Boolean;
  1622. var
  1623. WorkNode: TTreeNode;
  1624. DelNode: TTreeNode;
  1625. NewNode: TTreeNode;
  1626. SRec: TSearchRec;
  1627. SrecList: TStringList;
  1628. SubDirList: TStringList;
  1629. DosError: Integer;
  1630. Index: Integer;
  1631. NewDirFound: Boolean;
  1632. ParentDir: string;
  1633. NodeData: TNodeData;
  1634. ScanDirInfo: PScanDirInfo;
  1635. begin {CallBackValidateDir}
  1636. Result := True;
  1637. if (not Assigned(Node)) or (not Assigned(Node.Data)) then
  1638. Exit;
  1639. NewDirFound := False;
  1640. ScanDirInfo := PScanDirInfo(Data);
  1641. {Check, if directory still exists: (but not with root directory) }
  1642. if Assigned(Node.Parent) and (ScanDirInfo^.StartNode = Node) then
  1643. if not DirectoryExists(NodePathName(Node)) then
  1644. begin
  1645. WorkNode := Node.Parent;
  1646. if Selected = Node then
  1647. Selected := WorkNode;
  1648. if DropTarget = Node then
  1649. DropTarget := nil;
  1650. Node.Delete;
  1651. Node := nil;
  1652. Exit;
  1653. end;
  1654. WorkNode := Node.GetFirstChild;
  1655. NodeData := TNodeData(Node.Data);
  1656. if NodeData.Scanned and Assigned(WorkNode) then
  1657. {if node was already scanned: check wether the existing subnodes are still alive
  1658. and add all new subdirectories as subnodes:}
  1659. begin
  1660. if DoScanDir(Node) then
  1661. begin
  1662. ParentDir := IncludeTrailingBackslash(NodePath(Node));
  1663. {Build list of existing subnodes:}
  1664. SubDirList := TStringList.Create;
  1665. SubDirList.CaseSensitive := True; // We want to reflect changes in subfolder name case
  1666. while Assigned(WorkNode) do
  1667. begin
  1668. SubDirList.Add(TNodeData(WorkNode.Data).DirName);
  1669. WorkNode := Node.GetNextChild(WorkNode);
  1670. end;
  1671. {Sorting not required, because the subnodes are already sorted!}
  1672. SRecList := TStringList.Create;
  1673. SRecList.CaseSensitive := True;
  1674. DosError := FindFirst(ApiPath(ParentDir + '*.*'), DirAttrMask, SRec);
  1675. while DosError = 0 do
  1676. begin
  1677. if (Srec.Name <> '.' ) and
  1678. (Srec.Name <> '..') and
  1679. (Srec.Attr and faDirectory <> 0) then
  1680. begin
  1681. SrecList.Add(Srec.Name);
  1682. if not SubDirList.Find(Srec.Name, Index) then
  1683. {Subnode does not exists: add it:}
  1684. begin
  1685. NewNode := AddChildNode(Node, SRec);
  1686. NewNode.HasChildren := CheckForSubDirs(ParentDir + Srec.Name);
  1687. TNodeData(NewNode.Data).Scanned := Not NewNode.HasChildren;
  1688. NewDirFound := True;
  1689. end;
  1690. end;
  1691. DosError := FindNext(Srec);
  1692. end;
  1693. FindClose(Srec);
  1694. Sreclist.Sort;
  1695. {Remove not existing subnodes:}
  1696. WorkNode := Node.GetFirstChild;
  1697. while Assigned(WorkNode) do
  1698. begin
  1699. if not Assigned(WorkNode.Data) or
  1700. not SrecList.Find(TNodeData(WorkNode.Data).DirName, Index) then
  1701. begin
  1702. DelNode := WorkNode;
  1703. WorkNode := Node.GetNextChild(WorkNode);
  1704. DelNode.Delete;
  1705. end
  1706. else
  1707. begin
  1708. if (SrecList[Index] <> TNodeData(WorkNode.Data).DirName) then
  1709. begin
  1710. {Case of directory letters has changed:}
  1711. TNodeData(WorkNode.Data).DirName := SrecList[Index];
  1712. TNodeData(WorkNode.Data).ShortName := ExtractShortPathName(NodePathName(WorkNode));
  1713. WorkNode.Text := SrecList[Index];
  1714. end;
  1715. SrecList.Delete(Index);
  1716. WorkNode := Node.GetNextChild(WorkNode);
  1717. end;
  1718. end;
  1719. SrecList.Free;
  1720. SubDirList.Free;
  1721. {Sort subnodes:}
  1722. if NewDirFound then
  1723. SortChildren(Node, False);
  1724. end;
  1725. end
  1726. else
  1727. {Node was not already scanned:}
  1728. if (ScanDirInfo^.SearchNewDirs or
  1729. NodeData.Scanned or
  1730. (Node = ScanDirInfo^.StartNode)) and
  1731. DoScanDir(Node) then
  1732. begin
  1733. ReadSubDirs(Node, ScanDirInfo^.DriveType);
  1734. end;
  1735. end; {CallBackValidateDir}
  1736. procedure TDriveView.RebuildTree;
  1737. var
  1738. Drive: string;
  1739. begin
  1740. for Drive in FDriveStatus.Keys do
  1741. with GetDriveStatus(Drive) do
  1742. if Assigned(RootNode) and Scanned then
  1743. ValidateDirectory(RootNode);
  1744. end;
  1745. procedure TDriveView.ValidateCurrentDirectoryIfNotMonitoring;
  1746. begin
  1747. if Assigned(Selected) and
  1748. not Assigned(GetDriveStatus(GetDriveToNode(Selected)).DiscMonitor) then
  1749. begin
  1750. ValidateDirectory(Selected);
  1751. end;
  1752. end;
  1753. procedure TDriveView.ValidateDirectoryEx(Node: TTreeNode; Recurse: TRecursiveScan;
  1754. NewDirs: Boolean);
  1755. var
  1756. Info: PScanDirInfo;
  1757. SelDir: string;
  1758. SaveCursor: TCursor;
  1759. RestartWatchThread: Boolean;
  1760. SaveCanChange: Boolean;
  1761. CurrentPath: string;
  1762. Drive: string;
  1763. begin
  1764. if Assigned(Node) and Assigned(Node.Data) and
  1765. (not FValidateFlag) and DoScanDir(Node) then
  1766. begin
  1767. SelDir := Directory;
  1768. SaveCursor := Screen.Cursor;
  1769. if Self.Focused and (Screen.Cursor <> crHourGlass) then
  1770. Screen.Cursor := crHourGlass;
  1771. CurrentPath := NodePath(Node);
  1772. Drive := DriveInfo.GetDriveKey(CurrentPath);
  1773. if Node.Level = 0 then
  1774. GetDriveStatus(Drive).ChangeTimer.Enabled := False;
  1775. RestartWatchThread := WatchThreadActive;
  1776. try
  1777. if WatchThreadActive then
  1778. StopWatchThread;
  1779. FValidateFlag := True;
  1780. New(Info);
  1781. Info^.StartNode := Node;
  1782. Info^.SearchNewDirs := NewDirs;
  1783. Info^.DriveType := DriveInfo.Get(Drive).DriveType;
  1784. SaveCanChange := FCanChange;
  1785. FCanChange := True;
  1786. FChangeFlag := False;
  1787. IterateSubTree(Node, CallBackValidateDir, Recurse, coScanStartNode, Info);
  1788. FValidateFlag := False;
  1789. if (not Assigned(Selected)) and (Length(SelDir) > 0) then
  1790. Directory := ExtractFileDrive(SelDir);
  1791. if (SelDir <> Directory) and (not FChangeFlag) then
  1792. Change(Selected);
  1793. FCanChange := SaveCanChange;
  1794. Dispose(Info);
  1795. finally
  1796. if RestartWatchThread and FWatchDirectory and not WatchThreadActive then
  1797. StartWatchThread;
  1798. if Screen.Cursor <> SaveCursor then
  1799. Screen.Cursor := SaveCursor;
  1800. end;
  1801. end;
  1802. end; {ValidateDirectoryEx}
  1803. function TDriveView.GetDriveTypeToNode(Node: TTreeNode): Integer;
  1804. begin
  1805. Assert(Assigned(Node));
  1806. Result := DriveInfo.Get(GetDriveToNode(Node)).DriveType;
  1807. end; {GetDriveTypeToNode}
  1808. procedure TDriveView.CreateWatchThread(Drive: string);
  1809. begin
  1810. if csDesigning in ComponentState then
  1811. Exit;
  1812. if (not Assigned(GetDriveStatus(Drive).DiscMonitor)) and
  1813. FWatchDirectory and
  1814. (DriveInfo.Get(Drive).DriveType <> DRIVE_REMOTE) then
  1815. begin
  1816. with GetDriveStatus(Drive) do
  1817. begin
  1818. DiscMonitor := TDiscMonitor.Create(Self);
  1819. DiscMonitor.ChangeDelay := msThreadChangeDelay;
  1820. DiscMonitor.SubTree := True;
  1821. DiscMonitor.Filters := [moDirName];
  1822. DiscMonitor.OnChange := ChangeDetected;
  1823. DiscMonitor.OnInvalid := ChangeInvalid;
  1824. DiscMonitor.SetDirectory(DriveInfo.GetDriveRoot(Drive));
  1825. DiscMonitor.Open;
  1826. end;
  1827. UpdateDriveNotifications(Drive); // probably noop, as the monitor is not enabled yet
  1828. end;
  1829. end; {CreateWatchThread}
  1830. procedure TDriveView.SetWatchDirectory(Value: Boolean);
  1831. begin
  1832. if FWatchDirectory <> Value then
  1833. begin
  1834. FWatchDirectory := Value;
  1835. if (not (csDesigning in ComponentState)) and Value then
  1836. StartAllWatchThreads
  1837. else
  1838. StopAllWatchThreads;
  1839. end;
  1840. end; {SetAutoScan}
  1841. procedure TDriveView.SetDirView(Value: TDirView);
  1842. begin
  1843. if Assigned(FDirView) then
  1844. FDirView.DriveView := nil;
  1845. FDirView := Value;
  1846. if Assigned(FDirView) then
  1847. FDirView.DriveView := Self;
  1848. end; {SetDirView}
  1849. function TDriveView.NodeWatched(Node: TTreeNode): Boolean;
  1850. var
  1851. Drive: string;
  1852. begin
  1853. Drive := GetDriveToNode(Node);
  1854. Result := WatchThreadActive(Drive);
  1855. end; {NodeWatched}
  1856. procedure TDriveView.ChangeInvalid(Sender: TObject; const Directory: string;
  1857. const ErrorStr: string);
  1858. var
  1859. Drive: string;
  1860. begin
  1861. Drive := DriveInfo.GetDriveKey((Sender as TDiscMonitor).Directories[0]);
  1862. with GetDriveStatus(Drive) do
  1863. begin
  1864. DiscMonitor.Close;
  1865. end;
  1866. UpdateDriveNotifications(Drive);
  1867. end; {DirWatchChangeInvalid}
  1868. procedure TDriveView.ChangeDetected(Sender: TObject; const Directory: string;
  1869. var SubdirsChanged: Boolean);
  1870. var
  1871. DirChanged: string;
  1872. begin
  1873. if Sender is TDiscMonitor then
  1874. begin
  1875. DirChanged := (Sender as TDiscMonitor).Directories[0];
  1876. if Length(DirChanged) > 0 then
  1877. begin
  1878. with GetDriveStatus(DriveInfo.GetDriveKey(DirChanged)) do
  1879. begin
  1880. ChangeTimer.Interval := 0;
  1881. ChangeTimer.Interval := FChangeInterval;
  1882. ChangeTimer.Enabled := True;
  1883. end;
  1884. end;
  1885. end;
  1886. end; {DirWatchChangeDetected}
  1887. procedure TDriveView.ChangeTimerOnTimer(Sender: TObject);
  1888. var
  1889. DriveStatusPair: TDriveStatusPair;
  1890. begin
  1891. if (FChangeTimerSuspended = 0) and (Sender is TTimer) then
  1892. begin
  1893. for DriveStatusPair in FDriveStatus do
  1894. begin
  1895. if DriveStatusPair.Value.ChangeTimer = Sender then
  1896. begin
  1897. // Messages are processed during ValidateDirectory, so we may detect another change while
  1898. // updating the directory. Prevent the recursion.
  1899. // But retry the update afterwards (by reenabling the timer in ChangeDetected)
  1900. SuspendChangeTimer;
  1901. try
  1902. with DriveStatusPair.Value.ChangeTimer do
  1903. begin
  1904. Interval := 0;
  1905. Enabled := False;
  1906. end;
  1907. if Assigned(DriveStatusPair.Value.RootNode) then
  1908. begin
  1909. {Check also collapsed (invisible) subdirectories:}
  1910. ValidateDirectory(DriveStatusPair.Value.RootNode);
  1911. end;
  1912. finally
  1913. ResumeChangeTimer;
  1914. end;
  1915. end;
  1916. end;
  1917. end;
  1918. end; {ChangeTimerOnTimer}
  1919. procedure TDriveView.UpdateDriveNotifications(Drive: string);
  1920. var
  1921. NeedNotifications: Boolean;
  1922. Path: string;
  1923. DevBroadcastHandle: DEV_BROADCAST_HANDLE;
  1924. Size: Integer;
  1925. begin
  1926. if DriveInfo.IsFixedDrive(Drive) then
  1927. begin
  1928. with GetDriveStatus(Drive) do
  1929. begin
  1930. NeedNotifications :=
  1931. WatchThreadActive(Drive) and
  1932. (DriveInfo.Get(Drive).DriveType <> DRIVE_REMOTE) and
  1933. DriveInfo.Get(Drive).DriveReady;
  1934. if NeedNotifications <> (DriveHandle <> INVALID_HANDLE_VALUE) then
  1935. begin
  1936. if NeedNotifications then
  1937. begin
  1938. Path := DriveInfo.GetDriveRoot(Drive);
  1939. DriveHandle :=
  1940. CreateFile(PChar(Path), GENERIC_READ, FILE_SHARE_READ or FILE_SHARE_WRITE, nil,
  1941. OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS or FILE_ATTRIBUTE_NORMAL, 0);
  1942. if DriveHandle <> INVALID_HANDLE_VALUE then
  1943. begin
  1944. Size := SizeOf(DevBroadcastHandle);
  1945. ZeroMemory(@DevBroadcastHandle, Size);
  1946. DevBroadcastHandle.dbch_size := Size;
  1947. DevBroadcastHandle.dbch_devicetype := DBT_DEVTYP_HANDLE;
  1948. DevBroadcastHandle.dbch_handle := DriveHandle;
  1949. NotificationHandle :=
  1950. RegisterDeviceNotification(FInternalWindowHandle, @DevBroadcastHandle, DEVICE_NOTIFY_WINDOW_HANDLE);
  1951. if NotificationHandle = nil then
  1952. begin
  1953. CloseHandle(DriveHandle);
  1954. DriveHandle := INVALID_HANDLE_VALUE;
  1955. end;
  1956. end;
  1957. end
  1958. else
  1959. begin
  1960. UnregisterDeviceNotification(NotificationHandle);
  1961. NotificationHandle := nil;
  1962. CloseHandle(DriveHandle);
  1963. DriveHandle := INVALID_HANDLE_VALUE;
  1964. end;
  1965. end;
  1966. end;
  1967. end;
  1968. end;
  1969. procedure TDriveView.StartWatchThread;
  1970. var
  1971. Drive: string;
  1972. begin
  1973. if (csDesigning in ComponentState) or
  1974. not Assigned(Selected) or
  1975. not fWatchDirectory then Exit;
  1976. Drive := GetDriveToNode(Selected);
  1977. with GetDriveStatus(Drive) do
  1978. begin
  1979. if not Assigned(DiscMonitor) then
  1980. CreateWatchThread(Drive);
  1981. if Assigned(DiscMonitor) and not DiscMonitor.Enabled then
  1982. DiscMonitor.Enabled := True;
  1983. end;
  1984. UpdateDriveNotifications(Drive);
  1985. end; {StartWatchThread}
  1986. procedure TDriveView.StopWatchThread;
  1987. var
  1988. Drive: string;
  1989. begin
  1990. if Assigned(Selected) then
  1991. begin
  1992. Drive := GetDriveToNode(Selected);
  1993. with GetDriveStatus(Drive) do
  1994. if Assigned(DiscMonitor) then
  1995. DiscMonitor.Enabled := False;
  1996. UpdateDriveNotifications(Drive);
  1997. end;
  1998. end; {StopWatchThread}
  1999. procedure TDriveView.SuspendChangeTimer;
  2000. begin
  2001. Inc(FChangeTimerSuspended);
  2002. end;
  2003. procedure TDriveView.ResumeChangeTimer;
  2004. begin
  2005. Assert(FChangeTimerSuspended > 0);
  2006. Dec(FChangeTimerSuspended);
  2007. end;
  2008. procedure TDriveView.TerminateWatchThread(Drive: string);
  2009. begin
  2010. with GetDriveStatus(Drive) do
  2011. if Assigned(DiscMonitor) then
  2012. begin
  2013. DiscMonitor.Free;
  2014. DiscMonitor := nil;
  2015. end;
  2016. UpdateDriveNotifications(Drive);
  2017. end; {StopWatchThread}
  2018. procedure TDriveView.StartAllWatchThreads;
  2019. var
  2020. DriveStatusPair: TDriveStatusPair;
  2021. Drive: string;
  2022. begin
  2023. if (csDesigning in ComponentState) or (not FWatchDirectory) then
  2024. Exit;
  2025. for DriveStatusPair in FDriveStatus do
  2026. with DriveStatusPair.Value do
  2027. if Scanned then
  2028. begin
  2029. if not Assigned(DiscMonitor) then
  2030. CreateWatchThread(DriveStatusPair.Key);
  2031. if Assigned(DiscMonitor) and (not DiscMonitor.Active) then
  2032. begin
  2033. DiscMonitor.Open;
  2034. UpdateDriveNotifications(DriveStatusPair.Key);
  2035. end;
  2036. end;
  2037. if Assigned(Selected) then
  2038. begin
  2039. Drive := GetDriveToNode(Selected);
  2040. if not DriveInfo.IsFixedDrive(Drive) then
  2041. begin
  2042. StartWatchThread;
  2043. end;
  2044. end;
  2045. end; {StartAllWatchThreads}
  2046. procedure TDriveView.StopAllWatchThreads;
  2047. var
  2048. DriveStatusPair: TDriveStatusPair;
  2049. begin
  2050. if (csDesigning in ComponentState) or (not FWatchDirectory) then
  2051. Exit;
  2052. for DriveStatusPair in FDriveStatus do
  2053. with DriveStatusPair.Value do
  2054. begin
  2055. if Assigned(DiscMonitor) then
  2056. begin
  2057. DiscMonitor.Close;
  2058. UpdateDriveNotifications(DriveStatusPair.Key);
  2059. end;
  2060. end;
  2061. end; {StopAllWatchThreads}
  2062. function TDriveView.WatchThreadActive(Drive: string): Boolean;
  2063. begin
  2064. Result := FWatchDirectory and
  2065. Assigned(GetDriveStatus(Drive).DiscMonitor) and
  2066. GetDriveStatus(Drive).DiscMonitor.Active and
  2067. GetDriveStatus(Drive).DiscMonitor.Enabled;
  2068. end; {WatchThreadActive}
  2069. function TDriveView.WatchThreadActive: Boolean;
  2070. var
  2071. Drive: string;
  2072. begin
  2073. if not Assigned(Selected) then
  2074. begin
  2075. Result := False;
  2076. Exit;
  2077. end;
  2078. Drive := GetDriveToNode(Selected);
  2079. Result := WatchThreadActive(Drive);
  2080. end; {WatchThreadActive}
  2081. procedure TDriveView.SetFullDriveScan(DoFullDriveScan: Boolean);
  2082. begin
  2083. FFullDriveScan := DoFullDriveScan;
  2084. end; {SetAutoScan}
  2085. function TDriveView.FindPathNode(Path: string): TTreeNode;
  2086. var
  2087. PossiblyHiddenPath: string;
  2088. Attrs: Integer;
  2089. begin
  2090. if Assigned(FOnNeedHiddenDirectories) and
  2091. (not ShowHiddenDirs) and
  2092. DirectoryExistsFix(Path) then // do not even bother if the path does not exist
  2093. begin
  2094. PossiblyHiddenPath := ExcludeTrailingPathDelimiter(Path);
  2095. while (PossiblyHiddenPath <> '') and
  2096. (not IsRootPath(PossiblyHiddenPath)) do // Drives have hidden attribute
  2097. begin
  2098. Attrs := FileGetAttr(PossiblyHiddenPath, False);
  2099. if (Attrs and faHidden) = faHidden then
  2100. begin
  2101. if Assigned(FOnNeedHiddenDirectories) then
  2102. begin
  2103. FOnNeedHiddenDirectories(Self);
  2104. end;
  2105. Break;
  2106. end
  2107. else
  2108. begin
  2109. PossiblyHiddenPath := ExtractFileDir(PossiblyHiddenPath);
  2110. end;
  2111. end;
  2112. end;
  2113. {Find existing path or parent path of not existing path:}
  2114. repeat
  2115. Result := FindNodeToPath(Path);
  2116. if not Assigned(Result) then
  2117. Path := ExtractFilePath(ExcludeTrailingBackslash(Path));
  2118. until Assigned(Result) or (Length(Path) < 3);
  2119. end;
  2120. procedure TDriveView.SetDirectory(Value: string);
  2121. begin
  2122. Value := IncludeTrailingBackslash(Value);
  2123. FDirectory := Value;
  2124. inherited;
  2125. if Assigned(Selected) and (Selected.Level = 0) then
  2126. begin
  2127. if not GetDriveStatus(GetDriveToNode(Selected)).Scanned then
  2128. ScanDrive(GetDriveToNode(Selected));
  2129. end;
  2130. end; {SetDirectory}
  2131. function TDriveView.GetDirName(Node: TTreeNode): string;
  2132. begin
  2133. if Assigned(Node) and Assigned(Node.Data) then
  2134. Result := TNodeData(Node.Data).DirName
  2135. else
  2136. Result := '';
  2137. end; {GetDirName}
  2138. {GetDrive: returns the drive of the Node.}
  2139. function TDriveView.GetDriveToNode(Node: TTreeNode): string;
  2140. var
  2141. Path: string;
  2142. begin
  2143. if (not Assigned (Node)) or (not Assigned(Node.Data)) then
  2144. raise ENodeNotAssigned.Create(Format(ErrorNodeNA, ['GetDrive']));
  2145. Path := NodePath(Node);
  2146. Result := DriveInfo.GetDriveKey(Path);
  2147. end; {GetDrive}
  2148. {RootNode: returns the rootnode to the Node:}
  2149. function TDriveView.RootNode(Node: TTreeNode): TTreeNode;
  2150. begin
  2151. Result := Node;
  2152. if not Assigned(Node) then
  2153. raise ENodeNotAssigned.Create(Format(ErrorNodeNA, ['RootNode']));
  2154. while Assigned(Result.Parent) do
  2155. Result := Result.Parent;
  2156. end; {RootNode}
  2157. function TDriveView.GetDisplayName(Node: TTreeNode): string;
  2158. begin
  2159. Result := '';
  2160. if (not Assigned(Node)) or (not Assigned(Node.Data)) then
  2161. raise ENodeNotAssigned.Create(Format(ErrorNodeNA, ['GetDisplayName']));
  2162. if Node.Level = 0 then Result := GetDriveText(GetDriveToNode(Node))
  2163. else
  2164. begin
  2165. Result := GetDirName(Node);
  2166. end;
  2167. end; {GetDisplayName}
  2168. procedure TDriveView.SetShowVolLabel(ShowIt: Boolean);
  2169. begin
  2170. if ShowIt = FShowVolLabel then
  2171. Exit;
  2172. FShowVolLabel := ShowIt;
  2173. RefreshRootNodes(dvdsFloppy);
  2174. end; {SetShowVolLabel}
  2175. procedure TDriveView.DisplayContextMenu(Node: TTreeNode; Point: TPoint);
  2176. var
  2177. Verb: string;
  2178. DirWatched: Boolean;
  2179. begin
  2180. DirWatched := NodeWatched(Node) and WatchThreadActive;
  2181. Assert(Node <> nil);
  2182. if Node <> Selected then
  2183. DropTarget := Node;
  2184. Verb := EmptyStr;
  2185. if Assigned(FOnDisplayContextMenu) then
  2186. FOnDisplayContextMenu(Self);
  2187. ShellDisplayContextMenu(FParentForm.Handle, Point, NodePathName(Node),
  2188. CanEdit(Node), Verb, False);
  2189. if Verb = shcRename then Node.EditText
  2190. else
  2191. if Verb = shcCut then
  2192. begin
  2193. LastClipBoardOperation := cboCut;
  2194. LastPathCut := NodePathName(Node);
  2195. end
  2196. else
  2197. if Verb = shcCopy then LastClipBoardOperation := cboCopy
  2198. else
  2199. if Verb = shcPaste then
  2200. PasteFromClipBoard(NodePathName(Node));
  2201. DropTarget := nil;
  2202. if not DirWatched then
  2203. ValidateDirectory(Node);
  2204. end; {DisplayContextMenu (2)}
  2205. procedure TDriveView.DisplayPropertiesMenu(Node: TTreeNode);
  2206. begin
  2207. Assert(Assigned(Node));
  2208. ShellExecuteContextCommand(FParentForm.Handle, shcProperties, NodePathName(Node));
  2209. end; {ContextMenu}
  2210. procedure TDriveView.SetSelected(Node: TTreeNode);
  2211. begin
  2212. if Node <> Selected then
  2213. begin
  2214. FChangeFlag := False;
  2215. FCanChange := True;
  2216. inherited Selected := Node;
  2217. if not FChangeFlag then
  2218. Change(Selected);
  2219. end;
  2220. end; {SetSelected}
  2221. {Called by TFileDeleteThread, when a file deletion was detected by the D&D receiving application:}
  2222. procedure TDriveView.SignalDirDelete(Sender: TObject; Files: TStringList);
  2223. begin
  2224. if Files.Count > 0 then
  2225. ValidateDirectory(FindNodeToPath(Files[0]));
  2226. end; {SignalDirDelete}
  2227. function TDriveView.DDSourceEffects: TDropEffectSet;
  2228. begin
  2229. if FDragNode.Level = 0 then
  2230. Result := [deLink]
  2231. else
  2232. Result := [deLink, deCopy, deMove];
  2233. end;
  2234. procedure TDriveView.DDChooseEffect(KeyState: Integer; var Effect: Integer; PreferredEffect: Integer);
  2235. begin
  2236. if DropTarget = nil then Effect := DROPEFFECT_NONE
  2237. else
  2238. if (KeyState and (MK_CONTROL or MK_SHIFT) = 0) and (PreferredEffect = 0) then
  2239. begin
  2240. if FDragDrive <> '' then
  2241. begin
  2242. if FExeDrag and DriveInfo.IsFixedDrive(GetDriveToNode(DropTarget)) and DriveInfo.IsFixedDrive(FDragDrive) then
  2243. begin
  2244. Effect := DROPEFFECT_LINK;
  2245. end
  2246. else
  2247. if (Effect = DROPEFFECT_COPY) and
  2248. (SameText(FDragDrive, GetDriveToNode(DropTarget)) and
  2249. (FDragDropFilesEx.AvailableDropEffects and DROPEFFECT_MOVE <> 0)) then
  2250. begin
  2251. Effect := DROPEFFECT_MOVE;
  2252. end;
  2253. end;
  2254. end;
  2255. inherited;
  2256. end;
  2257. function TDriveView.DragCompleteFileList: Boolean;
  2258. begin
  2259. Result := (GetDriveTypeToNode(FDragNode) <> DRIVE_REMOVABLE);
  2260. end;
  2261. function TDriveView.DDExecute: TDragResult;
  2262. var
  2263. WatchThreadOK: Boolean;
  2264. DragParentPath: string;
  2265. DragPath: string;
  2266. begin
  2267. WatchThreadOK := WatchThreadActive;
  2268. Result := FDragDropFilesEx.Execute(nil);
  2269. if (Result = drMove) and (not WatchThreadOK) then
  2270. begin
  2271. DragPath := NodePathName(FDragNode);
  2272. if Assigned(FDragNode.Parent) then
  2273. DragParentPath := NodePathName(FDragNode.Parent)
  2274. else
  2275. DragParentPath := DragPath;
  2276. if (FDragNode.Level > 0) or (DragParentPath <> NodePathName(Selected.Parent)) then
  2277. begin
  2278. FDragNode := FindNodeToPath(DragPath);
  2279. if Assigned(FDragNode) then
  2280. begin
  2281. FDragFileList.Clear;
  2282. FDragFileList.Add(DragPath);
  2283. TFileDeleteThread.Create(FDragFileList, MaxWaitTimeOut, SignalDirDelete);
  2284. end;
  2285. end;
  2286. end;
  2287. end;
  2288. procedure TDriveView.PerformDragDropFileOperation(Node: TTreeNode; Effect: Integer);
  2289. var
  2290. Index: Integer;
  2291. SourcePath: string;
  2292. SourceParentPath: string;
  2293. SourceIsDirectory: Boolean;
  2294. SaveCursor: TCursor;
  2295. SourceNode, TargetNode: TTreeNode;
  2296. TargetPath: string;
  2297. IsRecycleBin: Boolean;
  2298. begin
  2299. TargetPath := NodePathName(Node);
  2300. IsRecycleBin := NodeIsRecycleBin(Node);
  2301. if FDragDropFilesEx.FileList.Count = 0 then
  2302. Exit;
  2303. SaveCursor := Screen.Cursor;
  2304. Screen.Cursor := crHourGlass;
  2305. SourcePath := EmptyStr;
  2306. try
  2307. if (Effect = DROPEFFECT_COPY) or (Effect = DROPEFFECT_MOVE) then
  2308. begin
  2309. StopAllWatchThreads;
  2310. if Assigned(FDirView) then
  2311. FDirView.StopWatchThread;
  2312. if Assigned(DropSourceControl) and
  2313. (DropSourceControl is TDirView) and
  2314. (DropSourceControl <> FDirView) then
  2315. begin
  2316. TDirView(DropSourceControl).StopWatchThread;
  2317. end;
  2318. if DropFiles(
  2319. DragDropFilesEx, Effect, FFileOperator, TargetPath, false, IsRecycleBin, ConfirmDelete, ConfirmOverwrite, False,
  2320. Self, OnDDFileOperation, SourcePath, SourceIsDirectory) then
  2321. begin
  2322. if Assigned(FOnDDFileOperationExecuted) then
  2323. FOnDDFileOperationExecuted(Self, Effect, SourcePath, TargetPath);
  2324. end;
  2325. ClearDragFileList(FDragDropFilesEx.FileList);
  2326. // TDirView.PerformDragDropFileOperation validates the SourcePath and that actually seems correct
  2327. SourceParentPath := ExtractFilePath(ExcludeTrailingBackslash(SourcePath));
  2328. end
  2329. else
  2330. if Effect = DROPEFFECT_LINK then
  2331. { Create Link requested: }
  2332. begin
  2333. for Index := 0 to FDragDropFilesEx.FileList.Count - 1 do
  2334. begin
  2335. if not DropLink(PFDDListItem(FDragDropFilesEx.FileList[Index]), TargetPath) then
  2336. begin
  2337. DDError(DDCreateShortCutError);
  2338. end;
  2339. end;
  2340. end;
  2341. if Effect = DROPEFFECT_MOVE then
  2342. Items.BeginUpdate;
  2343. {Update source directory, if move-operation was performed:}
  2344. if ((Effect = DROPEFFECT_MOVE) or IsRecycleBin) then
  2345. begin
  2346. // See comment in corresponding operation in TDirView.PerformDragDropFileOperation
  2347. SourceNode := TryFindNodeToPath(SourceParentPath);
  2348. if Assigned(SourceNode) then
  2349. ValidateDirectory(SourceNode);
  2350. end;
  2351. {Update subdirectories of target directory:}
  2352. TargetNode := FindNodeToPath(TargetPath);
  2353. if Assigned(TargetNode) then
  2354. ValidateDirectory(TargetNode)
  2355. else
  2356. ValidateDirectory(GetDriveStatus(DriveInfo.GetDriveKey(TargetPath)).RootNode);
  2357. if Effect = DROPEFFECT_MOVE then
  2358. Items.EndUpdate;
  2359. {Update linked component TDirView:}
  2360. if Assigned(FDirView) and (not FDirView.WatchThreadActive) then
  2361. begin
  2362. case Effect of
  2363. DROPEFFECT_COPY,
  2364. DROPEFFECT_LINK:
  2365. if (IncludeTrailingBackslash(TargetPath) = IncludeTrailingBackslash(DirView.Path)) then
  2366. FDirView.Reload2;
  2367. DROPEFFECT_MOVE:
  2368. if (IncludeTrailingBackslash(TargetPath) = IncludeTrailingBackslash(DirView.Path)) or
  2369. (IncludeTrailingBackslash(SourceParentPath) = IncludeTrailingBackslash(DirView.Path)) then
  2370. begin
  2371. if FDirView <> DropSourceControl then FDirView.Reload2;
  2372. end;
  2373. end; {Case}
  2374. end;
  2375. {Update the DropSource control, if files are moved and it is a TDirView:}
  2376. if (Effect = DROPEFFECT_MOVE) and (DropSourceControl is TDirView) then
  2377. begin
  2378. TDirView(DropSourceControl).ValidateSelectedFiles;
  2379. end;
  2380. finally
  2381. FFileOperator.OperandFrom.Clear;
  2382. FFileOperator.OperandTo.Clear;
  2383. StartAllWatchThreads;
  2384. if Assigned(FDirView) and (not FDirView.WatchThreadActive) then
  2385. FDirView.StartWatchThread;
  2386. if Assigned(DropSourceControl) and (DropSourceControl is TDirView) and
  2387. (not TDirView(DropSourceControl).WatchThreadActive) then
  2388. TDirView(DropSourceControl).StartWatchThread;
  2389. Screen.Cursor := SaveCursor;
  2390. end;
  2391. end; {PerformDragDropFileOperation}
  2392. function TDriveView.GetCanUndoCopyMove: Boolean;
  2393. begin
  2394. Result := Assigned(FFileOperator) and FFileOperator.CanUndo;
  2395. end; {CanUndoCopyMove}
  2396. function TDriveView.UndoCopyMove: Boolean;
  2397. var
  2398. LastTarget: string;
  2399. LastSource: string;
  2400. begin
  2401. Result := False;
  2402. if FFileOperator.CanUndo then
  2403. begin
  2404. Lasttarget := FFileOperator.LastOperandTo[0];
  2405. LastSource := FFileOperator.LastOperandFrom[0];
  2406. StopAllWatchThreads;
  2407. Result := FFileOperator.UndoExecute;
  2408. ValidateDirectory(FindNodeToPath(ExtractFilePath(LastTarget)));
  2409. ValidateDirectory(FindNodeToPath(ExtractFilePath(LastSource)));
  2410. StartAllWatchThreads;
  2411. if Assigned(FDirView) then
  2412. with FDirView do
  2413. if not WatchThreadActive then
  2414. begin
  2415. if (IncludeTrailingBackslash(ExtractFilePath(LastTarget)) = IncludeTrailingBackslash(Path)) or
  2416. (IncludeTrailingBackslash(ExtractFilePath(LastSource)) = IncludeTrailingBackslash(Path)) then
  2417. Reload2;
  2418. end;
  2419. end;
  2420. end; {UndoCopyMove}
  2421. {Clipboard operations:}
  2422. procedure TDriveView.SetLastPathCut(Path: string);
  2423. var
  2424. Node: TTreeNode;
  2425. begin
  2426. if FLastPathCut <> Path then
  2427. begin
  2428. Node := FindNodeToPath(FLastPathCut);
  2429. if Assigned(Node) then
  2430. begin
  2431. FLastPathCut := Path;
  2432. Node.Cut := False;
  2433. end;
  2434. Node := FindNodeToPath(Path);
  2435. if Assigned(Node) then
  2436. begin
  2437. FLastPathCut := Path;
  2438. Node.Cut := True;
  2439. end;
  2440. end;
  2441. end; {SetLastNodeCut}
  2442. procedure TDriveView.EmptyClipboard;
  2443. begin
  2444. if Windows.OpenClipBoard(0) then
  2445. begin
  2446. Windows.EmptyClipBoard;
  2447. Windows.CloseClipBoard;
  2448. LastPathCut := '';
  2449. LastClipBoardOperation := cboNone;
  2450. if Assigned(FDirView) then
  2451. FDirView.EmptyClipboard;
  2452. end;
  2453. end; {EmptyClipBoard}
  2454. function TDriveView.CopyToClipBoard(Node: TTreeNode): Boolean;
  2455. begin
  2456. Result := Assigned(Selected);
  2457. if Result then
  2458. begin
  2459. EmptyClipBoard;
  2460. ClearDragFileList(FDragDropFilesEx.FileList);
  2461. AddToDragFileList(FDragDropFilesEx.FileList, Selected);
  2462. Result := FDragDropFilesEx.CopyToClipBoard;
  2463. LastClipBoardOperation := cboCopy;
  2464. end;
  2465. end; {CopyToClipBoard}
  2466. function TDriveView.CutToClipBoard(Node: TTreeNode): Boolean;
  2467. begin
  2468. Result := Assigned(Node) and (Node.Level > 0) and CopyToClipBoard(Node);
  2469. if Result then
  2470. begin
  2471. LastPathCut := NodePathName(Node);
  2472. LastClipBoardOperation := cboCut;
  2473. end;
  2474. end; {CutToClipBoard}
  2475. function TDriveView.CanPasteFromClipBoard: Boolean;
  2476. begin
  2477. Result := False;
  2478. if Assigned(Selected) and Windows.OpenClipboard(0) then
  2479. begin
  2480. Result := IsClipboardFormatAvailable(CF_HDROP);
  2481. Windows.CloseClipBoard;
  2482. end;
  2483. end; {CanPasteFromClipBoard}
  2484. function TDriveView.PasteFromClipBoard(TargetPath: String = ''): Boolean;
  2485. begin
  2486. ClearDragFileList(FDragDropFilesEx.FileList);
  2487. Result := False;
  2488. if CanPasteFromClipBoard and {MP}FDragDropFilesEx.GetFromClipBoard{/MP}
  2489. then
  2490. begin
  2491. if TargetPath = '' then
  2492. TargetPath := NodePathName(Selected);
  2493. case LastClipBoardOperation of
  2494. cboCopy,
  2495. cboNone:
  2496. begin
  2497. PerformDragDropFileOperation(Selected, DROPEFFECT_COPY);
  2498. if Assigned(FOnDDExecuted) then
  2499. FOnDDExecuted(Self, DROPEFFECT_COPY);
  2500. end;
  2501. cboCut:
  2502. begin
  2503. PerformDragDropFileOperation(Selected, DROPEFFECT_MOVE);
  2504. if Assigned(FOnDDExecuted) then
  2505. FOnDDExecuted(Self, DROPEFFECT_MOVE);
  2506. EmptyClipBoard;
  2507. end;
  2508. end;
  2509. Result := True;
  2510. end;
  2511. end; {PasteFromClipBoard}
  2512. end.